CoreServiceProvider.php 7.06 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\Component\Utility\Settings;
12
use Drupal\Core\Cache\ListCacheBinsPass;
13
use Drupal\Core\Config\ConfigFactoryOverridePass;
14
15
16
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\Compiler\ModifyServiceDefinitionsPass;
17
use Drupal\Core\DependencyInjection\Compiler\RegisterKernelListenersPass;
18
use Drupal\Core\DependencyInjection\Compiler\RegisterAccessChecksPass;
19
use Drupal\Core\DependencyInjection\Compiler\RegisterPathProcessorsPass;
20
use Drupal\Core\DependencyInjection\Compiler\RegisterRouteProcessorsPass;
21
use Drupal\Core\DependencyInjection\Compiler\RegisterRouteFiltersPass;
22
23
use Drupal\Core\DependencyInjection\Compiler\RegisterRouteEnhancersPass;
use Drupal\Core\DependencyInjection\Compiler\RegisterParamConvertersPass;
24
use Drupal\Core\DependencyInjection\Compiler\RegisterServicesForDestructionPass;
25
use Drupal\Core\DependencyInjection\Compiler\RegisterStringTranslatorsPass;
26
use Drupal\Core\DependencyInjection\Compiler\RegisterBreadcrumbBuilderPass;
27
use Drupal\Core\DependencyInjection\Compiler\RegisterAuthenticationPass;
28
use Drupal\Core\DependencyInjection\Compiler\RegisterTwigExtensionsPass;
29
use Drupal\Core\Plugin\PluginManagerPass;
30
use Drupal\Core\Theme\ThemeNegotiatorPass;
31
use Symfony\Component\DependencyInjection\ContainerInterface;
32
use Symfony\Component\DependencyInjection\Reference;
33
use Symfony\Component\DependencyInjection\Definition;
34
use Symfony\Component\DependencyInjection\Scope;
35
36
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

37
/**
38
 * ServiceProvider class for mandatory core services.
Crell's avatar
Crell committed
39
 *
40
41
42
43
44
45
 * 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.
46
 */
47
class CoreServiceProvider implements ServiceProviderInterface  {
48

49
  /**
50
   * {@inheritdoc}
51
   */
52
  public function register(ContainerBuilder $container) {
53
54
55
    // The 'request' scope and service enable services to depend on the Request
    // object and get reconstructed when the request object changes (e.g.,
    // during a subrequest).
56
    $container->addScope(new Scope('request'));
57
    $this->registerTwig($container);
58
    $this->registerUuid($container);
59

60
61
62
63
    // 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());
64
65
    $container->addCompilerPass(new RegisterRouteFiltersPass());
    // Add a compiler pass for registering event subscribers.
66
67
    $container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING);
    $container->addCompilerPass(new RegisterAccessChecksPass());
68
    // Add a compiler pass for upcasting route parameters.
69
    $container->addCompilerPass(new RegisterParamConvertersPass());
70
    $container->addCompilerPass(new RegisterRouteEnhancersPass());
71
72
    // Add a compiler pass for registering services needing destruction.
    $container->addCompilerPass(new RegisterServicesForDestructionPass());
73
74
    // Add the compiler pass that will process the tagged services.
    $container->addCompilerPass(new RegisterPathProcessorsPass());
75
    $container->addCompilerPass(new RegisterRouteProcessorsPass());
76
    $container->addCompilerPass(new ListCacheBinsPass());
77
    $container->addCompilerPass(new CacheContextsPass());
78
79
    // Add the compiler pass for appending string translators.
    $container->addCompilerPass(new RegisterStringTranslatorsPass());
80
81
82
    // Add the compiler pass that will process the tagged breadcrumb builder
    // services.
    $container->addCompilerPass(new RegisterBreadcrumbBuilderPass());
83
84
85
    // Add the compiler pass that will process the tagged theme negotiator
    // service.
    $container->addCompilerPass(new ThemeNegotiatorPass());
86
87
88
    // Add the compiler pass that will process the tagged config factory
    // override services.
    $container->addCompilerPass(new ConfigFactoryOverridePass());
89
90
    // Add the compiler pass that will process tagged authentication services.
    $container->addCompilerPass(new RegisterAuthenticationPass());
91
92
    // Register Twig extensions.
    $container->addCompilerPass(new RegisterTwigExtensionsPass());
93
94
    // Register plugin managers.
    $container->addCompilerPass(new PluginManagerPass());
95
  }
96

97
98
  /**
   * Registers Twig services.
99
   *
100
   * This method is public and static so that it can be reused in the installer.
101
   */
102
  public static function registerTwig(ContainerBuilder $container) {
103
104
105
106
107
108
109
110
111
112
    $container->register('twig.loader.filesystem', 'Twig_Loader_Filesystem')
      ->addArgument(DRUPAL_ROOT);
    $container->setAlias('twig.loader', 'twig.loader.filesystem');

    $container->register('twig', 'Drupal\Core\Template\TwigEnvironment')
      ->addArgument(new Reference('twig.loader'))
      ->addArgument(array(
        // This is saved / loaded via drupal_php_storage().
        // All files can be refreshed by clearing caches.
        // @todo ensure garbage collection of expired files.
113
114
        // When in the installer, twig_cache must be FALSE until we know the
        // files folder is writable.
115
        'cache' => drupal_installation_attempted() ? FALSE : Settings::get('twig_cache', TRUE),
116
117
118
        // @todo Remove in followup issue
        // @see http://drupal.org/node/1712444.
        'autoescape' => FALSE,
119
120
        'debug' => Settings::get('twig_debug', FALSE),
        'auto_reload' => Settings::get('twig_auto_reload', NULL),
121
      ))
122
123
      ->addArgument(new Reference('module_handler'))
      ->addArgument(new Reference('theme_handler'))
124
125
126
127
128
      ->addMethodCall('addExtension', array(new Definition('Drupal\Core\Template\TwigExtension')))
      // @todo Figure out what to do about debugging functions.
      // @see http://drupal.org/node/1804998
      ->addMethodCall('addExtension', array(new Definition('Twig_Extension_Debug')));
  }
129

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  /**
   * 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;
  }

katbailey's avatar
katbailey committed
157
}