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 /**
26 * Creates a request an dispatches it to the controller which was specified by TS Setup, Flexform,
27 * or Extension Configuration (ExtConf), and returns the content to the v4 framework.
28 *
29 * @package TYPO3
30 * @subpackage extmvc
31 * @version $ID:$
32 */
33 class TX_EXTMVC_Dispatcher {
34
35 /**
36 * @var TX_EXTMVC_Configuration_Manager A reference to the configuration manager
37 */
38 protected $configurationManager;
39
40 /**
41 * @var TX_EXTMVC_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
81 // TODO Remove debug statement
82 // $start_time = microtime(TRUE);
83
84 $parameters = t3lib_div::_GET();
85 // TODO We should refuse to let the client change the plugins behaviour by adding "&action=show" to the url
86 $extensionKey = isset($parameters['extension']) ? stripslashes($parameters['extension']) : $configuration['extension'];
87 $controllerName = isset($parameters['controller']) ? stripslashes($parameters['controller']) : $configuration['controller'];
88 $actionName = isset($parameters['action']) ? stripslashes($parameters['action']) : $configuration['action'];
89
90 $request = t3lib_div::makeInstance('TX_EXTMVC_Web_Request');
91 $request->setControllerExtensionKey($extensionKey);
92 $request->setControllerName($controllerName);
93 $request->setControllerActionName($actionName);
94
95 $controllerObjectName = $request->getControllerObjectName();
96 $controller = t3lib_div::makeInstance($controllerObjectName);
97
98 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);
99
100 if (!$controller->isCachableAction($actionName) && $this->cObj->getUserObjectType() === tslib_cObj::OBJECTTYPE_USER) {
101 // FIXME Caching does nort work because it's by default a USER object, so the dispatcher is never called
102 // SK: does caching already work?
103 $this->cObj->convertToUserIntObject();
104 return $content;
105 }
106
107 $arguments = t3lib_div::makeInstance('TX_EXTMVC_Controller_Arguments');
108 // SK: strtolower($extensionKey) is wrong I think, as all underscores need to be removed as well.
109 // SK: Example: tt_news -> tx_ttnews
110 foreach (t3lib_div::GParrayMerged('tx_' . strtolower($extensionKey)) as $key => $value) {
111 $request->setArgument($key, $value);
112 }
113 $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
114 $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
115 $response = t3lib_div::makeInstance('TX_EXTMVC_Web_Response');
116
117 $configurationSources = array();
118 $configurationSources[] = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Source_TS');
119 if (!empty($this->cObj->data['pi_flexform'])) {
120 $configurationSource = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Source_FlexForm');
121 $configurationSource->setFlexFormContent($this->cObj->data['pi_flexform']);
122 $configurationSources[] = $configurationSource;
123 }
124 $configurationManager = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Manager', $configurationSources);
125 $configurationManager->loadGlobalSettings($extensionKey);
126 $configurationManager = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Manager');
127 $controller->injectSettings($configurationManager->getSettings($extensionKey));
128
129 $session = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Session');
130 try {
131 $controller->processRequest($request, $response);
132 } catch (TX_EXTMVC_Exception_StopAction $ignoredException) {
133 }
134 $session->commit();
135 $session->clear();
136
137 if (count($response->getAdditionalHeaderData()) > 0) {
138 $GLOBALS['TSFE']->additionalHeaderData[$request->getControllerExtensionKey()] = implode("\n", $response->getAdditionalHeaderData());
139 }
140
141 // TODO Remove debug statements
142 // $end_time = microtime(TRUE);
143 // debug($end_time - $start_time, -1);
144
145 // TODO Handle $response->getStatus()
146 // SK: Call sendHeaders() on the response
147 // JR: I don't think we need this, because the header will be sent by TYPO3
148 return $response->getContent();
149 }
150
151 /**
152 * Loads php files containing classes or interfaces found in the classes directory of
153 * an extension.
154 *
155 * @param string $className: Name of the class/interface to load
156 * @uses t3lib_extMgm::extPath()
157 * @return void
158 */
159 // TODO Remove autoloader as soon as we do not need it anymore
160 protected function autoLoadClasses($className) {
161 if (empty($this->registeredClassNames[$className])) {
162 $classNameParts = explode('_', $className);
163 if ($classNameParts[0] === 'ux') {
164 array_shift($classNameParts);
165 }
166 if (count($classNameParts) > 2 && $classNameParts[0] === 'TX') {
167 $classFilePathAndName = t3lib_extMgm::extPath(strtolower($classNameParts[1])) . 'Classes/';
168 $classFilePathAndName .= implode(array_slice($classNameParts, 2, -1), '/') . '/';
169 $classFilePathAndName .= implode('_', $classNameParts) . '.php';
170 }
171 if (isset($classFilePathAndName) && file_exists($classFilePathAndName)) {
172 require_once($classFilePathAndName);
173 $this->registeredClassNames[$className] = $classFilePathAndName;
174 }
175 }
176 }
177
178 }
179 ?>