[~TASK] Extbase: Reimplemented BE module support. Still needs cleanup.
[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 $this->initializeClassLoader();
93 $this->initializeObjectManager();
94 $this->initializeConfiguration($configuration);
95 $this->configureObjectManager();
96 $this->initializeCache();
97 $this->initializeReflection();
98 $this->initializePersistence();
99 $this->initializeBackwardsCompatibility();
100 $this->isInitialized = TRUE;
101 }
102
103 /**
104 * Initializes the autoload mechanism of Extbase. This is supplement to the core autoloader.
105 *
106 * @return void
107 * @see initialize()
108 */
109 protected function initializeClassLoader() {
110 if (!class_exists('Tx_Extbase_Utility_ClassLoader', FALSE)) {
111 require(t3lib_extmgm::extPath('extbase') . 'Classes/Utility/ClassLoader.php');
112 }
113
114 $classLoader = new Tx_Extbase_Utility_ClassLoader();
115 spl_autoload_register(array($classLoader, 'loadClass'));
116 }
117
118 /**
119 * Initializes the Object framework.
120 *
121 * @return void
122 * @see initialize()
123 */
124 protected function initializeObjectManager() {
125 $this->objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
126 }
127
128 /**
129 * Initializes the Object framework.
130 *
131 * @return void
132 * @see initialize()
133 */
134 public function initializeConfiguration($configuration) {
135 $this->configurationManager = $this->objectManager->get('Tx_Extbase_Configuration_ConfigurationManagerInterface');
136 $contentObject = isset($this->cObj) ? $this->cObj : t3lib_div::makeInstance('tslib_cObj');
137 $this->configurationManager->setContentObject($contentObject);
138 $this->configurationManager->setConfiguration($configuration);
139 }
140
141 /**
142 * Configures the object manager object configuration from
143 * config.tx_extbase.objects
144 *
145 * @return void
146 * @see initialize()
147 */
148 public function configureObjectManager() {
149 $typoScriptSetup = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
150 if (isset($typoScriptSetup['config.']['tx_extbase.']['objects.']) && is_array($typoScriptSetup['config.']['tx_extbase.']['objects.'])) {
151 $objectConfiguration = $typoScriptSetup['config.']['tx_extbase.']['objects.'];
152
153 foreach ($objectConfiguration as $classNameWithDot => $classConfiguration) {
154 if (isset($classConfiguration['className'])) {
155 $originalClassName = substr($classNameWithDot, 0, -1);
156 Tx_Extbase_Object_Container_Container::getContainer()->registerImplementation($originalClassName, $classConfiguration['className']);
157 }
158 }
159 }
160 }
161
162 /**
163 * Initializes the cache framework
164 *
165 * @return void
166 * @see initialize()
167 */
168 protected function initializeCache() {
169 t3lib_cache::initializeCachingFramework();
170 $this->cacheManager = $GLOBALS['typo3CacheManager'];
171 try {
172 $this->cacheManager->getCache('cache_extbase_reflection');
173 } catch (t3lib_cache_exception_NoSuchCache $exception) {
174 $GLOBALS['typo3CacheFactory']->create(
175 'cache_extbase_reflection',
176 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['frontend'],
177 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['backend'],
178 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['options']
179 );
180 }
181 }
182
183 /**
184 * Initializes the Reflection Service
185 *
186 * @return void
187 * @see initialize()
188 */
189 protected function initializeReflection() {
190 $this->reflectionService = $this->objectManager->get('Tx_Extbase_Reflection_Service');
191 $this->reflectionService->setDataCache($this->cacheManager->getCache('cache_extbase_reflection'));
192 $this->reflectionService->initialize();
193 }
194
195 /**
196 * Initializes the persistence framework
197 *
198 * @return void
199 * @see initialize()
200 */
201 public function initializePersistence() {
202 $this->persistenceManager = $this->objectManager->get('Tx_Extbase_Persistence_Manager'); // singleton
203 }
204
205 /**
206 * Initializes the backwards compatibility. This is necessary because the
207 * old Dispatcher provided several static methods.
208 *
209 * @return void
210 * @see initialize()
211 */
212 protected function initializeBackwardsCompatibility() {
213 $dispatcher = t3lib_div::makeInstance('Tx_Extbase_Dispatcher');
214 $dispatcher->injectConfigurationManager($this->configurationManager);
215 $dispatcher->injectPersistenceManager($this->persistenceManager);
216 }
217
218 /**
219 * Runs the the Extbase Framework by resolving an appropriate Request Handler and passing control to it.
220 * If the Framework is not initialized yet, it will be initialized.
221 *
222 * @param string $content The content
223 * @param array $configuration The TS configuration array
224 * @return string $content The processed content
225 * @api
226 */
227 public function run($content, $configuration) {
228 //var_dump(Tx_Extbase_Utility_Extension::createAutoloadRegistryForExtension('extbase', t3lib_extMgm::extPath('extbase'), array(
229 // 'tx_extbase_basetestcase' => '$extensionClassesPath . \'../Tests/BaseTestCase.php\''
230 //)));
231 //die("autoload registry");
232
233 $this->initialize($configuration);
234
235 $requestHandlerResolver = $this->objectManager->get('Tx_Extbase_MVC_RequestHandlerResolver');
236 $requestHandler = $requestHandlerResolver->resolveRequestHandler();
237
238 $response = $requestHandler->handleRequest();
239
240 // If response is NULL after handling the request we need to stop
241 // This happens for instance, when a USER object was converted to a USER_INT
242 // @see Tx_Extbase_MVC_Web_FrontendRequestHandler::handleRequest()
243 if ($response === NULL) {
244 $this->reflectionService->shutdown();
245 return;
246 }
247 if (count($response->getAdditionalHeaderData()) > 0) {
248 $GLOBALS['TSFE']->additionalHeaderData[] = implode(chr(10), $response->getAdditionalHeaderData());
249 }
250 $response->sendHeaders();
251 $content = $response->getContent();
252
253 $this->resetSingletons();
254 return $content;
255 }
256
257 /**
258 * Resets global singletons for the next plugin
259 *
260 * @return void
261 */
262 protected function resetSingletons() {
263 $this->persistenceManager->persistAll();
264 $this->objectManager->get('Tx_Extbase_MVC_Controller_FlashMessages')->persist();
265 $this->reflectionService->shutdown();
266 }
267
268 /**
269 * This method forwards the call to run(). This method is invoked by the mod.php
270 * function of TYPO3.
271 *
272 * @return TRUE
273 * @see run()
274 **/
275 public function callModule($moduleName) {
276 if (!isset($GLOBALS['TBE_MODULES']['_configuration'][$moduleName])) return FALSE;
277 $configuration = $GLOBALS['TBE_MODULES']['_configuration'][$moduleName];
278
279 // Check permissions and exit if the user has no permission for entry
280 $GLOBALS['BE_USER']->modAccess($configuration, TRUE);
281 if (t3lib_div::_GP('id')) {
282 // Check page access
283 $id = t3lib_div::_GP('id');
284 $permClause = $GLOBALS['BE_USER']->getPagePermsClause(TRUE);
285 $access = is_array(t3lib_BEfunc::readPageAccess($id, $permClause));
286 if (!$access) {
287 t3lib_BEfunc::typo3PrintError('No Access', 'You don\'t have access to this page', 0);
288 }
289 }
290
291 // BACK_PATH is the path from the typo3/ directory from within the
292 // directory containing the controller file. We are using mod.php dispatcher
293 // and thus we are already within typo3/ because we call typo3/mod.php
294 $GLOBALS['BACK_PATH'] = '';
295
296 $this->run('', $configuration);
297
298 return TRUE;
299 }
300 }
301 ?>