-
Notifications
You must be signed in to change notification settings - Fork 638
/
LateBootService.php
141 lines (120 loc) · 4.37 KB
/
LateBootService.php
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Install\Service;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\DependencyInjection\ContainerBuilder;
use TYPO3\CMS\Core\Imaging\IconRegistry;
use TYPO3\CMS\Core\Package\PackageManager;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* @internal This is NOT an API class, it is for internal use in the install tool only.
*/
class LateBootService
{
/**
* @var ContainerBuilder
*/
private $containerBuilder;
/**
* @var ContainerInterface
*/
private $failsafeContainer;
/**
* @var ContainerInterface
*/
private $container;
/**
* @param ContainerBuilder $containerBuilder
* @param ConteinerInterface $failsafeContainer
*/
public function __construct(ContainerBuilder $containerBuilder, ContainerInterface $failsafeContainer)
{
$this->containerBuilder = $containerBuilder;
$this->failsafeContainer = $failsafeContainer;
}
/**
* @return ContainerInterface
*/
public function getContainer(): ContainerInterface
{
return $this->container ?? $this->prepareContainer();
}
/**
* @return ContainerInterface
*/
private function prepareContainer(): ContainerInterface
{
$packageManager = $this->failsafeContainer->get(PackageManager::class);
$dependencyInjectionContainerCache = $this->failsafeContainer->get('cache.di');
$failsafe = false;
// Build a non-failsafe container which is required for loading ext_localconf
return $this->container = $this->containerBuilder->createDependencyInjectionContainer($packageManager, $dependencyInjectionContainerCache, $failsafe);
}
/**
* Switch global context to a new context, or revert
* to the original booting container if no container
* is specified
*
* @param ContainerInterface $container
* @param array $backup
* @return array
*/
public function makeCurrent(ContainerInterface $container = null, array $oldBackup = []): array
{
$container = $container ?? $this->failsafeContainer;
$backup = [
'singletonInstances', GeneralUtility::getSingletonInstances(),
];
GeneralUtility::purgeInstances();
// Set global state to the non-failsafe container and it's instances
GeneralUtility::setContainer($container);
ExtensionManagementUtility::setPackageManager($container->get(PackageManager::class));
$backupSingletonInstances = $oldBackup['singletonInstances'] ?? [];
foreach ($backupSingletonInstances as $className => $instance) {
GeneralUtility::setSingletonInstance($className, $instance);
}
return $backup;
}
/**
* Bootstrap a non-failsafe container and load ext_localconf
*
* @param bool $resetContainer
* @return ContainerInterface
*/
public function loadExtLocalconfDatabaseAndExtTables(bool $resetContainer = true): ContainerInterface
{
$container = $this->getContainer();
$backup = $this->makeCurrent($container);
$container->get('boot.state')->done = false;
$assetsCache = $container->get('cache.assets');
IconRegistry::setCache($assetsCache);
PageRenderer::setCache($assetsCache);
$eventDispatcher = $container->get(EventDispatcherInterface::class);
ExtensionManagementUtility::setEventDispatcher($eventDispatcher);
Bootstrap::loadTypo3LoadedExtAndExtLocalconf(false);
Bootstrap::unsetReservedGlobalVariables();
$container->get('boot.state')->done = true;
Bootstrap::loadBaseTca(false);
Bootstrap::loadExtTables(false);
if ($resetContainer) {
$this->makeCurrent(null, $backup);
}
return $container;
}
}