ExtBase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / class.tx_extbase_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 TYPO3
30 * @subpackage extbase
31 * @version $ID:$
32 */
33 class Tx_ExtBase_Dispatcher {
34
35 /**
36 * @var Tx_ExtBase_Configuration_Manager A reference to the configuration manager
37 */
38 protected $configurationManager;
39
40 /**
41 * @var Tx_ExtBase_MVC_Web_RequestBuilder
42 */
43 protected $requestBuilder;
44
45 /**
46 * @var ArrayObject The raw GET parameters
47 */
48 protected $getParameters;
49
50 /**
51 * @var ArrayObject The raw POST parameters
52 */
53 protected $postParameters;
54
55 /**
56 * @var array An array of registered classes (class files with path)
57 */
58 protected $registeredClassNames;
59
60 /**
61 * Constructs this dispatcher
62 *
63 */
64 public function __construct() {
65 spl_autoload_register(array($this, 'autoLoadClasses'));
66 }
67
68 /**
69 * Creates a request an dispatches it to a controller.
70 *
71 * @param String $content The content
72 * @param array|NULL $configuration The TS configuration array
73 * @uses t3lib_div::_GET()
74 * @uses t3lib_div::makeInstance()
75 * @uses t3lib_div::GParrayMerged()
76 * @uses t3lib_div::getIndpEnv()
77 * @return String $content The processed content
78 */
79 public function dispatch($content, $configuration) {
80 // TODO Remove debug statement
81 // $start_time = microtime(TRUE);
82
83 $parameters = t3lib_div::_GET();
84 $extensionName = $configuration['extension'];
85 $controllerName = $configuration['controller'];
86 $allowedActions = t3lib_div::trimExplode(',', $configuration['allowedActions']);
87 if (isset($parameters['action']) && in_array($parameters['action'], $allowedActions)) {
88 $actionName = stripslashes($parameters['action']);
89 } else {
90 $actionName = $configuration['action'];
91 }
92 if (empty($extensionName) || empty($controllerName) || empty($allowedActions)) {
93 throw new Exception('Could not dispatch the request. Please configure your plugin in the TS Setup.', 1237879677);
94 }
95
96 $request = $this->buildRequest($extensionName, $controllerName, $actionName);
97 $controller = t3lib_div::makeInstance($request->getControllerObjectName());
98 if (!$controller instanceof Tx_ExtBase_MVC_Controller_ControllerInterface) {
99 throw new Tx_ExtBase_Exception_InvalidController('Invalid controller "' . $request->getControllerObjectName() . '". The controller must be a valid request handling controller.', 1202921619);
100 }
101
102 $arguments = t3lib_div::makeInstance('Tx_ExtBase_MVC_Controller_Arguments');
103 // TODO Namespace for controller
104 foreach (t3lib_div::GParrayMerged('tx_' . strtolower($extensionName) . '_' . strtolower($controllerName)) as $key => $value) {
105 $request->setArgument($key, $value);
106 }
107
108 $response = t3lib_div::makeInstance('Tx_ExtBase_MVC_Web_Response');
109 $controller->injectSettings($this->getSettings($extensionName));
110
111 $persistenceSession = t3lib_div::makeInstance('Tx_ExtBase_Persistence_Session');
112 try {
113 $controller->processRequest($request, $response);
114 } catch (Tx_ExtBase_Exception_StopAction $ignoredException) {
115 }
116 $persistenceSession->commit();
117 $persistenceSession->clear();
118
119 if (count($response->getAdditionalHeaderData()) > 0) {
120 $GLOBALS['TSFE']->additionalHeaderData[$request->getExtensionName()] = implode("\n", $response->getAdditionalHeaderData());
121 }
122 // TODO Handle $response->getStatus()
123 // SK: Call sendHeaders() on the response
124
125 // TODO Remove debug statements
126 // $end_time = microtime(TRUE);
127 // debug($end_time - $start_time, -1);
128
129 return $response->getContent();
130 }
131
132 protected function getSettings($extensionName) {
133 $configurationSources = array();
134 $configurationSources[] = t3lib_div::makeInstance('Tx_ExtBase_Configuration_Source_TypoScriptSource');
135 if (!empty($this->cObj->data['pi_flexform'])) {
136 $configurationSource = t3lib_div::makeInstance('Tx_ExtBase_Configuration_Source_FlexFormSource');
137 $configurationSource->setFlexFormContent($this->cObj->data['pi_flexform']);
138 $configurationSources[] = $configurationSource;
139 }
140 $configurationManager = t3lib_div::makeInstance('Tx_ExtBase_Configuration_Manager', $configurationSources);
141 $configurationManager->loadGlobalSettings($extensionName);
142 return $configurationManager->getSettings($extensionName);
143 }
144
145 protected function buildRequest($extensionName, $controllerName, $actionName) {
146 $request = t3lib_div::makeInstance('Tx_ExtBase_MVC_Web_Request');
147 $request->setExtensionName($extensionName);
148 $request->setControllerName($controllerName);
149 $request->setControllerActionName($actionName);
150 $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
151 $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
152 return $request;
153 }
154
155 /**
156 * Loads php files containing classes or interfaces found in the classes directory of
157 * an extension.
158 *
159 * @param string $className: Name of the class/interface to load
160 * @uses t3lib_extMgm::extPath()
161 * @return void
162 */
163 // TODO Remove autoloader as soon as we do not need it anymore
164 public function autoLoadClasses($className) {
165 if (empty($this->registeredClassNames[$className])) {
166 $classNameParts = explode('_', $className);
167 if ($classNameParts[0] === 'ux') {
168 array_shift($classNameParts);
169 }
170 if (count($classNameParts) > 2 && $classNameParts[0] === 'Tx') {
171 $classFilePathAndName = t3lib_extMgm::extPath(strtolower($classNameParts[1])) . 'Classes/';
172 $classFilePathAndName .= implode(array_slice($classNameParts, 2, -1), '/') . '/';
173 $classFilePathAndName .= array_pop($classNameParts) . '.php';
174 }
175 if (isset($classFilePathAndName) && file_exists($classFilePathAndName)) {
176 require_once($classFilePathAndName);
177 $this->registeredClassNames[$className] = $classFilePathAndName;
178 }
179 }
180 }
181
182 }
183 ?>