[TASK] Remove ext:dbal from installation steps
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Core / Bootstrap.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Core;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Creates a request an dispatches it to the controller which was specified
19 * by TS Setup, flexForm and returns the content to the v4 framework.
20 *
21 * This class is the main entry point for extbase extensions.
22 */
23 class Bootstrap implements \TYPO3\CMS\Extbase\Core\BootstrapInterface
24 {
25 /**
26 * Back reference to the parent content object
27 * This has to be public as it is set directly from TYPO3
28 *
29 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
30 */
31 public $cObj;
32
33 /**
34 * The application context
35 *
36 * @var string
37 */
38 protected $context;
39
40 /**
41 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
42 */
43 protected $configurationManager;
44
45 /**
46 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
47 */
48 protected $objectManager;
49
50 /**
51 * @var \TYPO3\CMS\Core\Cache\CacheManager
52 */
53 protected $cacheManager;
54
55 /**
56 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
57 */
58 protected $reflectionService;
59
60 /**
61 * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
62 */
63 protected $persistenceManager;
64
65 /**
66 * Explicitly initializes all necessary Extbase objects by invoking the various initialize* methods.
67 *
68 * Usually this method is only called from unit tests or other applications which need a more fine grained control over
69 * the initialization and request handling process. Most other applications just call the run() method.
70 *
71 * @param array $configuration The TS configuration array
72 * @throws \RuntimeException
73 * @return void
74 * @see run()
75 * @api
76 */
77 public function initialize($configuration)
78 {
79 if (!$this->isInCliMode()) {
80 if (!isset($configuration['extensionName']) || $configuration['extensionName'] === '') {
81 throw new \RuntimeException('Invalid configuration: "extensionName" is not set', 1290623020);
82 }
83 if (!isset($configuration['pluginName']) || $configuration['pluginName'] === '') {
84 throw new \RuntimeException('Invalid configuration: "pluginName" is not set', 1290623027);
85 }
86 }
87 $this->initializeObjectManager();
88 $this->initializeConfiguration($configuration);
89 $this->configureObjectManager();
90 $this->initializeCache();
91 $this->initializeReflection();
92 $this->initializePersistence();
93 }
94
95 /**
96 * Initializes the Object framework.
97 *
98 * @return void
99 * @see initialize()
100 */
101 protected function initializeObjectManager()
102 {
103 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
104 }
105
106 /**
107 * Initializes the Object framework.
108 *
109 * @param array $configuration
110 * @return void
111 * @see initialize()
112 */
113 public function initializeConfiguration($configuration)
114 {
115 $this->configurationManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
116 /** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject */
117 $contentObject = isset($this->cObj) ? $this->cObj : \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
118 $this->configurationManager->setContentObject($contentObject);
119 $this->configurationManager->setConfiguration($configuration);
120 }
121
122 /**
123 * Configures the object manager object configuration from
124 * config.tx_extbase.objects and plugin.tx_foo.objects
125 *
126 * @return void
127 * @see initialize()
128 */
129 public function configureObjectManager()
130 {
131 $frameworkSetup = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
132 if (!is_array($frameworkSetup['objects'])) {
133 return;
134 }
135 $objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class);
136 foreach ($frameworkSetup['objects'] as $classNameWithDot => $classConfiguration) {
137 if (isset($classConfiguration['className'])) {
138 $originalClassName = rtrim($classNameWithDot, '.');
139 $objectContainer->registerImplementation($originalClassName, $classConfiguration['className']);
140 }
141 }
142 }
143
144 /**
145 * Initializes the cache framework
146 *
147 * @return void
148 * @see initialize()
149 */
150 protected function initializeCache()
151 {
152 $this->cacheManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
153 }
154
155 /**
156 * Initializes the Reflection Service
157 *
158 * @return void
159 * @see initialize()
160 */
161 protected function initializeReflection()
162 {
163 $this->reflectionService = $this->objectManager->get(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
164 $this->reflectionService->setDataCache($this->cacheManager->getCache('extbase_reflection'));
165 if (!$this->reflectionService->isInitialized()) {
166 $this->reflectionService->initialize();
167 }
168 }
169
170 /**
171 * Initializes the persistence framework
172 *
173 * @return void
174 * @see initialize()
175 */
176 public function initializePersistence()
177 {
178 $this->persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
179 }
180
181 /**
182 * Runs the the Extbase Framework by resolving an appropriate Request Handler and passing control to it.
183 * If the Framework is not initialized yet, it will be initialized.
184 *
185 * @param string $content The content. Not used
186 * @param array $configuration The TS configuration array
187 * @return string $content The processed content
188 * @api
189 */
190 public function run($content, $configuration)
191 {
192 $this->initialize($configuration);
193 return $this->handleRequest();
194 }
195
196 /**
197 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\CommandException Is thrown if the response object defined an exit code > 0
198 * @return string
199 */
200 protected function handleRequest()
201 {
202 /** @var $requestHandlerResolver \TYPO3\CMS\Extbase\Mvc\RequestHandlerResolver */
203 $requestHandlerResolver = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\RequestHandlerResolver::class);
204 $requestHandler = $requestHandlerResolver->resolveRequestHandler();
205
206 $response = $requestHandler->handleRequest();
207 // If response is NULL after handling the request we need to stop
208 // This happens for instance, when a USER object was converted to a USER_INT
209 // @see TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::handleRequest()
210 if ($response === null) {
211 $this->reflectionService->shutdown();
212 $content = '';
213 } else {
214 $content = $response->shutdown();
215 $this->resetSingletons();
216 $this->objectManager->get(\TYPO3\CMS\Extbase\Service\CacheService::class)->clearCachesOfRegisteredPageIds();
217 if ($this->isInCliMode() && $response->getExitCode()) {
218 throw new \TYPO3\CMS\Extbase\Mvc\Exception\CommandException('The request has been terminated as the response defined an exit code.', $response->getExitCode());
219 }
220 }
221
222 return $content;
223 }
224
225 /**
226 * Resets global singletons for the next plugin
227 *
228 * @return void
229 */
230 protected function resetSingletons()
231 {
232 $this->persistenceManager->persistAll();
233 $this->reflectionService->shutdown();
234 }
235
236 /**
237 * @return bool
238 */
239 protected function isInCliMode()
240 {
241 return (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI);
242 }
243 }