CoreServiceProvider.php 5.96 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\ListCacheBinsPass;
11 12 13
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\Compiler\ModifyServiceDefinitionsPass;
14
use Drupal\Core\DependencyInjection\Compiler\RegisterKernelListenersPass;
15
use Drupal\Core\DependencyInjection\Compiler\RegisterAccessChecksPass;
16
use Drupal\Core\DependencyInjection\Compiler\RegisterMatchersPass;
17
use Drupal\Core\DependencyInjection\Compiler\RegisterPathProcessorsPass;
18
use Drupal\Core\DependencyInjection\Compiler\RegisterRouteFiltersPass;
19 20
use Drupal\Core\DependencyInjection\Compiler\RegisterRouteEnhancersPass;
use Drupal\Core\DependencyInjection\Compiler\RegisterParamConvertersPass;
21
use Drupal\Core\DependencyInjection\Compiler\RegisterServicesForDestructionPass;
22
use Drupal\Core\DependencyInjection\Compiler\RegisterStringTranslatorsPass;
23
use Drupal\Core\DependencyInjection\Compiler\RegisterBreadcrumbBuilderPass;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
use Symfony\Component\DependencyInjection\Reference;
26
use Symfony\Component\DependencyInjection\Definition;
27
use Symfony\Component\DependencyInjection\Scope;
28 29
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

30
/**
31
 * ServiceProvider class for mandatory core services.
32
 *
33 34 35 36 37 38
 * 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.
39
 */
40
class CoreServiceProvider implements ServiceProviderInterface  {
41

42
  /**
43
   * {@inheritdoc}
44
   */
45
  public function register(ContainerBuilder $container) {
46 47 48
    // 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).
49
    $container->addScope(new Scope('request'));
50
    $this->registerTwig($container);
51
    $this->registerModuleHandler($container);
52

53
    $container->addCompilerPass(new RegisterMatchersPass());
54 55
    $container->addCompilerPass(new RegisterRouteFiltersPass());
    // Add a compiler pass for registering event subscribers.
56 57
    $container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING);
    $container->addCompilerPass(new RegisterAccessChecksPass());
58 59
    // Add a compiler pass for upcasting of entity route parameters.
    $container->addCompilerPass(new RegisterParamConvertersPass());
60
    $container->addCompilerPass(new RegisterRouteEnhancersPass());
61 62
    // Add a compiler pass for registering services needing destruction.
    $container->addCompilerPass(new RegisterServicesForDestructionPass());
63 64 65
    // Add the compiler pass that will process the tagged services.
    $container->addCompilerPass(new RegisterPathProcessorsPass());
    $container->addCompilerPass(new ListCacheBinsPass());
66 67
    // Add the compiler pass for appending string translators.
    $container->addCompilerPass(new RegisterStringTranslatorsPass());
68 69 70
    // Add the compiler pass that will process the tagged breadcrumb builder
    // services.
    $container->addCompilerPass(new RegisterBreadcrumbBuilderPass());
71 72 73
    // Add the compiler pass that lets service providers modify existing
    // service definitions.
    $container->addCompilerPass(new ModifyServiceDefinitionsPass());
74
  }
75

76 77
  /**
   * Registers the module handler.
78 79
   *
   * As this is different during install, it needs to stay in PHP.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
   */
  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'));
    }
  }

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 119 120 121 122
        '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,
123 124
        'debug' => settings()->get('twig_debug', FALSE),
        'auto_reload' => settings()->get('twig_auto_reload', NULL),
125 126 127 128 129 130
      ))
      ->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')));
  }
131

katbailey's avatar
katbailey committed
132
}