[+BUGFIX] Extbase (Persistence): Extbase still used PHPs current() on some QueryResul...
[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->initializeExtensions();
96 $this->initializeCache();
97 $this->initializeReflection();
98 // $this->initializeObjectContainer();
99 $this->initializePersistence();
100 $this->initializeBackwardsCompatibility();
101 // $this->initializeSession();
102 // $this->initializeLocale();
103 $this->isInitialized = TRUE;
104 }
105
106 /**
107 * Initializes the autoload mechanism of Extbase. This is supplement to the core autoloader.
108 *
109 * @return void
110 * @see initialize()
111 */
112 protected function initializeClassLoader() {
113 if (!class_exists('Tx_Extbase_Utility_ClassLoader', FALSE)) {
114 require(t3lib_extmgm::extPath('extbase') . 'Classes/Utility/ClassLoader.php');
115 }
116
117 $classLoader = new Tx_Extbase_Utility_ClassLoader();
118 spl_autoload_register(array($classLoader, 'loadClass'));
119 }
120
121 /**
122 * Initializes the Object framework.
123 *
124 * @return void
125 * @see initialize()
126 */
127 protected function initializeObjectManager() {
128 $this->objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
129 // $this->objectManager->injectClassLoader($this->classLoader);
130 // $this->objectManager->injectConfigurationManager($this->configurationManager);
131 // $this->objectManager->setContext($this->context);
132
133 // $this->objectManager->initialize();
134 }
135
136 /**
137 * Initializes the Object framework.
138 *
139 * @return void
140 * @see initialize()
141 */
142 public function initializeConfiguration($configuration) {
143 $this->configurationManager = $this->objectManager->get('Tx_Extbase_Configuration_ConfigurationManagerInterface');
144 $contentObject = isset($this->cObj) ? $this->cObj : t3lib_div::makeInstance('tslib_cObj');
145 $this->configurationManager->setContentObject($contentObject);
146 $this->configurationManager->setConfiguration($configuration);
147 }
148
149 /**
150 * Initializes the cache framework
151 *
152 * @return void
153 * @see initialize()
154 */
155 protected function initializeCache() {
156 t3lib_cache::initializeCachingFramework();
157 $this->cacheManager = $GLOBALS['typo3CacheManager'];
158 try {
159 $this->cacheManager->getCache('cache_extbase_reflection');
160 } catch (t3lib_cache_exception_NoSuchCache $exception) {
161 $GLOBALS['typo3CacheFactory']->create(
162 'cache_extbase_reflection',
163 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['frontend'],
164 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['backend'],
165 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['options']
166 );
167 }
168 }
169
170 /**
171 * Initializes the Reflection Service
172 *
173 * @return void
174 * @see initialize()
175 */
176 protected function initializeReflection() {
177 $this->reflectionService = $this->objectManager->get('Tx_Extbase_Reflection_Service');
178 $this->reflectionService->setDataCache($this->cacheManager->getCache('cache_extbase_reflection'));
179 $this->reflectionService->initialize();
180 }
181
182 /**
183 * Initializes the persistence framework
184 *
185 * @return void
186 * @see initialize()
187 */
188 public function initializePersistence() {
189 $this->persistenceManager = $this->objectManager->get('Tx_Extbase_Persistence_Manager'); // singleton
190 }
191
192 /**
193 * Initializes the backwards compatibility. This is necessary because the
194 * old Dispatcher provided several static methods.
195 *
196 * @return void
197 * @see initialize()
198 */
199 protected function initializeBackwardsCompatibility() {
200 $dispatcher = t3lib_div::makeInstance('Tx_Extbase_Dispatcher');
201 $dispatcher->injectConfigurationManager($this->configurationManager);
202 $dispatcher->injectPersistenceManager($this->persistenceManager);
203 }
204
205 /**
206 * Runs the the Extbase Framework by resolving an appropriate Request Handler and passing control to it.
207 * If the Framework is not initialized yet, it will be initialized.
208 *
209 * @param string $content The content
210 * @param array $configuration The TS configuration array
211 * @return string $content The processed content
212 * @api
213 */
214 public function run($content, $configuration) {
215 //var_dump(Tx_Extbase_Utility_Extension::createAutoloadRegistryForExtension('extbase', t3lib_extMgm::extPath('extbase'), array(
216 // 'tx_extbase_basetestcase' => '$extensionClassesPath . \'../Tests/BaseTestCase.php\''
217 //)));
218 //die("autoload registry");
219
220 $this->initialize($configuration);
221
222 $requestHandlerResolver = $this->objectManager->get('Tx_Extbase_MVC_RequestHandlerResolver');
223 $requestHandler = $requestHandlerResolver->resolveRequestHandler();
224 $requestHandler->setContentObject($this->cObj);
225
226 $response = $requestHandler->handleRequest();
227
228 $this->persistenceManager->persistAll();
229 // $this->objectManager->shutdown();
230 if (count($response->getAdditionalHeaderData()) > 0) {
231 $GLOBALS['TSFE']->additionalHeaderData[] = implode('', $response->getAdditionalHeaderData());
232 }
233 $response->sendHeaders();
234 $content = $response->getContent();
235 $this->resetSingletons();
236 return $content;
237 }
238
239 protected function resetSingletons() {
240 $this->reflectionService->shutdown();
241 $this->objectManager->get('Tx_Extbase_MVC_Controller_FlashMessages')->reset();
242 }
243
244 /**
245 * This method forwards the call to run(). This method is invoked by the mod.php
246 * function of TYPO3.
247 *
248 * @return TRUE
249 * @see run()
250 **/
251 public function callModule($moduleName) {
252 $configuration = array();
253 $configuration['module.']['tx_extbase.']['moduleName'] = $moduleName;
254 $this->run('', $configuration);
255 return TRUE;
256 }
257 }
258 ?>