Extbase:
[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 * @package Extbase
30 * @subpackage extbase
31 * @version $ID:$
32 */
33 class Tx_Extbase_Dispatcher {
34
35 /**
36 * Creates a request an dispatches it to a controller.
37 *
38 * @param string $content The content
39 * @param array|NULL $configuration The TS configuration array
40 * @return string $content The processed content
41 */
42 public function dispatch($content, $configuration) {
43 if (!is_array($configuration)) {
44 t3lib_div::sysLog('Extbase was not able to dispatch the request. No configuration.', 'extbase', t3lib_div::SYSLOG_SEVERITY_ERROR);
45 return $content;
46 }
47 $requestBuilder = t3lib_div::makeInstance('Tx_Extbase_MVC_Web_RequestBuilder');
48 $request = $requestBuilder->initialize($configuration);
49 $request = $requestBuilder->build();
50 $response = t3lib_div::makeInstance('Tx_Extbase_MVC_Web_Response');
51 $persistenceSession = t3lib_div::makeInstance('Tx_Extbase_Persistence_Session'); // singleton
52
53 $dispatchLoopCount = 0;
54 while (!$request->isDispatched()) {
55 if ($dispatchLoopCount++ > 99) throw new TxExtbase_MVC_Exception_InfiniteLoop('Could not ultimately dispatch the request after ' . $dispatchLoopCount . ' iterations.', 1217839467);
56 $controller = $this->getPreparedController($request);
57 try {
58 $controller->processRequest($request, $response);
59 } catch (Tx_Extbase_Exception_StopAction $ignoredException) {
60 } catch (Tx_Extbase_Exception_InvalidArgumentValue $exception) {
61 $persistenceSession->clear();
62 return '';
63 }
64 }
65
66 $persistenceSession->commit();
67 $persistenceSession->clear();
68 if (count($response->getAdditionalHeaderData()) > 0) {
69 $GLOBALS['TSFE']->additionalHeaderData[$request->getControllerExtensionName()] = implode("\n", $response->getAdditionalHeaderData());
70 }
71 // TODO Handle $response->getStatus()
72 $response->sendHeaders();
73 return $response->getContent();
74 }
75
76 /**
77 * Builds and returns a controller
78 *
79 * @param Tx_Extbase_MVC_Web_Request $request
80 * @return Tx_Extbase_MVC_Controller_ControllerInterface The prepared controller
81 */
82 protected function getPreparedController(Tx_Extbase_MVC_Web_Request $request) {
83 $controllerObjectName = $request->getControllerObjectName();
84 $controller = t3lib_div::makeInstance($controllerObjectName);
85 if (!$controller instanceof Tx_Extbase_MVC_Controller_ControllerInterface) {
86 throw new Tx_Extbase_Exception_InvalidController('Invalid controller "' . $request->getControllerObjectName() . '". The controller must implement the Tx_Extbase_MVC_Controller_ControllerInterface.', 1202921619);
87 }
88 $propertyMapper = t3lib_div::makeInstance('Tx_Extbase_Property_Mapper');
89 $controller->injectPropertyMapper($propertyMapper);
90 $controller->injectSettings($this->getSettings($request));
91 $URIHelper = t3lib_div::makeInstance('Tx_Extbase_MVC_View_Helper_URIHelper');
92 $URIHelper->setRequest($request);
93 $controller->injectURIHelper($URIHelper);
94 $reflectionService = t3lib_div::makeInstance('Tx_Extbase_Reflection_Service');
95 $validatorResolver = t3lib_div::makeInstance('Tx_Extbase_Validation_ValidatorResolver');
96 $validatorResolver->injectReflectionService($reflectionService);
97 $controller->injectValidatorResolver($validatorResolver);
98 $controller->injectReflectionService($reflectionService);
99 return $controller;
100 }
101
102 /**
103 * Builds the settings by overlaying TS Setup with FlexForm values of the extension
104 * and returns them as a plain array (with no trailing dots).
105 *
106 * @param Tx_Extbase_MVC_Web_Request $request
107 * @return array The settings array
108 */
109 protected function getSettings(Tx_Extbase_MVC_Web_Request $request) {
110 $extensionName = $request->getControllerExtensionName();
111 $configurationSources = array();
112 $configurationSources[] = t3lib_div::makeInstance('Tx_Extbase_Configuration_Source_TypoScriptSource');
113 if (!empty($this->cObj->data['pi_flexform'])) {
114 $configurationSource = t3lib_div::makeInstance('Tx_Extbase_Configuration_Source_FlexFormSource');
115 $configurationSource->setFlexFormContent($this->cObj->data['pi_flexform']);
116 $configurationSources[] = $configurationSource;
117 }
118 $configurationManager = t3lib_div::makeInstance('Tx_Extbase_Configuration_Manager', $configurationSources);
119 return $configurationManager->getSettings($extensionName);
120 }
121
122 /**
123 * Loads php files containing classes or interfaces found in the classes directory of
124 * an extension.
125 *
126 * @param string $className: Name of the class/interface to load
127 * @uses t3lib_extMgm::extPath()
128 * @return void
129 */
130 public static function autoloadClass($className) {
131 $classNameParts = explode('_', $className);
132 $extensionKey = t3lib_div::camelCaseToLowerCaseUnderscored($classNameParts[1]);
133 if (t3lib_extMgm::isLoaded($extensionKey)) {
134 if ($classNameParts[0] === 'ux') {
135 array_shift($classNameParts);
136 }
137 $className = implode('_', $classNameParts);
138 if (count($classNameParts) > 2 && $classNameParts[0] === 'Tx') {
139 $classFilePathAndName = t3lib_extMgm::extPath(t3lib_div::camelCaseToLowerCaseUnderscored($classNameParts[1])) . 'Classes/';
140 $classFilePathAndName .= implode(array_slice($classNameParts, 2, -1), '/') . '/';
141 $classFilePathAndName .= array_pop($classNameParts) . '.php';
142 }
143 if (isset($classFilePathAndName) && file_exists($classFilePathAndName)) {
144 require_once($classFilePathAndName);
145 }
146 }
147 }
148
149 }
150 ?>