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

Crell's avatar
Crell committed
3
4
/**
 * @file
5
 * Definition of Drupal\Core\CoreServiceProvider.
Crell's avatar
Crell committed
6
7
 */

8
9
namespace Drupal\Core;

10
use Drupal\Core\Cache\CacheContextsPass;
11
use Drupal\Core\Cache\ListCacheBinsPass;
12
use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
13
use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass;
14
use Drupal\Core\DependencyInjection\Compiler\RegisterStreamWrappersPass;
15
16
17
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\Compiler\ModifyServiceDefinitionsPass;
18
use Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass;
19
use Drupal\Core\DependencyInjection\Compiler\RegisterKernelListenersPass;
20
use Drupal\Core\DependencyInjection\Compiler\RegisterAccessChecksPass;
21
use Drupal\Core\DependencyInjection\Compiler\RegisterServicesForDestructionPass;
22
use Drupal\Core\Plugin\PluginManagerPass;
23
use Drupal\Core\Render\MainContent\MainContentRenderersPass;
24
25
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

26
/**
27
 * ServiceProvider class for mandatory core services.
Crell's avatar
Crell committed
28
 *
29
30
31
32
33
34
 * 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.
35
36
 *
 * @ingroup container
37
 */
38
class CoreServiceProvider implements ServiceProviderInterface  {
39

40
  /**
41
   * {@inheritdoc}
42
   */
43
  public function register(ContainerBuilder $container) {
44
    $this->registerUuid($container);
45
    $this->registerTest($container);
46

47
48
49
50
    // 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());
51

52
53
    $container->addCompilerPass(new BackendCompilerPass());

54
55
    $container->addCompilerPass(new StackedKernelPass());

56
57
    $container->addCompilerPass(new MainContentRenderersPass());

58
59
    // Collect tagged handler services as method calls on consumer services.
    $container->addCompilerPass(new TaggedHandlersPass());
60
    $container->addCompilerPass(new RegisterStreamWrappersPass());
61

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

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

67
68
    // Add a compiler pass for registering services needing destruction.
    $container->addCompilerPass(new RegisterServicesForDestructionPass());
69

70
71
    // Add the compiler pass that will process the tagged services.
    $container->addCompilerPass(new ListCacheBinsPass());
72
    $container->addCompilerPass(new CacheContextsPass());
73

74
75
    // Register plugin managers.
    $container->addCompilerPass(new PluginManagerPass());
76
  }
77

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  /**
   * 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;
  }

105
106
107
108
109
110
111
112
113
114
115
116
117
118
  /**
   * 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
119
}