fc6cdc099e9448bfd08785583dbb9c7bc23e0c29
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / class.tx_extmvc_dispatcher.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 framework. *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 // TODO these statements become obsolete with the new autoloader -> remove them
24
25 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/TX_EXTMVC_Request.php');
26 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Web/TX_EXTMVC_Web_Request.php');
27 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/TX_EXTMVC_Response.php');
28 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Web/TX_EXTMVC_Web_Response.php');
29 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Configuration/TX_EXTMVC_Configuration_Manager.php');
30 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_AbstractController.php');
31 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_ActionController.php');
32 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_Arguments.php');
33 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_Argument.php');
34 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/View/TX_EXTMVC_View_AbstractView.php');
35 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/TX_EXTMVC_Persistence_Session.php');
36 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/Mapper/TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper.php');
37
38 /**
39 * Creates a request an dispatches it to the controller which was specified by TS Setup, Flexform,
40 * or Extension Configuration (ExtConf), and returns the content to the v4 framework.
41 *
42 * @version $Id:$
43 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
44 */
45 class TX_EXTMVC_Dispatcher {
46
47 /**
48 * @var TX_EXTMVC_Configuration_Manager A reference to the configuration manager
49 */
50 protected $configurationManager;
51
52 /**
53 * @var TX_EXTMVC_Web_RequestBuilder
54 */
55 protected $requestBuilder;
56
57 /**
58 * @var ArrayObject The raw GET parameters
59 */
60 protected $getParameters;
61
62 /**
63 * @var ArrayObject The raw POST parameters
64 */
65 protected $postParameters;
66
67 /**
68 * @var array An array of registered classes (class files with path)
69 */
70 protected $registeredClassNames;
71
72 /**
73 * Constructs this dispatcher
74 *
75 */
76 public function __construct() {
77 // SK: $this->arguments is not needed here.
78 $this->arguments = new ArrayObject;
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 $start_time = microtime(TRUE);
96
97 // SK: should the parameters really be prepended with tx_extmvc? Maybe it makes more sense to have a few "global" parameters, like:
98 // SK: "controller", "extensionKey", "action"?
99 $parameters = t3lib_div::_GET('tx_extmvc');
100 $extensionKey = isset($parameters['extension']) ? stripslashes($parameters['extension']) : $configuration['extension'];
101 $controllerName = isset($parameters['controller']) ? stripslashes($parameters['controller']) : $configuration['controller'];
102 $actionName = isset($parameters['action']) ? stripslashes($parameters['action']) : $configuration['action'];
103
104 $request = t3lib_div::makeInstance('TX_EXTMVC_Web_Request');
105 $request->setControllerExtensionKey($extensionKey);
106 $request->setControllerName($controllerName);
107 $request->setControllerActionName($actionName);
108
109 $controllerObjectName = $request->getControllerObjectName();
110 $controller = t3lib_div::makeInstance($controllerObjectName);
111
112 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);
113
114 if (!$controller->isCachableAction($actionName) && $this->cObj->getUserObjectType() === tslib_cObj::OBJECTTYPE_USER) {
115 // FIXME Caching does nort work because it's by default a USER object, so the dispatcher is never called
116 // SK: does caching already work?
117 $this->cObj->convertToUserIntObject();
118 return $content;
119 }
120
121 $arguments = t3lib_div::makeInstance('TX_EXTMVC_Controller_Arguments');
122 // SK: strtolower($extensionKey) is wrong I think, as all underscores need to be removed as well.
123 // SK: Example: tt_news -> tx_ttnews
124 foreach (t3lib_div::GParrayMerged('tx_' . strtolower($extensionKey)) as $key => $value) {
125 // SK: This argument is passed attached to the Request - so do NOT use TX_EXTMVC_Controller_Argument or TX_EXTMVC_Controller_Arguments here.
126 // SK: Instead, just add an argument by using $request->addArgument('key', 'value');
127 // SK: The real argument mapping and validation then happens in the AbstractController.
128 $argument = new TX_EXTMVC_Controller_Argument($key, 'Raw');
129 $argument->setValue($value);
130 $arguments->addArgument($argument);
131 }
132 // SK: as shown above, this needs to be changed
133 $request->setArguments($arguments);
134 $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
135 $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
136 $response = t3lib_div::makeInstance('TX_EXTMVC_Web_Response');
137
138 $configurationSources = array();
139 $configurationSources[] = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Source_TS');
140 if (!empty($this->cObj->data['pi_flexform'])) {
141 $configurationSource = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Source_FlexForm');
142 $configurationSource->setFlexFormContent($this->cObj->data['pi_flexform']);
143 $configurationSources[] = $configurationSource;
144 }
145 $configurationManager = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Manager', $configurationSources);
146 $configurationManager->loadGlobalSettings($extensionKey);
147 $configurationManager = t3lib_div::makeInstance('TX_EXTMVC_Configuration_Manager');
148 $settings = $configurationManager->getSettings($extensionKey);
149 $controller->injectSettings($settings);
150
151 $session = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Session');
152 try {
153 $controller->processRequest($request, $response);
154 } catch (TX_EXTMVC_Exception_StopAction $ignoredException) {
155 }
156 $session->commit();
157 $session->clear();
158
159 $GLOBALS['TSFE']->additionalHeaderData[$request->getControllerExtensionKey()] = implode("\n", $response->getAdditionalHeaderTags());
160
161 // SK: TODO: Remove debug statements
162 $end_time = microtime(TRUE);
163 debug($end_time - $start_time, -1);
164
165 // SK: Handle $response->getStatus()
166 // SK: Call sendHeaders() on the response
167 return $response->getContent();
168 }
169
170 /**
171 * Loads php files containing classes or interfaces found in the classes directory of
172 * an extension.
173 *
174 * @param string $className: Name of the class/interface to load
175 * @uses t3lib_extMgm::extPath()
176 * @return void
177 */
178 // SK: Remove autoloader as soon as we do not need it anymore
179 protected function autoLoadClasses($className) {
180 if (empty($this->registeredClassNames[$className])) {
181 $classNameParts = explode('_', $className);
182 if ($classNameParts[0] === 'ux') {
183 array_shift($classNameParts);
184 }
185 if (count($classNameParts) > 2 && $classNameParts[0] === 'TX') {
186 $classFilePathAndName = t3lib_extMgm::extPath(strtolower($classNameParts[1])) . 'Classes/';
187 $classFilePathAndName .= implode(array_slice($classNameParts, 2, -1), '/') . '/';
188 $classFilePathAndName .= implode('_', $classNameParts) . '.php';
189 }
190 if (isset($classFilePathAndName) && file_exists($classFilePathAndName)) {
191 require_once($classFilePathAndName);
192 $this->registeredClassNames[$className] = $classFilePathAndName;
193 }
194 }
195 }
196
197 }
198 ?>