‪TYPO3CMS  ‪main
BootService.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Core;
19 
20 use Psr\Container\ContainerInterface;
21 use Psr\EventDispatcher\EventDispatcherInterface;
26 use TYPO3\CMS\Core\DependencyInjection\ContainerBuilder;
27 use TYPO3\CMS\Core\Package\PackageManager;
30 
35 {
36  private ContainerBuilder ‪$containerBuilder;
37 
38  private ContainerInterface ‪$failsafeContainer;
39 
40  private ?ContainerInterface ‪$container = null;
41 
42  public function ‪__construct(ContainerBuilder ‪$containerBuilder, ContainerInterface ‪$failsafeContainer)
43  {
44  $this->containerBuilder = ‪$containerBuilder;
45  $this->failsafeContainer = ‪$failsafeContainer;
46  }
47 
48  public function ‪getContainer(bool $allowCaching = true): ContainerInterface
49  {
50  return $this->container ?? $this->‪prepareContainer($allowCaching);
51  }
52 
53  private function ‪prepareContainer(bool $allowCaching = true): ContainerInterface
54  {
55  $packageManager = $this->failsafeContainer->get(PackageManager::class);
56  $dependencyInjectionContainerCache = $this->failsafeContainer->get('cache.di');
57 
58  $failsafe = false;
59 
60  // Build a non-failsafe container which is required for loading ext_localconf
61  $this->container = $this->containerBuilder->createDependencyInjectionContainer($packageManager, $dependencyInjectionContainerCache, $failsafe);
62  $this->container->set('_early.boot-service', $this);
63  if ($allowCaching) {
64  $this->container->get('boot.state')->cacheDisabled = false;
65  $coreCache = ‪Bootstrap::createCache('core');
66  // Core cache is initialized with a NullBackend in failsafe mode.
67  // Replace it with a new cache that uses the real backend.
68  $this->container->set('_early.cache.core', $coreCache);
69  $this->container->set('_early.cache.assets', ‪Bootstrap::createCache('assets'));
71  $this->container->get(PackageManager::class)->setPackageCache(‪Bootstrap::createPackageCache($coreCache));
72  }
73  }
74 
75  return ‪$this->container;
76  }
77 
83  public function ‪makeCurrent(ContainerInterface ‪$container = null, array $backup = []): array
84  {
85  ‪$container = ‪$container ?? $backup['container'] ?? ‪$this->failsafeContainer;
86 
87  $newBackup = [
88  'singletonInstances' => GeneralUtility::getSingletonInstances(),
89  'container' => GeneralUtility::getContainer(),
90  ];
91 
92  GeneralUtility::purgeInstances();
93 
94  // Set global state to the non-failsafe container and it's instances
95  GeneralUtility::setContainer(‪$container);
97 
98  $backupSingletonInstances = $backup['singletonInstances'] ?? [];
99  foreach ($backupSingletonInstances as $className => $instance) {
100  GeneralUtility::setSingletonInstance($className, $instance);
101  }
102 
103  return $newBackup;
104  }
105 
118  public function ‪loadExtLocalconfDatabaseAndExtTables(bool $resetContainer = false, bool $allowCaching = true, bool $loadExtTables = true): ContainerInterface
119  {
120  ‪$container = $this->‪getContainer($allowCaching);
121 
122  $backup = $this->‪makeCurrent($container);
123  $beUserBackup = ‪$GLOBALS['BE_USER'] ?? null;
124 
125  ‪$container->get('boot.state')->complete = false;
126  $eventDispatcher = ‪$container->get(EventDispatcherInterface::class);
127  $tcaFactory = ‪$container->get(TcaFactory::class);
128  if ($allowCaching) {
129  ‪$container->get(ExtLocalconfFactory::class)->load();
130  } else {
131  ‪$container->get(ExtLocalconfFactory::class)->loadUncached();
132  }
134  ‪$GLOBALS['BE_USER'] = $beUserBackup;
135  if ($allowCaching) {
136  ‪$GLOBALS['TCA'] = $tcaFactory->get();
137  } else {
138  ‪$GLOBALS['TCA'] = $tcaFactory->create();
139  }
140  ‪$container->get('boot.state')->complete = true;
141  $eventDispatcher->dispatch(new ‪BootCompletedEvent($allowCaching));
142  if ($loadExtTables) {
143  if ($allowCaching) {
144  ‪$container->get(ExtTablesFactory::class)->load();
145  } else {
146  ‪$container->get(ExtTablesFactory::class)->loadUncached();
147  }
148  }
149 
150  if ($resetContainer) {
151  $this->‪makeCurrent(null, $backup);
152  }
153 
154  return ‪$container;
155  }
156 
157  public function ‪resetGlobalContainer(): void
158  {
159  $this->‪makeCurrent(null, []);
160  }
161 
162  public function ‪getFailsafeContainer(): ContainerInterface
163  {
165  }
166 }
‪TYPO3\CMS\Core\Configuration\Tca\TcaFactory
Definition: TcaFactory.php:34
‪TYPO3\CMS\Core\Core\BootService\__construct
‪__construct(ContainerBuilder $containerBuilder, ContainerInterface $failsafeContainer)
Definition: BootService.php:42
‪TYPO3\CMS\Core\Core\BootService\loadExtLocalconfDatabaseAndExtTables
‪loadExtLocalconfDatabaseAndExtTables(bool $resetContainer=false, bool $allowCaching=true, bool $loadExtTables=true)
Definition: BootService.php:118
‪TYPO3\CMS\Core\Core\Bootstrap\unsetReservedGlobalVariables
‪static unsetReservedGlobalVariables()
Definition: Bootstrap.php:465
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static isComposerMode()
Definition: Environment.php:137
‪TYPO3\CMS\Core\Core\Bootstrap\createPackageCache
‪static createPackageCache(FrontendInterface $coreCache)
Definition: Bootstrap.php:290
‪TYPO3\CMS\Core\Core\BootService\getFailsafeContainer
‪getFailsafeContainer()
Definition: BootService.php:162
‪TYPO3\CMS\Core\Core\BootService
Definition: BootService.php:35
‪TYPO3\CMS\Core\Core\BootService\$failsafeContainer
‪ContainerInterface $failsafeContainer
Definition: BootService.php:38
‪TYPO3\CMS\Core\Core\BootService\getContainer
‪getContainer(bool $allowCaching=true)
Definition: BootService.php:48
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:32
‪TYPO3\CMS\Core\Core\BootService\prepareContainer
‪prepareContainer(bool $allowCaching=true)
Definition: BootService.php:53
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\setPackageManager
‪static setPackageManager(PackageManager $packageManager)
Definition: ExtensionManagementUtility.php:41
‪TYPO3\CMS\Core\Core\BootService\resetGlobalContainer
‪resetGlobalContainer()
Definition: BootService.php:157
‪TYPO3\CMS\Core\Core\BootService\$containerBuilder
‪ContainerBuilder $containerBuilder
Definition: BootService.php:36
‪TYPO3\CMS\Core\Configuration\Extension\ExtLocalconfFactory
Definition: ExtLocalconfFactory.php:28
‪TYPO3\CMS\Core\Core\Event\BootCompletedEvent
Definition: BootCompletedEvent.php:24
‪TYPO3\CMS\Core\Configuration\Extension\ExtTablesFactory
Definition: ExtTablesFactory.php:28
‪TYPO3\CMS\Core\Core\Bootstrap\createCache
‪static createCache(string $identifier, bool $disableCaching=false)
Definition: Bootstrap.php:335
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core
Definition: ApplicationContext.php:16
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Core\BootService\$container
‪ContainerInterface $container
Definition: BootService.php:40
‪TYPO3\CMS\Core\Core\BootService\makeCurrent
‪makeCurrent(ContainerInterface $container=null, array $backup=[])
Definition: BootService.php:83