[CLEANUP] Replace strlen() with === for zero length check
[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 if (!$this->isInCliMode()) {
79 if (!isset($configuration['extensionName']) || $configuration['extensionName'] === '') {
80 throw new \RuntimeException('Invalid configuration: "extensionName" is not set', 1290623020);
81 }
82 if (!isset($configuration['pluginName']) || $configuration['pluginName'] === '') {
83 throw new \RuntimeException('Invalid configuration: "pluginName" is not set', 1290623027);
84 }
85 }
86 $this->initializeObjectManager();
87 $this->initializeConfiguration($configuration);
88 $this->configureObjectManager();
89 $this->initializeCache();
90 $this->initializeReflection();
91 $this->initializePersistence();
92 }
93
94 /**
95 * Initializes the Object framework.
96 *
97 * @return void
98 * @see initialize()
99 */
100 protected function initializeObjectManager() {
101 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
102 }
103
104 /**
105 * Initializes the Object framework.
106 *
107 * @param array $configuration
108 * @return void
109 * @see initialize()
110 */
111 public function initializeConfiguration($configuration) {
112 $this->configurationManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
113 /** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject */
114 $contentObject = isset($this->cObj) ? $this->cObj : \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
115 $this->configurationManager->setContentObject($contentObject);
116 $this->configurationManager->setConfiguration($configuration);
117 }
118
119 /**
120 * Configures the object manager object configuration from
121 * config.tx_extbase.objects and plugin.tx_foo.objects
122 *
123 * @return void
124 * @see initialize()
125 */
126 public function configureObjectManager() {
127 $frameworkSetup = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
128 if (!is_array($frameworkSetup['objects'])) {
129 return;
130 }
131 $objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class);
132 foreach ($frameworkSetup['objects'] as $classNameWithDot => $classConfiguration) {
133 if (isset($classConfiguration['className'])) {
134 $originalClassName = rtrim($classNameWithDot, '.');
135 $objectContainer->registerImplementation($originalClassName, $classConfiguration['className']);
136 }
137 }
138 }
139
140 /**
141 * Initializes the cache framework
142 *
143 * @return void
144 * @see initialize()
145 */
146 protected function initializeCache() {
147 $this->cacheManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
148 }
149
150 /**
151 * Initializes the Reflection Service
152 *
153 * @return void
154 * @see initialize()
155 */
156 protected function initializeReflection() {
157 $this->reflectionService = $this->objectManager->get(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
158 $this->reflectionService->setDataCache($this->cacheManager->getCache('extbase_reflection'));
159 if (!$this->reflectionService->isInitialized()) {
160 $this->reflectionService->initialize();
161 }
162 }
163
164 /**
165 * Initializes the persistence framework
166 *
167 * @return void
168 * @see initialize()
169 */
170 public function initializePersistence() {
171 $this->persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
172 }
173
174 /**
175 * Runs the the Extbase Framework by resolving an appropriate Request Handler and passing control to it.
176 * If the Framework is not initialized yet, it will be initialized.
177 *
178 * @param string $content The content. Not used
179 * @param array $configuration The TS configuration array
180 * @return string $content The processed content
181 * @api
182 */
183 public function run($content, $configuration) {
184 $this->initialize($configuration);
185 return $this->handleRequest();
186 }
187
188 /**
189 * @return string
190 */
191 protected function handleRequest() {
192 /** @var $requestHandlerResolver \TYPO3\CMS\Extbase\Mvc\RequestHandlerResolver */
193 $requestHandlerResolver = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\RequestHandlerResolver::class);
194 $requestHandler = $requestHandlerResolver->resolveRequestHandler();
195
196 $response = $requestHandler->handleRequest();
197 // If response is NULL after handling the request we need to stop
198 // This happens for instance, when a USER object was converted to a USER_INT
199 // @see TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::handleRequest()
200 if ($response === NULL) {
201 $this->reflectionService->shutdown();
202 $content = '';
203 } else {
204 $content = $response->shutdown();
205 $this->resetSingletons();
206 $this->objectManager->get(\TYPO3\CMS\Extbase\Service\CacheService::class)->clearCachesOfRegisteredPageIds();
207 }
208
209 return $content;
210 }
211
212 /**
213 * Resets global singletons for the next plugin
214 *
215 * @return void
216 */
217 protected function resetSingletons() {
218 $this->persistenceManager->persistAll();
219 $this->reflectionService->shutdown();
220 }
221
222 /**
223 * @return bool
224 */
225 protected function isInCliMode() {
226 return (defined('TYPO3_cliMode') && TYPO3_cliMode);
227 }
228
229 }