CoreBundle.php 22.4 KB
Newer Older
1 2
<?php

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

8 9
namespace Drupal\Core;

10 11
use Drupal\Core\Cache\CacheFactory;
use Drupal\Core\Cache\ListCacheBinsPass;
12
use Drupal\Core\DependencyInjection\Compiler\RegisterKernelListenersPass;
13
use Drupal\Core\DependencyInjection\Compiler\RegisterAccessChecksPass;
14
use Drupal\Core\DependencyInjection\Compiler\RegisterMatchersPass;
15
use Drupal\Core\DependencyInjection\Compiler\RegisterPathProcessorsPass;
16
use Drupal\Core\DependencyInjection\Compiler\RegisterRouteFiltersPass;
17 18
use Drupal\Core\DependencyInjection\Compiler\RegisterRouteEnhancersPass;
use Drupal\Core\DependencyInjection\Compiler\RegisterParamConvertersPass;
19
use Drupal\Core\DependencyInjection\Compiler\RegisterServicesForDestructionPass;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
use Symfony\Component\DependencyInjection\Reference;
23
use Symfony\Component\DependencyInjection\Definition;
24
use Symfony\Component\DependencyInjection\Scope;
25 26 27
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

28
/**
29 30
 * Bundle class for mandatory core services.
 *
31 32 33 34
 * This is where Drupal core registers all of its services to the Dependency
 * Injection Container. Modules wishing to register services to the container
 * should extend Symfony's Bundle class directly, not this class.
 */
