From ecf2fa7296d0640088c6bbf3a8fd7f3459d6a28c Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Thu, 15 May 2014 10:49:31 +0100 Subject: [PATCH] Revert "Issue #2241633 by sun: Simplify site-specific service overrides." This reverts commit c5c075dfd8b3d71a6a3a3c2c926ab1ba7bc9c9ce. --- .../Compiler/ModifyServiceDefinitionsPass.php | 8 +- core/lib/Drupal/Core/DrupalKernel.php | 101 +++++++----------- .../lib/Drupal/Core/DrupalKernelInterface.php | 5 +- .../DrupalKernel/DrupalKernelSiteTest.php | 57 ---------- 4 files changed, 39 insertions(+), 132 deletions(-) delete mode 100644 core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelSiteTest.php diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/ModifyServiceDefinitionsPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/ModifyServiceDefinitionsPass.php index 8ecded9ba98..25614e351bb 100644 --- a/core/lib/Drupal/Core/DependencyInjection/Compiler/ModifyServiceDefinitionsPass.php +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/ModifyServiceDefinitionsPass.php @@ -28,13 +28,7 @@ class ModifyServiceDefinitionsPass implements CompilerPassInterface { if (!($kernel instanceof DrupalKernelInterface)) { return; } - $providers = $kernel->getServiceProviders('app'); - foreach ($providers as $provider) { - if ($provider instanceof ServiceModifierInterface) { - $provider->alter($container); - } - } - $providers = $kernel->getServiceProviders('site'); + $providers = $kernel->getServiceProviders(); foreach ($providers as $provider) { if ($provider instanceof ServiceModifierInterface) { $provider->alter($container); diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index 9603d38d99a..8baa1014d7d 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -109,6 +109,13 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { */ protected $configStorage; + /** + * The list of the classnames of the service providers in this kernel. + * + * @var array + */ + protected $serviceProviderClasses; + /** * Whether the container can be dumped. * @@ -124,33 +131,14 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { protected $containerNeedsDumping; /** - * List of discovered services.yml pathnames. - * - * This is a nested array whose top-level keys are 'app' and 'site', denoting - * the origin of a service provider. Site-specific providers have to be - * collected separately, because they need to be processed last, so as to be - * able to override services from application service providers. + * Holds the list of YAML files containing service definitions. * * @var array */ protected $serviceYamls; /** - * List of discovered service provider class names. - * - * This is a nested array whose top-level keys are 'app' and 'site', denoting - * the origin of a service provider. Site-specific providers have to be - * collected separately, because they need to be processed last, so as to be - * able to override services from application service providers. - * - * @var array - */ - protected $serviceProviderClasses; - - /** - * List of instantiated service provider classes. - * - * @see \Drupal\Core\DrupalKernel::$serviceProviderClasses + * The array of registered service providers. * * @var array */ @@ -215,18 +203,16 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { * {@inheritdoc} */ public function discoverServiceProviders() { + $serviceProviders = array( + 'CoreServiceProvider' => new CoreServiceProvider(), + ); $this->serviceYamls = array( - 'app' => array(), - 'site' => array(), + 'core/core.services.yml' ); - $this->serviceProviderClasses = array( - 'app' => array(), - 'site' => array(), - ); - $this->serviceYamls['app']['core'] = 'core/core.services.yml'; - $this->serviceProviderClasses['app']['core'] = 'Drupal\Core\CoreServiceProvider'; + $this->serviceProviderClasses = array('Drupal\Core\CoreServiceProvider'); - // Retrieve enabled modules and register their namespaces. + // Ensure we know what modules are enabled and that their namespaces are + // registered. if (!isset($this->moduleList)) { $extensions = $this->getConfigStorage()->read('core.extension'); $this->moduleList = isset($extensions['module']) ? $extensions['module'] : array(); @@ -240,35 +226,34 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { $name = "{$camelized}ServiceProvider"; $class = "Drupal\\{$module}\\{$name}"; if (class_exists($class)) { - $this->serviceProviderClasses['app'][$module] = $class; + $serviceProviders[$name] = new $class(); + $this->serviceProviderClasses[] = $class; } $filename = dirname($module_filenames[$module]) . "/$module.services.yml"; if (file_exists($filename)) { - $this->serviceYamls['app'][$module] = $filename; + $this->serviceYamls[] = $filename; } } - // Add site-specific service providers. + // Add site specific or test service providers. if (!empty($GLOBALS['conf']['container_service_providers'])) { - foreach ($GLOBALS['conf']['container_service_providers'] as $class) { - if (class_exists($class)) { - $this->serviceProviderClasses['site'][] = $class; - } + foreach ($GLOBALS['conf']['container_service_providers'] as $name => $class) { + $serviceProviders[$name] = new $class(); + $this->serviceProviderClasses[] = $class; } } + // Add site specific or test YAMLs. if (!empty($GLOBALS['conf']['container_yamls'])) { - $this->serviceYamls['site'] = $GLOBALS['conf']['container_yamls']; - } - if (file_exists($site_services_yml = conf_path() . '/services.yml')) { - $this->serviceYamls['site'][] = $site_services_yml; + $this->serviceYamls = array_merge($this->serviceYamls, $GLOBALS['conf']['container_yamls']); } + return $serviceProviders; } /** * {@inheritdoc} */ - public function getServiceProviders($origin) { - return $this->serviceProviders[$origin]; + public function getServiceProviders() { + return $this->serviceProviders; } /** @@ -511,6 +496,7 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { $this->initializeServiceProviders(); $container = $this->getContainerBuilder(); $container->set('kernel', $this); + $container->setParameter('container.service_providers', $this->serviceProviderClasses); $container->setParameter('container.modules', $this->getModulesParameter()); // Get a list of namespaces and put it onto the container. @@ -545,22 +531,11 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { $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) { + foreach ($this->serviceYamls 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) { + foreach ($this->serviceProviders as $provider) { if ($provider instanceof ServiceProviderInterface) { $provider->register($container); } @@ -588,15 +563,13 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { * @throws \LogicException */ protected function initializeServiceProviders() { - $this->discoverServiceProviders(); - $this->serviceProviders = array( - 'app' => array(), - 'site' => array(), - ); - foreach ($this->serviceProviderClasses as $origin => $classes) { - foreach ($classes as $name => $class) { - $this->serviceProviders[$origin][$name] = new $class; + $this->serviceProviders = array(); + + foreach ($this->discoverServiceProviders() as $name => $provider) { + if (isset($this->serviceProviders[$name])) { + throw new \LogicException(sprintf('Trying to register two service providers with the same name "%s"', $name)); } + $this->serviceProviders[$name] = $provider; } } diff --git a/core/lib/Drupal/Core/DrupalKernelInterface.php b/core/lib/Drupal/Core/DrupalKernelInterface.php index 1e2ed070fdd..544b44cb053 100644 --- a/core/lib/Drupal/Core/DrupalKernelInterface.php +++ b/core/lib/Drupal/Core/DrupalKernelInterface.php @@ -38,13 +38,10 @@ interface DrupalKernelInterface extends HttpKernelInterface { /** * Returns all registered service providers. * - * @param string $origin - * The origin for which to return service providers; one of 'app' or 'site'. - * * @return array * An associative array of ServiceProvider objects, keyed by name. */ - public function getServiceProviders($origin); + public function getServiceProviders(); /** * Gets the current container. diff --git a/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelSiteTest.php b/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelSiteTest.php deleted file mode 100644 index d6f9fe8a16b..00000000000 --- a/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelSiteTest.php +++ /dev/null @@ -1,57 +0,0 @@ - 'DrupalKernel site service overrides', - 'description' => 'Tests site-specific service overrides.', - 'group' => 'DrupalKernel', - ); - } - - /** - * Tests services.yml in site directory. - */ - public function testServicesYml() { - $this->assertFalse($this->container->has('site.service.yml')); - // A service provider class always has precedence over services.yml files. - // DrupalUnitTestBase::buildContainer() swaps out many services with - // in-memory implementations already, so those cannot be tested. - $this->assertIdentical(get_class($this->container->get('cache.backend.database')), 'Drupal\Core\Cache\DatabaseBackendFactory'); - - $class = __CLASS__; - $doc = <<siteDirectory . '/services.yml', $doc); - - // Rebuild the container. - $this->kernel->updateModules(array()); - - $this->assertTrue($this->container->has('site.service.yml')); - $this->assertIdentical(get_class($this->container->get('cache.backend.database')), 'Drupal\Core\Cache\MemoryBackendFactory'); - } - -}