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