35
class CoreBundle extends Bundle {
36

37 38 39
  /**
   * Implements \Symfony\Component\HttpKernel\Bundle\BundleInterface::build().
   */
40
  public function build(ContainerBuilder $container) {
41
    $this->registerCache($container);
42 43 44 45 46 47 48 49 50
    // Register active configuration storage.
    $container
      ->register('config.cachedstorage.storage', 'Drupal\Core\Config\FileStorage')
      ->addArgument(config_get_config_directory(CONFIG_ACTIVE_DIRECTORY));
    $container
      ->register('config.storage', 'Drupal\Core\Config\CachedStorage')
      ->addArgument(new Reference('config.cachedstorage.storage'))
      ->addArgument(new Reference('cache.config'));

51 52 53 54 55 56
    $container->register('config.context.factory', 'Drupal\Core\Config\Context\ConfigContextFactory')
      ->addArgument(new Reference('event_dispatcher'));

    $container->register('config.context', 'Drupal\Core\Config\Context\ContextInterface')
      ->setFactoryService(new Reference('config.context.factory'))
      ->setFactoryMethod('get')
57
      ->addTag('persist');
58 59 60 61 62

    // Register a config context with no overrides for use in administration
    // forms, enabling modules and importing configuration.
    $container->register('config.context.free', 'Drupal\Core\Config\Context\ContextInterface')
      ->setFactoryService(new Reference('config.context.factory'))
63 64
      ->setFactoryMethod('get')
      ->addArgument('Drupal\Core\Config\Context\FreeConfigContext');
65

66 67
    $container->register('config.factory', 'Drupal\Core\Config\ConfigFactory')
      ->addArgument(new Reference('config.storage'))
68
      ->addArgument(new Reference('config.context'))
69
      ->addTag('persist');
70 71 72 73 74 75

    // Register staging configuration storage.
    $container
      ->register('config.storage.staging', 'Drupal\Core\Config\FileStorage')
      ->addArgument(config_get_config_directory(CONFIG_STAGING_DIRECTORY));

76 77 78 79 80 81
    // Register import snapshot configuration storage.
    $container
      ->register('config.storage.snapshot', 'Drupal\Core\Config\DatabaseStorage')
      ->addArgument(new Reference('database'))
      ->addArgument('config_snapshot');

82 83 84 85
    // Register schema configuration storage.
    $container
      ->register('config.storage.schema', 'Drupal\Core\Config\Schema\SchemaStorage');

86 87
    // Register the typed configuration data manager.
    $container->register('config.typed', 'Drupal\Core\Config\TypedConfigManager')
88 89
      ->addArgument(new Reference('config.storage'))
      ->addArgument(new Reference('config.storage.schema'));
90

91 92 93 94 95 96 97 98 99 100 101 102
    // Register the service for the default database connection.
    $container->register('database', 'Drupal\Core\Database\Connection')
      ->setFactoryClass('Drupal\Core\Database\Database')
      ->setFactoryMethod('getConnection')
      ->addArgument('default');
    // Register the KeyValueStore factory.
    $container
      ->register('keyvalue', 'Drupal\Core\KeyValueStore\KeyValueFactory')
      ->addArgument(new Reference('service_container'));
    $container
      ->register('keyvalue.database', 'Drupal\Core\KeyValueStore\KeyValueDatabaseFactory')
      ->addArgument(new Reference('database'));
103 104 105 106 107 108 109 110
    // Register the KeyValueStoreExpirable factory.
    $container
      ->register('keyvalue.expirable', 'Drupal\Core\KeyValueStore\KeyValueExpirableFactory')
      ->addArgument(new Reference('service_container'));
    $container
      ->register('keyvalue.expirable.database', 'Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory')
      ->addArgument(new Reference('database'))
      ->addTag('needs_destruction');
111

112 113 114 115
    $container->register('settings', 'Drupal\Component\Utility\Settings')
      ->setFactoryClass('Drupal\Component\Utility\Settings')
      ->setFactoryMethod('getSingleton');

116 117 118 119 120 121
    // Register the State k/v store as a service.
    $container->register('state', 'Drupal\Core\KeyValueStore\KeyValueStoreInterface')
      ->setFactoryService(new Reference('keyvalue'))
      ->setFactoryMethod('get')
      ->addArgument('state');

122 123 124 125 126 127 128 129 130
    // Register the Queue factory.
    $container
      ->register('queue', 'Drupal\Core\Queue\QueueFactory')
      ->addArgument(new Reference('settings'))
      ->addMethodCall('setContainer', array(new Reference('service_container')));
    $container
      ->register('queue.database', 'Drupal\Core\Queue\QueueDatabaseFactory')
      ->addArgument(new Reference('database'));

131 132 133 134
    $container->register('path.alias_whitelist', 'Drupal\Core\Path\AliasWhitelist')
      ->addArgument('path_alias_whitelist')
      ->addArgument('cache')
      ->addArgument(new Reference('keyvalue'))
135
      ->addArgument(new Reference('database'))
136 137 138 139 140
      ->addTag('needs_destruction');

     $container->register('path.alias_manager', 'Drupal\Core\Path\AliasManager')
      ->addArgument(new Reference('database'))
      ->addArgument(new Reference('path.alias_whitelist'))
141
      ->addArgument(new Reference('language_manager'));
142

143 144 145 146 147 148 149 150 151 152
    $container->register('http_client_simpletest_subscriber', 'Drupal\Core\Http\Plugin\SimpletestHttpRequestSubscriber');
    $container->register('http_default_client', 'Guzzle\Http\Client')
      ->addArgument(NULL)
      ->addArgument(array(
        'curl.CURLOPT_TIMEOUT' => 30.0,
        'curl.CURLOPT_MAXREDIRS' => 3,
      ))
      ->addMethodCall('addSubscriber', array(new Reference('http_client_simpletest_subscriber')))
      ->addMethodCall('setUserAgent', array('Drupal (+http://drupal.org/)'));

153
    // Register the EntityManager.
154 155
    $container->register('plugin.manager.entity', 'Drupal\Core\Entity\EntityManager')
      ->addArgument('%container.namespaces%');
156

157 158 159
    // 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).
160
    $container->addScope(new Scope('request'));
161
    $container->register('request', 'Symfony\Component\HttpFoundation\Request');
162

163
    $container->register('event_dispatcher', 'Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher')
164
      ->addArgument(new Reference('service_container'));
165
    $container->register('controller_resolver', 'Drupal\Core\ControllerResolver')
166
      ->addArgument(new Reference('service_container'));
167 168 169

    $this->registerModuleHandler($container);

170
    $container->register('http_kernel', 'Drupal\Core\HttpKernel')
171
      ->addArgument(new Reference('event_dispatcher'))
172
      ->addArgument(new Reference('service_container'))
173
      ->addArgument(new Reference('controller_resolver'));
174 175 176 177

    // Register the 'language_manager' service.
    $container->register('language_manager', 'Drupal\Core\Language\LanguageManager');

178 179 180 181
    $container->register('database.slave', 'Drupal\Core\Database\Connection')
      ->setFactoryClass('Drupal\Core\Database\Database')
      ->setFactoryMethod('getConnection')
      ->addArgument('slave');
182 183
    $container->register('typed_data', 'Drupal\Core\TypedData\TypedDataManager')
      ->addMethodCall('setValidationConstraintManager', array(new Reference('validation.constraint')));
184 185
    $container->register('validation.constraint', 'Drupal\Core\Validation\ConstraintManager')
      ->addArgument('%container.namespaces%');
186

187 188 189 190 191
    // Add the user's storage for temporary, non-cache data.
    $container->register('lock', 'Drupal\Core\Lock\DatabaseLockBackend');
    $container->register('user.tempstore', 'Drupal\user\TempStoreFactory')
      ->addArgument(new Reference('database'))
      ->addArgument(new Reference('lock'));
192 193

    $this->registerTwig($container);
194
    $this->registerRouting($container);
195

196
    // Add the entity query factories.
197
    $container->register('entity.query', 'Drupal\Core\Entity\Query\QueryFactory')
198 199 200 201
      ->addArgument(new Reference('plugin.manager.entity'))
      ->addMethodCall('setContainer', array(new Reference('service_container')));
    $container->register('entity.query.config', 'Drupal\Core\Config\Entity\Query\QueryFactory')
      ->addArgument(new Reference('config.storage'));
202

203
    $container->register('router.dumper', 'Drupal\Core\Routing\MatcherDumper')
204
      ->addArgument(new Reference('database'));
205
    $container->register('router.builder', 'Drupal\Core\Routing\RouteBuilder')
206
      ->addArgument(new Reference('router.dumper'))
207
      ->addArgument(new Reference('lock'))
208 209
      ->addArgument(new Reference('event_dispatcher'))
      ->addArgument(new Reference('module_handler'));
210

211 212 213 214 215 216 217 218
    $container->register('path.alias_manager.cached', 'Drupal\Core\CacheDecorator\AliasManagerCacheDecorator')
      ->addArgument(new Reference('path.alias_manager'))
      ->addArgument(new Reference('cache.path'));

    $container->register('path.crud', 'Drupal\Core\Path\Path')
      ->addArgument(new Reference('database'))
      ->addArgument(new Reference('path.alias_manager'));

219 220 221 222 223 224 225 226 227
    // Add password hashing service. The argument to PhpassHashedPassword
    // constructor is the log2 number of iterations for password stretching.
    // This should increase by 1 every Drupal version in order to counteract
    // increases in the speed and power of computers available to crack the
    // hashes. The current password hashing method was introduced in Drupal 7
    // with a log2 count of 15.
    $container->register('password', 'Drupal\Core\Password\PhpassHashedPassword')
      ->addArgument(16);

228 229
    // The following services are tagged as 'route_filter' services and are
    // processed in the RegisterRouteFiltersPass compiler pass.
230
    $container->register('mime_type_matcher', 'Drupal\Core\Routing\MimeTypeMatcher')
231
      ->addTag('route_filter');
232

233 234 235 236 237 238
    $container->register('paramconverter_manager', 'Drupal\Core\ParamConverter\ParamConverterManager')
      ->addTag('route_enhancer');
    $container->register('paramconverter.entity', 'Drupal\Core\ParamConverter\EntityConverter')
      ->addArgument(new Reference('plugin.manager.entity'))
      ->addTag('paramconverter');

239
    $container->register('router_processor_subscriber', 'Drupal\Core\EventSubscriber\RouteProcessorSubscriber')
240
      ->addArgument(new Reference('content_negotiation'))
241
      ->addTag('event_subscriber');
242
    $container->register('router_listener', 'Symfony\Component\HttpKernel\EventListener\RouterListener')
243
      ->addArgument(new Reference('router'))
244
      ->addTag('event_subscriber');
245 246 247
    $container->register('content_negotiation', 'Drupal\Core\ContentNegotiation');
    $container->register('view_subscriber', 'Drupal\Core\EventSubscriber\ViewSubscriber')
      ->addArgument(new Reference('content_negotiation'))
248
      ->addTag('event_subscriber');
249 250 251 252
    $container->register('legacy_access_subscriber', 'Drupal\Core\EventSubscriber\LegacyAccessSubscriber')
      ->addTag('event_subscriber');
    $container->register('access_manager', 'Drupal\Core\Access\AccessManager')
      ->addMethodCall('setContainer', array(new Reference('service_container')));
253
    $container->register('access_subscriber', 'Drupal\Core\EventSubscriber\AccessSubscriber')
254
      ->addArgument(new Reference('access_manager'))
255
      ->addTag('event_subscriber');
256 257
    $container->register('access_check.default', 'Drupal\Core\Access\DefaultAccessCheck')
      ->addTag('access_check');
258
    $container->register('maintenance_mode_subscriber', 'Drupal\Core\EventSubscriber\MaintenanceModeSubscriber')
259
      ->addTag('event_subscriber');
260
    $container->register('path_subscriber', 'Drupal\Core\EventSubscriber\PathSubscriber')
261
      ->addArgument(new Reference('path.alias_manager.cached'))
262
      ->addArgument(new Reference('path_processor_manager'))
263
      ->addTag('event_subscriber');
264
    $container->register('legacy_request_subscriber', 'Drupal\Core\EventSubscriber\LegacyRequestSubscriber')
265
      ->addTag('event_subscriber');
266
    $container->register('legacy_controller_subscriber', 'Drupal\Core\EventSubscriber\LegacyControllerSubscriber')
267
      ->addTag('event_subscriber');
268
    $container->register('finish_response_subscriber', 'Drupal\Core\EventSubscriber\FinishResponseSubscriber')
269 270
      ->addArgument(new Reference('language_manager'))
      ->setScope('request')
271
      ->addTag('event_subscriber');
272
    $container->register('request_close_subscriber', 'Drupal\Core\EventSubscriber\RequestCloseSubscriber')
273
      ->addArgument(new Reference('module_handler'))
274
      ->addTag('event_subscriber');
275
    $container->register('config_global_override_subscriber', 'Drupal\Core\EventSubscriber\ConfigGlobalOverrideSubscriber')
276
      ->addTag('event_subscriber');
277 278 279
    $container->register('language_request_subscriber', 'Drupal\Core\EventSubscriber\LanguageRequestSubscriber')
      ->addArgument(new Reference('language_manager'))
      ->addTag('event_subscriber');
280 281 282 283

    $container->register('exception_controller', 'Drupal\Core\ExceptionController')
      ->addArgument(new Reference('content_negotiation'))
      ->addMethodCall('setContainer', array(new Reference('service_container')));
284
    $container->register('exception_listener', 'Symfony\Component\HttpKernel\EventListener\ExceptionListener')
285
      ->addTag('event_subscriber')
286
      ->addArgument(array(new Reference('exception_controller'), 'execute'));
287

288 289
    $this->registerPathProcessors($container);

290 291
    $container
      ->register('transliteration', 'Drupal\Core\Transliteration\PHPTransliteration');
292

293 294 295
    $container->register('flood', 'Drupal\Core\Flood\DatabaseBackend')
      ->addArgument(new Reference('database'));

296 297
    $container->register('plugin.manager.condition', 'Drupal\Core\Condition\ConditionManager');

298
    $container->register('kernel_destruct_subscriber', 'Drupal\Core\EventSubscriber\KernelDestructionSubscriber')
299 300
      ->addMethodCall('setContainer', array(new Reference('service_container')))
      ->addTag('event_subscriber');
301 302 303

    // Register Ajax event subscriber.
    $container->register('ajax.subscriber', 'Drupal\Core\Ajax\AjaxSubscriber')
304 305
      ->addTag('event_subscriber');

306 307 308 309 310 311 312 313 314 315
    // Register image toolkit manager.
    $container
      ->register('image.toolkit.manager', 'Drupal\system\Plugin\ImageToolkitManager')
      ->addArgument('%container.namespaces%');
    // Register image toolkit.
    $container
      ->register('image.toolkit', 'Drupal\system\Plugin\ImageToolkitInterface')
      ->setFactoryService('image.toolkit.manager')
      ->setFactoryMethod('getDefaultToolkit');

316
    $container->addCompilerPass(new RegisterMatchersPass());
317 318 319
    $container->addCompilerPass(new RegisterRouteFiltersPass());
    // Add a compiler pass for registering event subscribers.
    $container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING);
320 321 322
    // Add a compiler pass for registering event subscribers.
    $container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING);
    $container->addCompilerPass(new RegisterAccessChecksPass());
