[!!!][~TASK] Extbase (Utility): Moved configureDispatcher() and registerPlugin()...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Dispatcher.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 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 in the frontend.
30 *
31 * @package Extbase
32 * @version $ID:$
33 */
34 class Tx_Extbase_Dispatcher {
35
36 /**
37 * @var Tx_Extbase_Configuration_Manager
38 */
39 protected $configurationManager;
40
41 /**
42 * @var Tx_Extbase_Reflection_Service
43 */
44 protected static $reflectionService;
45
46 /**
47 * @var Tx_Extbase_Persistence_Manager
48 */
49 private static $persistenceManager;
50
51 /**
52 * The configuration for the Extbase framework
53 * @var array
54 */
55 private static $extbaseFrameworkConfiguration;
56
57
58 /**
59 * Constructs this Dispatcher and registers the autoloader
60 */
61 public function __construct() {
62 spl_autoload_register(array($this, 'autoloadClass'));
63 }
64
65 /**
66 * Creates a request an dispatches it to a controller.
67 *
68 * @param string $content The content
69 * @param array|NULL $configuration The TS configuration array
70 * @return string $content The processed content
71 */
72 public function dispatch($content, $configuration) {
73 // FIXME Remove the next lines. These are only there to generate the ext_autoload.php file
74 //$extutil = new Tx_Extbase_Utility_Extension;
75 //$extutil->createAutoloadRegistryForExtension('extbase', t3lib_extMgm::extPath('extbase'));
76 //$extutil->createAutoloadRegistryForExtension('fluid', t3lib_extMgm::extPath('fluid'));
77
78 if (!is_array($configuration)) {
79 t3lib_div::sysLog('Extbase was not able to dispatch the request. No configuration.', 'extbase', t3lib_div::SYSLOG_SEVERITY_ERROR);
80 return $content;
81 }
82 $this->initializeConfigurationManagerAndFrameworkConfiguration($configuration);
83
84 $requestBuilder = t3lib_div::makeInstance('Tx_Extbase_MVC_Web_RequestBuilder');
85 $request = $requestBuilder->initialize($configuration);
86 $request = $requestBuilder->build();
87 $response = t3lib_div::makeInstance('Tx_Extbase_MVC_Web_Response');
88
89 $persistenceManager = self::getPersistenceManager();
90
91 $dispatchLoopCount = 0;
92 while (!$request->isDispatched()) {
93 if ($dispatchLoopCount++ > 99) throw new Tx_Extbase_MVC_Exception_InfiniteLoop('Could not ultimately dispatch the request after ' . $dispatchLoopCount . ' iterations.', 1217839467);
94 $controller = $this->getPreparedController($request);
95 try {
96 $controller->processRequest($request, $response);
97 } catch (Tx_Extbase_MVC_Exception_StopAction $ignoredException) {
98 } catch (Tx_Extbase_MVC_Exception_InvalidArgumentValue $exception) {
99 return '';
100 }
101 }
102
103 $persistenceManager->persistAll();
104 self::$reflectionService->shutdown();
105 if (count($response->getAdditionalHeaderData()) > 0) {
106 $GLOBALS['TSFE']->additionalHeaderData[$request->getControllerExtensionName()] = implode("\n", $response->getAdditionalHeaderData());
107 }
108 $response->sendHeaders();
109 return $response->getContent();
110 }
111
112 /**
113 * Initializes the configuration manager and the Extbase settings
114 *
115 * @param $configuration The current incoming configuration
116 * @return void
117 */
118 protected function initializeConfigurationManagerAndFrameworkConfiguration($configuration) {
119 $configurationSources = array();
120 $configurationSources[] = t3lib_div::makeInstance('Tx_Extbase_Configuration_Source_TypoScriptSource');
121 if (!empty($this->cObj->data['pi_flexform'])) {
122 $configurationSource = t3lib_div::makeInstance('Tx_Extbase_Configuration_Source_FlexFormSource');
123 $configurationSource->setFlexFormContent($this->cObj->data['pi_flexform']);
124 $configurationSources[] = $configurationSource;
125 }
126 $this->configurationManager = t3lib_div::makeInstance('Tx_Extbase_Configuration_Manager', $configurationSources);
127 self::$extbaseFrameworkConfiguration = $this->configurationManager->getFrameworkConfiguration($configuration, $this->cObj);
128 }
129
130 /**
131 * Builds and returns a controller
132 *
133 * @param Tx_Extbase_MVC_Web_Request $request
134 * @return Tx_Extbase_MVC_Controller_ControllerInterface The prepared controller
135 */
136 protected function getPreparedController(Tx_Extbase_MVC_Web_Request $request) {
137 $controllerObjectName = $request->getControllerObjectName();
138 $controller = t3lib_div::makeInstance($controllerObjectName);
139 if (!$controller instanceof Tx_Extbase_MVC_Controller_ControllerInterface) {
140 throw new Tx_Extbase_MVC_Exception_InvalidController('Invalid controller "' . $request->getControllerObjectName() . '". The controller must implement the Tx_Extbase_MVC_Controller_ControllerInterface.', 1202921619);
141 }
142 self::$reflectionService = t3lib_div::makeInstance('Tx_Extbase_Reflection_Service');
143 $propertyMapper = t3lib_div::makeInstance('Tx_Extbase_Property_Mapper');
144 $propertyMapper->injectReflectionService(self::$reflectionService);
145 $controller->injectPropertyMapper($propertyMapper);
146 $controller->injectSettings($this->configurationManager->getSettings($request->getControllerExtensionName()));
147 $cacheManager = t3lib_div::makeInstance('t3lib_cache_Manager');
148 try {
149 self::$reflectionService->setCache($cacheManager->getCache('cache_extbase_reflection'));
150 } catch (t3lib_cache_exception_NoSuchCache $exception) {
151 $GLOBALS['typo3CacheFactory']->create(
152 'cache_extbase_reflection',
153 't3lib_cache_frontend_VariableFrontend',
154 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['backend'],
155 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['options']
156 );
157 self::$reflectionService->setCache($cacheManager->getCache('cache_extbase_reflection'));
158 }
159 if (!self::$reflectionService->isInitialized()) {
160 self::$reflectionService->initialize();
161 }
162 $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_Manager');
163 $validatorResolver = t3lib_div::makeInstance('Tx_Extbase_Validation_ValidatorResolver');
164 $validatorResolver->injectObjectManager($objectManager);
165 $validatorResolver->injectReflectionService(self::$reflectionService);
166 $controller->injectValidatorResolver($validatorResolver);
167 $controller->injectReflectionService(self::$reflectionService);
168 $controller->injectObjectManager($objectManager);
169 return $controller;
170 }
171
172 /**
173 * This function prepares and returns the Persistance Manager
174 *
175 * @return Tx_Extbase_Persistence_Manager A (singleton) instance of the Persistence Manager
176 */
177 public static function getPersistenceManager() {
178 if (self::$persistenceManager === NULL) {
179 $dataMapper = t3lib_div::makeInstance('Tx_Extbase_Persistence_Mapper_DataMapper'); // singleton
180
181 $storageBackend = t3lib_div::makeInstance('Tx_Extbase_Persistence_Storage_Typo3DbBackend', $GLOBALS['TYPO3_DB']); // singleton
182 $storageBackend->injectDataMapper($dataMapper);
183
184 $qomFactory = t3lib_div::makeInstance('Tx_Extbase_Persistence_QOM_QueryObjectModelFactory', $storageBackend, $dataMapper);
185
186 $persistenceSession = t3lib_div::makeInstance('Tx_Extbase_Persistence_Session'); // singleton
187
188 $persistenceBackend = t3lib_div::makeInstance('Tx_Extbase_Persistence_Backend', $persistenceSession, $storageBackend); // singleton
189 $persistenceBackend->injectDataMapper($dataMapper);
190 $persistenceBackend->injectIdentityMap(t3lib_div::makeInstance('Tx_Extbase_Persistence_IdentityMap'));
191 $persistenceBackend->injectQueryFactory(t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryFactory'));
192 $persistenceBackend->injectQOMFactory($qomFactory);
193 $persistenceBackend->injectValueFactory(t3lib_div::makeInstance('Tx_Extbase_Persistence_ValueFactory'));
194
195 $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_Manager'); // singleton
196
197 $persistenceManager = t3lib_div::makeInstance('Tx_Extbase_Persistence_Manager'); // singleton
198 $persistenceManager->injectBackend($persistenceBackend);
199 $persistenceManager->injectSession($persistenceSession);
200 $persistenceManager->injectObjectManager($objectManager);
201
202 self::$persistenceManager = $persistenceManager;
203 }
204
205 return self::$persistenceManager;
206 }
207
208 /**
209 * This function returns the settings of Extbase
210 *
211 * @return array The settings
212 */
213 public static function getExtbaseFrameworkConfiguration() {
214 return self::$extbaseFrameworkConfiguration;
215 }
216
217 /**
218 * Loads php files containing classes or interfaces found in the classes directory of
219 * an extension.
220 *
221 * @param string $className: Name of the class/interface to load
222 * @uses t3lib_extMgm::extPath()
223 * @return void
224 */
225 public static function autoloadClass($className) {
226 // TODO Remove debug code
227 // TODO Make a registry for Extbase classes
228 //$starttime = microtime(true);
229 $classNameParts = explode('_', $className, 3);
230 $extensionKey = Tx_Extbase_Utility_Extension::convertCamelCaseToLowerCaseUnderscored($classNameParts[1]);
231 if (t3lib_extMgm::isLoaded($extensionKey)) {
232 $classFilePathAndName = t3lib_extMgm::extPath($extensionKey) . 'Classes/' . strtr($classNameParts[2], '_', '/') . '.php';
233 if (file_exists($classFilePathAndName)) {
234 require($classFilePathAndName);
235 }
236 }
237 //$endtime = microtime(true);
238 //debug(($endtime - $starttime) * 10000, $className);
239 }
240 }
241 ?>