* Updated ChangeLog and raised version number to 1.3.0RC1
[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 if (!$this->reflectionService->isInitialized()) {
199 $this->reflectionService->initialize();
200 }
201 }
202
203 /**
204 * Initializes the persistence framework
205 *
206 * @return void
207 * @see initialize()
208 */
209 public function initializePersistence() {
210 $this->persistenceManager = $this->objectManager->get('Tx_Extbase_Persistence_Manager'); // singleton
211 }
212
213 /**
214 * Initializes the backwards compatibility. This is necessary because the
215 * old Dispatcher provided several static methods.
216 *
217 * @return void
218 * @see initialize()
219 */
220 protected function initializeBackwardsCompatibility() {
221 $dispatcher = t3lib_div::makeInstance('Tx_Extbase_Dispatcher');
222 $dispatcher->injectConfigurationManager($this->configurationManager);
223 $dispatcher->injectPersistenceManager($this->persistenceManager);
224 }
225
226 /**
227 * Runs the the Extbase Framework by resolving an appropriate Request Handler and passing control to it.
228 * If the Framework is not initialized yet, it will be initialized.
229 *
230 * @param string $content The content
231 * @param array $configuration The TS configuration array
232 * @return string $content The processed content
233 * @api
234 */
235 public function run($content, $configuration) {
236 //var_dump(Tx_Extbase_Utility_Extension::createAutoloadRegistryForExtension('extbase', t3lib_extMgm::extPath('extbase'), array(
237 // 'tx_extbase_basetestcase' => '$extensionClassesPath . \'../Tests/BaseTestCase.php\'',
238 // 'tx_extbase_tests_unit_basetestcase' => '$extensionClassesPath . \'../Tests/Unit/BaseTestCase.php\'',
239 //)));
240 //die("autoload registry");
241
242 $this->initialize($configuration);
243
244 $requestHandlerResolver = $this->objectManager->get('Tx_Extbase_MVC_RequestHandlerResolver');
245 $requestHandler = $requestHandlerResolver->resolveRequestHandler();
246
247 $response = $requestHandler->handleRequest();
248
249 // If response is NULL after handling the request we need to stop
250 // This happens for instance, when a USER object was converted to a USER_INT
251 // @see Tx_Extbase_MVC_Web_FrontendRequestHandler::handleRequest()
252 if ($response === NULL) {
253 $this->reflectionService->shutdown();
254 return;
255 }
256 if (count($response->getAdditionalHeaderData()) > 0) {
257 $GLOBALS['TSFE']->additionalHeaderData[] = implode(chr(10), $response->getAdditionalHeaderData());
258 }
259 $response->sendHeaders();
260 $content = $response->getContent();
261
262 $this->resetSingletons();
263 return $content;
264 }
265
266 /**
267 * Resets global singletons for the next plugin
268 *
269 * @return void
270 */
271 protected function resetSingletons() {
272 $this->persistenceManager->persistAll();
273 $this->reflectionService->shutdown();
274 }
275
276 /**
277 * This method forwards the call to run(). This method is invoked by the mod.php
278 * function of TYPO3.
279 *
280 * @param string $moduleSignature
281 * @return boolean TRUE, if the request request could be dispatched
282 * @see run()
283 **/
284 public function callModule($moduleSignature) {
285 if (!isset($GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature])) {
286 return FALSE;
287 }
288 $moduleConfiguration = $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature];
289
290 // Check permissions and exit if the user has no permission for entry
291 $GLOBALS['BE_USER']->modAccess($moduleConfiguration, TRUE);
292 if (t3lib_div::_GP('id')) {
293 // Check page access
294 $permClause = $GLOBALS['BE_USER']->getPagePermsClause(TRUE);
295 $access = is_array(t3lib_BEfunc::readPageAccess((integer)t3lib_div::_GP('id'), $permClause));
296 if (!$access) {
297 throw new RuntimeException('You don\'t have access to this page', 1289917924);
298 }
299 }
300
301 // BACK_PATH is the path from the typo3/ directory from within the
302 // directory containing the controller file. We are using mod.php dispatcher
303 // and thus we are already within typo3/ because we call typo3/mod.php
304 $GLOBALS['BACK_PATH'] = '';
305
306 $configuration = array(
307 'extensionName' => $moduleConfiguration['extensionName'],
308 'pluginName' => $moduleSignature
309 );
310 $content = $this->run('', $configuration);
311
312 print $content;
313 return TRUE;
314 }
315 }
316 ?>