323 324 325
    // Add a compiler pass for upcasting of entity route parameters.
    $container->addCompilerPass(new RegisterParamConvertersPass());
    $container->addCompilerPass(new RegisterRouteEnhancersPass());
326 327
    // Add a compiler pass for registering services needing destruction.
    $container->addCompilerPass(new RegisterServicesForDestructionPass());
328
  }
329

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  /**
   * Registers the module handler.
   */
  protected function registerModuleHandler(ContainerBuilder $container) {
    // The ModuleHandler manages enabled modules and provides the ability to
    // invoke hooks in all enabled modules.
    if ($container->getParameter('kernel.environment') == 'install') {
      // During installation we use the non-cached version.
      $container->register('module_handler', 'Drupal\Core\Extension\ModuleHandler')
        ->addArgument('%container.modules%');
    }
    else {
      $container->register('module_handler', 'Drupal\Core\Extension\CachedModuleHandler')
        ->addArgument('%container.modules%')
        ->addArgument(new Reference('state'))
        ->addArgument(new Reference('cache.bootstrap'));
    }
  }

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  /**
   * Registers the various services for the routing system.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
   */
  protected function registerRouting(ContainerBuilder $container) {
    $container->register('router.request_context', 'Symfony\Component\Routing\RequestContext')
      ->addMethodCall('fromRequest', array(new Reference('request')));

    $container->register('router.route_provider', 'Drupal\Core\Routing\RouteProvider')
      ->addArgument(new Reference('database'));
    $container->register('router.matcher.final_matcher', 'Drupal\Core\Routing\UrlMatcher');
    $container->register('router.matcher', 'Symfony\Cmf\Component\Routing\NestedMatcher\NestedMatcher')
      ->addArgument(new Reference('router.route_provider'))
      ->addMethodCall('setFinalMatcher', array(new Reference('router.matcher.final_matcher')));
    $container->register('router.generator', 'Drupal\Core\Routing\UrlGenerator')
      ->addArgument(new Reference('router.route_provider'))
      ->addArgument(new Reference('path.alias_manager.cached'));
    $container->register('router.dynamic', 'Symfony\Cmf\Component\Routing\DynamicRouter')
      ->addArgument(new Reference('router.request_context'))
      ->addArgument(new Reference('router.matcher'))
      ->addArgument(new Reference('router.generator'));

    $container->register('legacy_generator', 'Drupal\Core\Routing\NullGenerator');
    $container->register('legacy_url_matcher', 'Drupal\Core\LegacyUrlMatcher');
    $container->register('legacy_router', 'Symfony\Cmf\Component\Routing\DynamicRouter')
            ->addArgument(new Reference('router.request_context'))
            ->addArgument(new Reference('legacy_url_matcher'))
            ->addArgument(new Reference('legacy_generator'));

    $container->register('router', 'Symfony\Cmf\Component\Routing\ChainRouter')
      ->addMethodCall('setContext', array(new Reference('router.request_context')))
      ->addMethodCall('add', array(new Reference('router.dynamic')))
      ->addMethodCall('add', array(new Reference('legacy_router')));
  }

