Passed
Push — master ( 39145a...3500a5 )
by
unknown
15:58
created

BootService::getFailsafeContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
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;
22
use TYPO3\CMS\Core\DependencyInjection\ContainerBuilder;
23
use TYPO3\CMS\Core\Imaging\IconRegistry;
24
use TYPO3\CMS\Core\Package\PackageManager;
25
use TYPO3\CMS\Core\Page\PageRenderer;
26
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29
/**
30
 * @internal This is NOT an API class, it is for internal use in TYPO3 core only.
31
 */
32
class BootService
33
{
34
    private ContainerBuilder $containerBuilder;
35
36
    private ContainerInterface $failsafeContainer;
37
38
    private ?ContainerInterface $container = null;
39
40
    public function __construct(ContainerBuilder $containerBuilder, ContainerInterface $failsafeContainer)
41
    {
42
        $this->containerBuilder = $containerBuilder;
43
        $this->failsafeContainer = $failsafeContainer;
44
    }
45
46
    public function getContainer(bool $allowCaching = true): ContainerInterface
47
    {
48
        return $this->container ?? $this->prepareContainer($allowCaching);
49
    }
50
51
    private function prepareContainer(bool $allowCaching = true): ContainerInterface
52
    {
53
        $packageManager = $this->failsafeContainer->get(PackageManager::class);
54
        $dependencyInjectionContainerCache = $this->failsafeContainer->get('cache.di');
55
56
        $failsafe = false;
57
58
        // Build a non-failsafe container which is required for loading ext_localconf
59
        $this->container = $this->containerBuilder->createDependencyInjectionContainer($packageManager, $dependencyInjectionContainerCache, $failsafe);
60
        $this->container->set('_early.boot-service', $this);
0 ignored issues
show
Bug introduced by
The method set() does not exist on Psr\Container\ContainerInterface. It seems like you code against a sub-type of Psr\Container\ContainerInterface such as Symfony\Component\Depend...tion\ContainerInterface or Symfony\Component\Depend...rameterBag\ContainerBag. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        $this->container->/** @scrutinizer ignore-call */ 
61
                          set('_early.boot-service', $this);
Loading history...
61
        if ($allowCaching) {
62
            $this->container->get('boot.state')->cacheDisabled = false;
63
            $coreCache = Bootstrap::createCache('core');
64
            // Core cache is initialized with a NullBackend in failsafe mode.
65
            // Replace it with a new cache that uses the real backend.
66
            $this->container->set('_early.cache.core', $coreCache);
67
            $this->container->set('_early.cache.assets', Bootstrap::createCache('assets'));
68
            $this->container->get(PackageManager::class)->injectCoreCache($coreCache);
69
        }
70
71
        return $this->container;
72
    }
73
74
    /**
75
     * Switch global context to a new context, or revert
76
     * to the original booting container if no container
77
     * is specified
78
     *
79
     * @param ContainerInterface $container
80
     * @param array $backup
81
     * @return array
82
     */
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);
96
        ExtensionManagementUtility::setPackageManager($container->get(PackageManager::class));
97
98
        $backupSingletonInstances = $backup['singletonInstances'] ?? [];
99
        foreach ($backupSingletonInstances as $className => $instance) {
100
            GeneralUtility::setSingletonInstance($className, $instance);
101
        }
102
103
        return $newBackup;
104
    }
105
106
    /**
107
     * Bootstrap a non-failsafe container and load ext_localconf
108
     *
109
     * Use by actions like the database analyzer and the upgrade wizards which
110
     * need additional bootstrap actions performed.
111
     *
112
     * Those actions can potentially fatal if some old extension is loaded that triggers
113
     * a fatal in ext_localconf or ext_tables code! Use only if really needed.
114
     *
115
     * @param bool $resetContainer
116
     * @param bool $allowCaching
117
     * @return ContainerInterface
118
     */
119
    public function loadExtLocalconfDatabaseAndExtTables(bool $resetContainer = false, bool $allowCaching = true): ContainerInterface
120
    {
121
        $container = $this->getContainer($allowCaching);
122
123
        $backup = $this->makeCurrent($container);
124
        $beUserBackup = $GLOBALS['BE_USER'] ?? null;
125
126
        $container->get('boot.state')->done = false;
127
        $assetsCache = $container->get('cache.assets');
128
        IconRegistry::setCache($assetsCache);
129
        PageRenderer::setCache($assetsCache);
130
        $eventDispatcher = $container->get(EventDispatcherInterface::class);
131
        ExtensionManagementUtility::setEventDispatcher($eventDispatcher);
132
        Bootstrap::loadTypo3LoadedExtAndExtLocalconf($allowCaching, $container->get('cache.core'));
133
        Bootstrap::unsetReservedGlobalVariables();
134
        $GLOBALS['BE_USER'] = $beUserBackup;
135
        $container->get('boot.state')->done = true;
136
        Bootstrap::loadBaseTca($allowCaching);
137
        Bootstrap::loadExtTables($allowCaching);
138
139
        if ($resetContainer) {
140
            $this->makeCurrent(null, $backup);
141
        }
142
143
        return $container;
144
    }
145
146
    public function resetGlobalContainer(): void
147
    {
148
        $this->makeCurrent(null, []);
149
    }
150
151
    public function getFailsafeContainer(): ContainerInterface
152
    {
153
        return $this->failsafeContainer;
154
    }
155
}
156