38896a5d7d6c76ec5e0eeed7ebeeb7655a0a100f
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Core / Bootstrap.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Creates a request an dispatches it to the controller which was specified
27 * by TS Setup, Flexform and returns the content to the v4 framework.
28 *
29 * This class is the main entry point for extbase extensions.
30 *
31 * @package Extbase
32 * @version $ID:$
33 */
34 class Tx_Extbase_Core_Bootstrap {
35
36 /**
37 * Back reference to the parent content object
38 * This has to be public as it is set directly from TYPO3
39 *
40 * @var tslib_cObj
41 */
42 public $cObj;
43
44 /**
45 * The application context
46 * @var string
47 */
48 protected $context;
49
50 /**
51 * @var Tx_Extbase_Configuration_ConfigurationManager
52 */
53 protected $configurationManager;
54
55 /**
56 * @var Tx_Extbase_Object_ObjectManagerInterface
57 */
58 protected $objectManager;
59
60 /**
61 * @var t3lib_cache_Manager
62 */
63 protected $cacheManager;
64
65 /**
66 * @var Tx_Extbase_Reflection_Service
67 */
68 protected $reflectionService;
69
70 /**
71 * @var Tx_Extbase_Persistence_Manager
72 */
73 protected $persistenceManager;
74
75 /**
76 * @var boolean
77 */
78 protected $isInitialized = FALSE;
79
80 /**
81 * Explicitly initializes all necessary Extbase objects by invoking the various initialize* methods.
82 *
83 * Usually this method is only called from unit tests or other applications which need a more fine grained control over
84 * the initialization and request handling process. Most other applications just call the run() method.
85 *
86 * @param array $configuration The TS configuration array
87 * @return void
88 * @see run()
89 * @api
90 */
91 public function initialize($configuration) {
92 if (!isset($configuration['extensionName']) || strlen($configuration['extensionName']) === 0) {
93 throw new RuntimeException('Invalid configuration: "extensionName" is not set', 1290623020);
94 }
95 if (!isset($configuration['pluginName']) || strlen($configuration['pluginName']) === 0) {
96 throw new RuntimeException('Invalid configuration: "pluginName" is not set', 1290623027);
97 }
98 $this->initializeClassLoader();
99 $this->initializeObjectManager();
100 $this->initializeConfiguration($configuration);
101 $this->configureObjectManager();
102 $this->initializeCache();
103 $this->initializeReflection();
104 $this->initializePersistence();
105 $this->initializeBackwardsCompatibility();
106 $this->isInitialized = TRUE;
107 }
108
109 /**
110 * Initializes the autoload mechanism of Extbase. This is supplement to the core autoloader.
111 *
112 * @return void
113 * @see initialize()
114 */
115 protected function initializeClassLoader() {
116 if (!class_exists('Tx_Extbase_Utility_ClassLoader', FALSE)) {
117 require(t3lib_extmgm::extPath('extbase') . 'Classes/Utility/ClassLoader.php');
118 }
119
120 $classLoader = new Tx_Extbase_Utility_ClassLoader();
121 spl_autoload_register(array($classLoader, 'loadClass'));
122 }
123
124 /**
125 * Initializes the Object framework.
126 *
127 * @return void
128 * @see initialize()
129 */
130 protected function initializeObjectManager() {
131 $this->objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
132 }
133
134 /**
135 * Initializes the Object framework.
136 *
137 * @return void
138 * @see initialize()
139 */
140 public function initializeConfiguration($configuration) {
141 $this->configurationManager = $this->objectManager->get('Tx_Extbase_Configuration_ConfigurationManagerInterface');
142 $contentObject = isset($this->cObj) ? $this->cObj : t3lib_div::makeInstance('tslib_cObj');
143 $this->configurationManager->setContentObject($contentObject);
144 $this->configurationManager->setConfiguration($configuration);
145 }
146
147 /**
148 * Configures the object manager object configuration from
149 * config.tx_extbase.objects
150 *
151 * @return void
152 * @see initialize()
153 */
154 public function configureObjectManager() {
155 $typoScriptSetup = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
156 if (!is_array($typoScriptSetup['config.']['tx_extbase.']['objects.'])) {
157 return;
158 }
159 $objectContainer = t3lib_div::makeInstance('Tx_Extbase_Object_Container_Container');
160 foreach ($typoScriptSetup['config.']['tx_extbase.']['objects.'] as $classNameWithDot => $classConfiguration) {
161 if (isset($classConfiguration['className'])) {
162 $originalClassName = rtrim($classNameWithDot, '.');
163 $objectContainer->registerImplementation($originalClassName, $classConfiguration['className']);
164 }
165 }
166 }
167
168 /**
169 * Initializes the cache framework
170 *
171 * @return void
172 * @see initialize()
173 */
174 protected function initializeCache() {
175 t3lib_cache::initializeCachingFramework();
176 $this->cacheManager = $GLOBALS['typo3CacheManager'];
177 try {
178 $this->cacheManager->getCache('cache_extbase_reflection');
179 } catch (t3lib_cache_exception_NoSuchCache $exception) {
180 $GLOBALS['typo3CacheFactory']->create(
181 'cache_extbase_reflection',
182 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['frontend'],
183 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['backend'],
184 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['options']
185 );
186 }
187 }
188
189 /**
190 * Initializes the Reflection Service
191 *
192 * @return void
193 * @see initialize()
194 */
195 protected function initializeReflection() {
196 $this->reflectionService = $this->objectManager->get('Tx_Extbase_Reflection_Service');
197 $this->reflectionService->setDataCache($this->cacheManager->getCache('cache_extbase_reflection'));
198 $this->reflectionService->initialize();
199 }
200
201 /**
202 * Initializes the persistence framework
203 *
204 * @return void
205 * @see initialize()
206 */
207 public function initializePersistence() {
208 $this->persistenceManager = $this->objectManager->get('Tx_Extbase_Persistence_Manager'); // singleton
209 }
210
211 /**
212 * Initializes the backwards compatibility. This is necessary because the
213 * old Dispatcher provided several static methods.
214 *
215 * @return void
216 * @see initialize()
217 */
218 protected function initializeBackwardsCompatibility() {
219 $dispatcher = t3lib_div::makeInstance('Tx_Extbase_Dispatcher');
220 $dispatcher->injectConfigurationManager($this->configurationManager);
221 $dispatcher->injectPersistenceManager($this->persistenceManager);
222 }
223
224 /**
225 * Runs the the Extbase Framework by resolving an appropriate Request Handler and passing control to it.
226 * If the Framework is not initialized yet, it will be initialized.
227 *
228 * @param string $content The content
229 * @param array $configuration The TS configuration array
230 * @return string $content The processed content
231 * @api
232 */
233 public function run($content, $configuration) {
234 //var_dump(Tx_Extbase_Utility_Extension::createAutoloadRegistryForExtension('extbase', t3lib_extMgm::extPath('extbase'), array(
235 // 'tx_extbase_basetestcase' => '$extensionClassesPath . \'../Tests/BaseTestCase.php\''
236 //)));
237 //die("autoload registry");
238
239 $this->initialize($configuration);
240
241 $requestHandlerResolver = $this->objectManager->get('Tx_Extbase_MVC_RequestHandlerResolver');
242 $requestHandler = $requestHandlerResolver->resolveRequestHandler();
243
244 $response = $requestHandler->handleRequest();
245
246 // If response is NULL after handling the request we need to stop
247 // This happens for instance, when a USER object was converted to a USER_INT
248 // @see Tx_Extbase_MVC_Web_FrontendRequestHandler::handleRequest()
249 if ($response === NULL) {
250 $this->reflectionService->shutdown();
251 return;
252 }
253 if (count($response->getAdditionalHeaderData()) > 0) {
254 $GLOBALS['TSFE']->additionalHeaderData[] = implode(chr(10), $response->getAdditionalHeaderData());
255 }
256 $response->sendHeaders();
257 $content = $response->getContent();
258
259 $this->resetSingletons();
260 return $content;
261 }
262
263 /**
264 * Resets global singletons for the next plugin
265 *
266 * @return void
267 */
268 protected function resetSingletons() {
269 $this->persistenceManager->persistAll();
270 $this->reflectionService->shutdown();
271 }
272
273 /**
274 * This method forwards the call to run(). This method is invoked by the mod.php
275 * function of TYPO3.
276 *
277 * @param string $moduleSignature
278 * @return boolean TRUE, if the request request could be dispatched
279 * @see run()
280 **/
281 public function callModule($moduleSignature) {
282 if (!isset($GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature])) {
283 return FALSE;
284 }
285 $moduleConfiguration = $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature];
286
287 // Check permissions and exit if the user has no permission for entry
288 $GLOBALS['BE_USER']->modAccess($moduleConfiguration, TRUE);
289 if (t3lib_div::_GP('id')) {
290 // Check page access
291 $permClause = $GLOBALS['BE_USER']->getPagePermsClause(TRUE);
292 $access = is_array(t3lib_BEfunc::readPageAccess((integer)t3lib_div::_GP('id'), $permClause));
293 if (!$access) {
294 throw new RuntimeException('You don\'t have access to this page', 1289917924);
295 }
296 }
297
298 // BACK_PATH is the path from the typo3/ directory from within the
299 // directory containing the controller file. We are using mod.php dispatcher
300 // and thus we are already within typo3/ because we call typo3/mod.php
301 $GLOBALS['BACK_PATH'] = '';
302
303 $configuration = array(
304 'extensionName' => $moduleConfiguration['extensionName'],
305 'pluginName' => $moduleSignature
306 );
307 $content = $this->run('', $configuration);
308
309 print $content;
310 return TRUE;
311 }
312 }
313 ?>