/** * Registers services to the container. * * @param ContainerBuilder $container * The ContainerBuilder to register services to. */ public function register(ContainerBuilder $container) { static::$container = $container; $class_map = $this->AutoloaderInit(); $ns = []; foreach ($class_map as $name => $path) { $path = preg_replace("/\\.php\$/i", '', $path); $path = explode(DIRECTORY_SEPARATOR, $path); $name = explode('\\', $name); while (end($path) === end($name)) { array_pop($path); array_pop($name); } $path = implode(DIRECTORY_SEPARATOR, $path); $name = implode('\\', $name); if (is_dir($path)) { if (is_dir($path . '/Plugin') || is_dir($path . '/Entity') || is_dir($path . '/Element')) { $ns[$name][] = $path; } } } foreach ($ns as $name => $path) { $path = array_unique($path); $ns[$name] = count($path) == 1 ? reset($path) : $path; } $ns += $container->getParameter('container.namespaces'); $container->setParameter('container.namespaces', $ns); $yaml_loader = new YamlFileLoader($container); foreach (static::getFiles('/^.+\\.services\\.yml$/i') as $file) { $yaml_loader->load($file); } }
public function testParseDefinitionsWithProvider() { $yml = <<<YAML services: example_service: class: \\Drupal\\Core\\ExampleClass YAML; vfsStream::setup('drupal', NULL, ['modules/example/example.yml' => $yml]); $builder = new ContainerBuilder(); $yaml_file_loader = new YamlFileLoader($builder); $yaml_file_loader->load('vfs://drupal/modules/example/example.yml'); $this->assertEquals(['_provider' => [['provider' => 'example']]], $builder->getDefinition('example_service')->getTags()); }
/** * {@inheritdoc} */ public function getContainerDefinition() { FileCacheFactory::setConfiguration(array('default' => array('class' => '\\Drupal\\Component\\FileCache\\NullFileCache'))); $container_builder = new ContainerBuilder(); $yaml_loader = new YamlFileLoader($container_builder); foreach (module_list() as $module) { $filename = drupal_get_filename('module', $module); $services = dirname($filename) . "/{$module}.services.yml"; if (file_exists($services)) { $yaml_loader->load($services); } } // Disabled for now. // $container_builder->compile(); $dumper = new PhpArrayDumper($container_builder); return $dumper->getArray(); }
/** * Compiles a new service container. * * @return ContainerBuilder The compiled service container */ protected function compileContainer() { // We are forcing a container build so it is reasonable to assume that the // calling method knows something about the system has changed requiring the // container to be dumped to the filesystem. if ($this->allowDumping) { $this->containerNeedsDumping = TRUE; } $this->initializeServiceProviders(); $container = $this->getContainerBuilder(); $container->set('kernel', $this); $container->setParameter('container.modules', $this->getModulesParameter()); // Get a list of namespaces and put it onto the container. $namespaces = $this->getModuleNamespacesPsr4($this->getModuleFileNames()); // Add all components in \Drupal\Core and \Drupal\Component that have one of // the following directories: // - Element // - Entity // - Plugin foreach (array('Core', 'Component') as $parent_directory) { $path = 'core/lib/Drupal/' . $parent_directory; $parent_namespace = 'Drupal\\' . $parent_directory; foreach (new \DirectoryIterator($this->root . '/' . $path) as $component) { /** @var $component \DirectoryIterator */ $pathname = $component->getPathname(); if (!$component->isDot() && $component->isDir() && (is_dir($pathname . '/Plugin') || is_dir($pathname . '/Entity') || is_dir($pathname . '/Element'))) { $namespaces[$parent_namespace . '\\' . $component->getFilename()] = $path . '/' . $component->getFilename(); } } } $container->setParameter('container.namespaces', $namespaces); // Store the default language values on the container. This is so that the // default language can be configured using the configuration factory. This // avoids the circular dependencies that would created by // \Drupal\language\LanguageServiceProvider::alter() and allows the default // language to not be English in the installer. $default_language_values = Language::$defaultValues; if ($system = $this->getConfigStorage()->read('system.site')) { if ($default_language_values['id'] != $system['langcode']) { $default_language_values = array('id' => $system['langcode']); } } $container->setParameter('language.default_values', $default_language_values); // Register synthetic services. $container->register('class_loader')->setSynthetic(TRUE); $container->register('kernel', 'Symfony\\Component\\HttpKernel\\KernelInterface')->setSynthetic(TRUE); $container->register('service_container', 'Symfony\\Component\\DependencyInjection\\ContainerInterface')->setSynthetic(TRUE); // Register application services. $yaml_loader = new YamlFileLoader($container); foreach ($this->serviceYamls['app'] as $filename) { $yaml_loader->load($filename); } foreach ($this->serviceProviders['app'] as $provider) { if ($provider instanceof ServiceProviderInterface) { $provider->register($container); } } // Register site-specific service overrides. foreach ($this->serviceYamls['site'] as $filename) { $yaml_loader->load($filename); } foreach ($this->serviceProviders['site'] as $provider) { if ($provider instanceof ServiceProviderInterface) { $provider->register($container); } } // Identify all services whose instances should be persisted when rebuilding // the container during the lifetime of the kernel (e.g., during a kernel // reboot). Include synthetic services, because by definition, they cannot // be automatically reinstantiated. Also include services tagged to persist. $persist_ids = array(); foreach ($container->getDefinitions() as $id => $definition) { if ($definition->isSynthetic() || $definition->getTag('persist')) { $persist_ids[] = $id; } } $container->setParameter('persistIds', $persist_ids); $container->compile(); return $container; }