CoreServiceProvider.php 5.6 KB

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