Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / CoreServiceProvider.php
1 <?php
2
3 namespace Drupal\Core;
4
5 use Drupal\Core\Cache\Context\CacheContextsPass;
6 use Drupal\Core\Cache\ListCacheBinsPass;
7 use Drupal\Core\DependencyInjection\Compiler\AuthenticationProviderPass;
8 use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
9 use Drupal\Core\DependencyInjection\Compiler\CorsCompilerPass;
10 use Drupal\Core\DependencyInjection\Compiler\GuzzleMiddlewarePass;
11 use Drupal\Core\DependencyInjection\Compiler\ContextProvidersPass;
12 use Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass;
13 use Drupal\Core\DependencyInjection\Compiler\DependencySerializationTraitPass;
14 use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass;
15 use Drupal\Core\DependencyInjection\Compiler\StackedSessionHandlerPass;
16 use Drupal\Core\DependencyInjection\Compiler\RegisterStreamWrappersPass;
17 use Drupal\Core\DependencyInjection\Compiler\TwigExtensionPass;
18 use Drupal\Core\DependencyInjection\ServiceModifierInterface;
19 use Drupal\Core\DependencyInjection\ServiceProviderInterface;
20 use Drupal\Core\DependencyInjection\ContainerBuilder;
21 use Drupal\Core\DependencyInjection\Compiler\ModifyServiceDefinitionsPass;
22 use Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass;
23 use Drupal\Core\DependencyInjection\Compiler\RegisterEventSubscribersPass;
24 use Drupal\Core\DependencyInjection\Compiler\RegisterAccessChecksPass;
25 use Drupal\Core\DependencyInjection\Compiler\RegisterServicesForDestructionPass;
26 use Drupal\Core\Plugin\PluginManagerPass;
27 use Drupal\Core\Render\MainContent\MainContentRenderersPass;
28 use Drupal\Core\Site\Settings;
29 use Symfony\Component\DependencyInjection\Compiler\PassConfig;
30
31 /**
32  * ServiceProvider class for mandatory core services.
33  *
34  * This is where Drupal core registers all of its compiler passes.
35  * The service definitions themselves are in core/core.services.yml with a
36  * few, documented exceptions (typically, install requirements).
37  *
38  * Modules wishing to register services to the container should use
39  * modulename.services.yml in their respective directories.
40  *
41  * @ingroup container
42  */
43 class CoreServiceProvider implements ServiceProviderInterface, ServiceModifierInterface {
44
45   /**
46    * {@inheritdoc}
47    */
48   public function register(ContainerBuilder $container) {
49     $this->registerTest($container);
50
51     // Only register the private file stream wrapper if a file path has been set.
52     if (Settings::get('file_private_path')) {
53       $container->register('stream_wrapper.private', 'Drupal\Core\StreamWrapper\PrivateStream')
54         ->addTag('stream_wrapper', ['scheme' => 'private']);
55     }
56
57     // Add the compiler pass that lets service providers modify existing
58     // service definitions. This pass must come first so that later
59     // list-building passes are operating on the post-alter services list.
60     $container->addCompilerPass(new ModifyServiceDefinitionsPass());
61
62     $container->addCompilerPass(new ProxyServicesPass());
63
64     $container->addCompilerPass(new BackendCompilerPass());
65
66     $container->addCompilerPass(new CorsCompilerPass());
67
68     $container->addCompilerPass(new StackedKernelPass());
69
70     $container->addCompilerPass(new StackedSessionHandlerPass());
71
72     $container->addCompilerPass(new MainContentRenderersPass());
73
74     // Collect tagged handler services as method calls on consumer services.
75     $container->addCompilerPass(new TaggedHandlersPass());
76     $container->addCompilerPass(new RegisterStreamWrappersPass());
77     $container->addCompilerPass(new GuzzleMiddlewarePass());
78
79     $container->addCompilerPass(new TwigExtensionPass());
80
81     // Add a compiler pass for registering event subscribers.
82     $container->addCompilerPass(new RegisterEventSubscribersPass(), PassConfig::TYPE_AFTER_REMOVING);
83
84     $container->addCompilerPass(new RegisterAccessChecksPass());
85
86     // Add a compiler pass for registering services needing destruction.
87     $container->addCompilerPass(new RegisterServicesForDestructionPass());
88
89     // Add the compiler pass that will process the tagged services.
90     $container->addCompilerPass(new ListCacheBinsPass());
91     $container->addCompilerPass(new CacheContextsPass());
92     $container->addCompilerPass(new ContextProvidersPass());
93     $container->addCompilerPass(new AuthenticationProviderPass());
94
95     // Register plugin managers.
96     $container->addCompilerPass(new PluginManagerPass());
97
98     $container->addCompilerPass(new DependencySerializationTraitPass());
99   }
100
101   /**
102    * Alters the UUID service to use the most efficient method available.
103    *
104    * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
105    *   The container builder.
106    */
107   public function alter(ContainerBuilder $container) {
108     $uuid_service = $container->getDefinition('uuid');
109     // Debian/Ubuntu uses the (broken) OSSP extension as their UUID
110     // implementation. The OSSP implementation is not compatible with the
111     // PECL functions.
112     if (function_exists('uuid_create') && !function_exists('uuid_make')) {
113       $uuid_service->setClass('Drupal\Component\Uuid\Pecl');
114     }
115     // Try to use the COM implementation for Windows users.
116     elseif (function_exists('com_create_guid')) {
117       $uuid_service->setClass('Drupal\Component\Uuid\Com');
118     }
119   }
120
121   /**
122    * Registers services and event subscribers for a site under test.
123    *
124    * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
125    *   The container builder.
126    */
127   protected function registerTest(ContainerBuilder $container) {
128     // Do nothing if we are not in a test environment.
129     if (!drupal_valid_test_ua()) {
130       return;
131     }
132     // Add the HTTP request middleware to Guzzle.
133     $container
134       ->register('test.http_client.middleware', 'Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware')
135       ->addTag('http_client_middleware');
136   }
137
138 }