CoreServiceProvider.php 5.17 KB
Newer Older
1 2
<?php

3 4
/**
 * @file
5
 * Definition of Drupal\Core\CoreServiceProvider.
6 7
 */

8 9
namespace Drupal\Core;

10
use Drupal\Core\Cache\Context\CacheContextsPass;
11
use Drupal\Core\Cache\ListCacheBinsPass;
12
use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
13 14
use Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteEnhancers;
use Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteFilters;
15
use Drupal\Core\DependencyInjection\Compiler\DependencySerializationTraitPass;
16
use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass;
17
use Drupal\Core\DependencyInjection\Compiler\StackedSessionHandlerPass;
18
use Drupal\Core\DependencyInjection\Compiler\RegisterStreamWrappersPass;
19 20 21
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\Compiler\ModifyServiceDefinitionsPass;
22
use Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass;
23
use Drupal\Core\DependencyInjection\Compiler\RegisterKernelListenersPass;
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 30
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

31
/**
32
 * ServiceProvider class for mandatory core services.
33
 *
34 35 36 37 38 39
 * This is where Drupal core registers all of its compiler passes.
 * The service definitions themselves are in core/core.services.yml with a
 * few, documented exceptions (typically, install requirements).
 *
 * Modules wishing to register services to the container should use
 * modulename.services.yml in their respective directories.
40 41
 *
 * @ingroup container
42
 */
43
class CoreServiceProvider implements ServiceProviderInterface  {
44

45
  /**
46
   * {@inheritdoc}
47
   */
48
  public function register(ContainerBuilder $container) {
49
    $this->registerUuid($container);
50
    $this->registerTest($container);
51

52 53 54 55 56 57
    // Only register the private file stream wrapper if a file path has been set.
    if (Settings::get('file_private_path')) {
      $container->register('stream_wrapper.private', 'Drupal\Core\StreamWrapper\PrivateStream')
        ->addTag('stream_wrapper', ['scheme' => 'private']);
    }

58 59 60 61
    // Add the compiler pass that lets service providers modify existing
    // service definitions. This pass must come first so that later
    // list-building passes are operating on the post-alter services list.
    $container->addCompilerPass(new ModifyServiceDefinitionsPass());
62

63 64
    $container->addCompilerPass(new BackendCompilerPass());

65 66
    $container->addCompilerPass(new StackedKernelPass());

67 68
    $container->addCompilerPass(new StackedSessionHandlerPass());

69 70
    $container->addCompilerPass(new MainContentRenderersPass());

71 72
    // Collect tagged handler services as method calls on consumer services.
    $container->addCompilerPass(new TaggedHandlersPass());
73
    $container->addCompilerPass(new RegisterStreamWrappersPass());
74

75
    // Add a compiler pass for registering event subscribers.
76
    $container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING);
77

78
    $container->addCompilerPass(new RegisterAccessChecksPass());
79 80
    $container->addCompilerPass(new RegisterLazyRouteEnhancers());
    $container->addCompilerPass(new RegisterLazyRouteFilters());
81

82 83
    // Add a compiler pass for registering services needing destruction.
    $container->addCompilerPass(new RegisterServicesForDestructionPass());
84

85 86
    // Add the compiler pass that will process the tagged services.
    $container->addCompilerPass(new ListCacheBinsPass());
87
    $container->addCompilerPass(new CacheContextsPass());
88

89 90
    // Register plugin managers.
    $container->addCompilerPass(new PluginManagerPass());
91 92

    $container->addCompilerPass(new DependencySerializationTraitPass());
93
  }
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  /**
   * Determines and registers the UUID service.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
   *   The container.
   *
   * @return string
   *   Class name for the UUID service.
   */
  public static function registerUuid(ContainerBuilder $container) {
    $uuid_class = 'Drupal\Component\Uuid\Php';

    // Debian/Ubuntu uses the (broken) OSSP extension as their UUID
    // implementation. The OSSP implementation is not compatible with the
    // PECL functions.
    if (function_exists('uuid_create') && !function_exists('uuid_make')) {
      $uuid_class = 'Drupal\Component\Uuid\Pecl';
    }
    // Try to use the COM implementation for Windows users.
    elseif (function_exists('com_create_guid')) {
      $uuid_class = 'Drupal\Component\Uuid\Com';
    }

    $container->register('uuid', $uuid_class);
    return $uuid_class;
  }

122 123 124 125 126 127 128 129 130 131 132 133 134 135
  /**
   * Registers services and event subscribers for a site under test.
   */
  protected function registerTest(ContainerBuilder $container) {
    // Do nothing if we are not in a test environment.
    if (!drupal_valid_test_ua()) {
      return;
    }
    // Add the HTTP request subscriber to Guzzle.
    $container
      ->register('test.http_client.request_subscriber', 'Drupal\Core\Test\EventSubscriber\HttpRequestSubscriber')
      ->addTag('http_client_subscriber');
  }

katbailey's avatar
katbailey committed
136
}