EXTMVC:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / class.tx_extmvc_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 // TODO these statements become obsolete with the new autoloader -> remove them
26
27 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/TX_EXTMVC_Request.php');
28 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Web/TX_EXTMVC_Web_Request.php');
29 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/TX_EXTMVC_Response.php');
30 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Web/TX_EXTMVC_Web_Response.php');
31 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Configuration/TX_EXTMVC_Configuration_Manager.php');
32 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_AbstractController.php');
33 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_ActionController.php');
34 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_Arguments.php');
35 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_Argument.php');
36 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/View/TX_EXTMVC_View_AbstractView.php');
37 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/TX_EXTMVC_Persistence_Session.php');
38 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/Mapper/TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper.php');
39
40 /**
41 * Creates a request an dispatches it to the controller which was specified by TS Setup, Flexform,
42 * or Extension Configuration (ExtConf), and returns the content to the v4 framework.
43 *
44 * @version $Id:$
45 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
46 */
47 class TX_EXTMVC_Dispatcher {
48
49 /**
50 * @var TX_EXTMVC_Configuration_Manager A reference to the configuration manager
51 */
52 protected $configurationManager;
53
54 /**
55 * @var TX_EXTMVC_Web_RequestBuilder
56 */
57 protected $requestBuilder;
58
59 /**
60 * @var ArrayObject The raw GET parameters
61 */
62 protected $getParameters;
63
64 /**
65 * @var ArrayObject The raw POST parameters
66 */
67 protected $postParameters;
68
69 /**
70 * @var array An array of registered classes (class files with path)
71 */
72 protected $registeredClassNames;
73
74 /**
75 * Constructs this dispatcher
76 *
77 */
78 public function __construct() {
79 spl_autoload_register(array($this, 'autoLoadClasses'));
80 }
81
82 /**
83 * Creates a request an dispatches it to a controller.
84 *
85 * @param String $content The content
86 * @param array|NULL $configuration The TS configuration array
87 * @uses t3lib_div::_GET()
88 * @uses t3lib_div::makeInstance()
89 * @uses t3lib_div::GParrayMerged()
90 * @uses t3lib_div::getIndpEnv()
91 * @return String $content The processed content
92 */
93 public function dispatch($content, $configuration) {
94
95 // TODO Remove debug statement
96 // $start_time = microtime(TRUE);
97
98 $parameters = t3lib_div::_GET();
99 $extensionKey = isset($parameters['extension']) ? stripslashes($parameters['extension']) : $configuration['extension'];
100 $controllerName = isset($parameters['controller']) ? stripslashes($parameters['controller']) : $configuration['controller'];
101 $actionName = isset($parameters['action']) ? stripslashes($parameters['action']) : $configuration['action'];
102
103 $request = t3lib_div::makeInstance('TX_EXTMVC_Web_Request');
104 $request->setControllerExtensionKey($extensionKey);
105 $request->setControllerName($controllerName);
106 $request->setControllerActionName($actionName);
107
108 $controllerObjectName = $request->getControllerObjectName();
109 $controller = t3lib_div::makeInstance($controllerObjectName);
110
111 if (!$controller instanceof TX_EXTMVC_Controller_AbstractController) throw new TX_EXTMVC_Exception_InvalidController('Invalid controller "' . $controllerObjectName . '". The controller must be a valid request handling controller.', 1202921619);
112
113 if (!$controller->isCachableAction($actionName) && $this->cObj->getUserObjectType() === tslib_cObj::OBJECTTYPE_USER) {
114 // FIXME Caching does nort work because it's by default a USER object, so the dispatcher is never called
115 // SK: does caching already work?
116 $this->cObj->convertToUserIntObject();
117 return $content;
118 }
119
120 $arguments = t3lib_div::makeInstance('TX_EXTMVC_Controller_Arguments');
121 // SK: strtolower($extensionKey) is wrong I think, as all underscores need to be removed as well.
122 // SK: Example: tt_news -> tx_ttnews
123 foreach (t3lib_div::GParrayMerged('tx_' . strtolower($extensionKey)) as $key => $value) {
124 // SK: This argument is passed attached to the Request - so do NOT use TX_EXTMVC_Controller_Argument or TX_EXTMVC_Controller_Arguments here.
125 // SK: Instead, just add an argument by using $request->addArgument('key', 'value');
126 // SK: The real argument mapping and validation then happens in the AbstractController.
127 $argument = new TX_EXTMVC_Controller_Argument($key, 'Raw');
128 $argument->setValue($value);
129 $arguments->addArgument($argument);
130 }
131 // SK: as shown above, this needs to be changed
132 $request->setArguments($arguments);
133 $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
134 $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
135 $response = t3lib_div::makeInstance('TX_EXTMVC_Web_Response');
136
137 $configurationSources = array();
138 $configurationSources[] = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Source_TS');
139 if (!empty($this->cObj->data['pi_flexform'])) {
140 $configurationSource = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Source_FlexForm');
141 $configurationSource->setFlexFormContent($this->cObj->data['pi_flexform']);
142 $configurationSources[] = $configurationSource;
143 }
144 $configurationManager = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Manager', $configurationSources);
145 $configurationManager->loadGlobalSettings($extensionKey);
146 $configurationManager = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Manager');
147 $settings = $configurationManager->getSettings($extensionKey);
148 $controller->injectSettings($settings);
149
150 $session = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Session');
151 try {
152 $controller->processRequest($request, $response);
153 } catch (TX_EXTMVC_Exception_StopAction $ignoredException) {
154 }
155 $session->commit();
156 $session->clear();
157
158 $GLOBALS['TSFE']->additionalHeaderData[$request->getControllerExtensionKey()] = implode("\n", $response->getAdditionalHeaderTags());
159
160 // TODO Remove debug statements
161 // $end_time = microtime(TRUE);
162 // debug($end_time - $start_time, -1);
163
164 // SK: Handle $response->getStatus()
165 // SK: Call sendHeaders() on the response
166 return $response->getContent();
167 }
168
169 /**
170 * Loads php files containing classes or interfaces found in the classes directory of
171 * an extension.
172 *
173 * @param string $className: Name of the class/interface to load
174 * @uses t3lib_extMgm::extPath()
175 * @return void
176 */
177 // TODO Remove autoloader as soon as we do not need it anymore
178 protected function autoLoadClasses($className) {
179 if (empty($this->registeredClassNames[$className])) {
180 $classNameParts = explode('_', $className);
181 if ($classNameParts[0] === 'ux') {
182 array_shift($classNameParts);
183 }
184 if (count($classNameParts) > 2 && $classNameParts[0] === 'TX') {
185 $classFilePathAndName = t3lib_extMgm::extPath(strtolower($classNameParts[1])) . 'Classes/';
186 $classFilePathAndName .= implode(array_slice($classNameParts, 2, -1), '/') . '/';
187 $classFilePathAndName .= implode('_', $classNameParts) . '.php';
188 }
189 if (isset($classFilePathAndName) && file_exists($classFilePathAndName)) {
190 require_once($classFilePathAndName);
191 $this->registeredClassNames[$className] = $classFilePathAndName;
192 }
193 }
194 }
195
196 }
197 ?>