protected DrupalKernel::initializeContainer()
Initializes the service container.
Return value
\Symfony\Component\DependencyInjection\ContainerInterface
File
- core/lib/Drupal/Core/DrupalKernel.php, line 830
Class
- DrupalKernel
- The DrupalKernel class is the core of Drupal itself.
Namespace
Drupal\Core
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | protected function initializeContainer() { $this ->containerNeedsDumping = FALSE; $session_started = FALSE; if (isset( $this ->container)) { // Save the id of the currently logged in user. if ( $this ->container->initialized( 'current_user' )) { $current_user_id = $this ->container->get( 'current_user' )->id(); } // If there is a session, close and save it. if ( $this ->container->initialized( 'session' )) { $session = $this ->container->get( 'session' ); if ( $session ->isStarted()) { $session_started = TRUE; $session ->save(); } unset( $session ); } } // If we haven't booted yet but there is a container, then we're asked to // boot the container injected via setContainer(). // @see \Drupal\KernelTests\KernelTestBase::setUp() if (isset( $this ->container) && ! $this ->booted) { $container = $this ->container; } // If the module list hasn't already been set in updateModules and we are // not forcing a rebuild, then try and load the container from the cache. if ( empty ( $this ->moduleList) && ! $this ->containerNeedsRebuild) { $container_definition = $this ->getCachedContainerDefinition(); } // If there is no container and no cached container definition, build a new // one from scratch. if (!isset( $container ) && !isset( $container_definition )) { $container = $this ->compileContainer(); // Only dump the container if dumping is allowed. This is useful for // KernelTestBase, which never wants to use the real container, but always // the container builder. if ( $this ->allowDumping) { $dumper = new $this ->phpArrayDumperClass( $container ); $container_definition = $dumper ->getArray(); } } // The container was rebuilt successfully. $this ->containerNeedsRebuild = FALSE; // Only create a new class if we have a container definition. if (isset( $container_definition )) { $class = Settings::get( 'container_base_class' , '\Drupal\Core\DependencyInjection\Container' ); $container = new $class ( $container_definition ); } $this ->attachSynthetic( $container ); $this ->container = $container ; if ( $session_started ) { $this ->container->get( 'session' )->start(); } // The request stack is preserved across container rebuilds. Reinject the // new session into the master request if one was present before. if (( $request_stack = $this ->container->get( 'request_stack' , ContainerInterface::NULL_ON_INVALID_REFERENCE))) { if ( $request = $request_stack ->getMasterRequest()) { if ( $request ->hasSession()) { $request ->setSession( $this ->container->get( 'session' )); } } } if (! empty ( $current_user_id )) { $this ->container->get( 'current_user' )->setInitialAccountId( $current_user_id ); } \Drupal::setContainer( $this ->container); // If needs dumping flag was set, dump the container. if ( $this ->containerNeedsDumping && ! $this ->cacheDrupalContainer( $container_definition )) { $this ->container->get( 'logger.factory' )->get( 'DrupalKernel' )->error( 'Container cannot be saved to cache.' ); } return $this ->container; } |
Please login to continue.