385 386 387 388 389 390 391 392 393 394 395 396 397 398
  /**
   * Registers Twig services.
   */
  protected function registerTwig(ContainerBuilder $container) {
    $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.
399
        'cache' => settings()->get('twig_cache', TRUE),
400 401 402 403 404 405 406
        'base_template_class' => 'Drupal\Core\Template\TwigTemplate',
        // @todo Remove in followup issue
        // @see http://drupal.org/node/1712444.
        'autoescape' => FALSE,
        // @todo Remove in followup issue
        // @see http://drupal.org/node/1806538.
        'strict_variables' => FALSE,
407 408
        'debug' => settings()->get('twig_debug', FALSE),
        'auto_reload' => settings()->get('twig_auto_reload', NULL),
409 410 411 412 413 414
      ))
      ->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')));
  }
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

  /**
   * Register services related to path processing.
   */
  protected function registerPathProcessors(ContainerBuilder $container) {
    // Register the path processor manager service.
    $container->register('path_processor_manager', 'Drupal\Core\PathProcessor\PathProcessorManager');
    // Register the processor that urldecodes the path.
    $container->register('path_processor_decode', 'Drupal\Core\PathProcessor\PathProcessorDecode')
      ->addTag('path_processor_inbound', array('priority' => 1000));
    // Register the processor that resolves the front page.
    $container->register('path_processor_front', 'Drupal\Core\PathProcessor\PathProcessorFront')
      ->addArgument(new Reference('config.factory'))
      ->addTag('path_processor_inbound', array('priority' => 200));
    // Register the alias path processor.
    $container->register('path_processor_alias', 'Drupal\Core\PathProcessor\PathProcessorAlias')
      ->addArgument(new Reference('path.alias_manager'))
      ->addTag('path_processor_inbound', array('priority' => 100));

    // Add the compiler pass that will process the tagged services.
    $container->addCompilerPass(new RegisterPathProcessorsPass());
  }

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
  /**
   * Register services related to cache.
   */
  protected function registerCache(ContainerBuilder $container) {
    // This factory chooses the backend service for a given bin.
    $container
      ->register('cache_factory', 'Drupal\Core\Cache\CacheFactory')
      ->addArgument(new Reference('settings'))
      ->addMethodCall('setContainer', array(new Reference('service_container')));
    // These are the core provided backend services.
    $container
      ->register('cache.backend.database', 'Drupal\Core\Cache\DatabaseBackendFactory')
      ->addArgument(new Reference('database'));
    $container
      ->register('cache.backend.memory', 'Drupal\Core\Cache\MemoryBackendFactory');
    // Register a service for each bin for injecting purposes.
    foreach (array('bootstrap', 'config', 'cache', 'form', 'menu', 'page', 'path') as $bin) {
      CacheFactory::registerBin($container, $bin);
    }

    $container->addCompilerPass(new ListCacheBinsPass());
  }

katbailey's avatar
katbailey committed
461
}