EXTMVC:
[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/Controller/TX_EXTMVC_Controller_AbstractController.php');
30 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_ActionController.php');
31 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_Arguments.php');
32 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_Argument.php');
33 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/View/TX_EXTMVC_View_AbstractView.php');
34 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/TX_EXTMVC_Persistence_Session.php');
35 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/Mapper/TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper.php');
36
37 /**
38 * Creates a request an dispatches it to the controller which was specified by TS Setup, Flexform,
39 * or Extension Configuration (ExtConf), and returns the content to the v4 framework.
40 *
41 * @version $Id:$
42 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
43 */
44 class TX_EXTMVC_Dispatcher {
45
46 /**
47 * @var TX_EXTMVC_Configuration_Manager A reference to the configuration manager
48 */
49 protected $configurationManager;
50
51 /**
52 * @var TX_EXTMVC_Web_RequestBuilder
53 */
54 protected $requestBuilder;
55
56 /**
57 * @var ArrayObject The raw GET parameters
58 */
59 protected $getParameters;
60
61 /**
62 * @var ArrayObject The raw POST parameters
63 */
64 protected $postParameters;
65
66 /**
67 * Constructs this dispatcher
68 *
69 */
70 public function __construct() {
71 $this->arguments = new ArrayObject;
72 spl_autoload_register(array($this, 'autoLoadClasses'));
73 }
74
75 /**
76 * Creates a request an dispatches it to a controller.
77 *
78 * @param String $content The content
79 * @param array|NULL $configuration The TS configuration array
80 * @return String $content The processed content
81 */
82 public function dispatch($content, $configuration) {
83 // TODO Add an AJAX dispatcher
84 // debug($configuration);
85 $request = t3lib_div::makeInstance('TX_EXTMVC_Web_Request');
86 $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
87 $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
88 $request->setControllerExtensionKey(strtolower($configuration['extension']));
89 $request->setControllerName($configuration['controller']);
90 $request->setControllerActionName($configuration['action']);
91 $arguments = t3lib_div::makeInstance('TX_EXTMVC_Controller_Arguments');
92 foreach (t3lib_div::GParrayMerged('tx_' . strtolower($configuration['extension'])) as $key => $value) {
93 $argument = new TX_EXTMVC_Controller_Argument($key, 'Raw');
94 $argument->setValue($value);
95 $arguments->addArgument($argument);
96 }
97 $request->setArguments($arguments);
98 $response = t3lib_div::makeInstance('TX_EXTMVC_Web_Response');
99
100 $controllerObjectName = $request->getControllerObjectName();
101 $controller = t3lib_div::makeInstance($controllerObjectName);
102
103 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);
104 // TODO Configuration management
105 // $settings = $this->configurationManager->getSettings($extensionKey);
106 // $controller->injectSettings($this->configurationManager->getSettings($request->getControllerExtensionKey()));
107 $session = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Session');
108 try {
109 $controller->processRequest($request, $response);
110 } catch (TX_EXTMVC_Exception_StopAction $ignoredException) {
111 } catch (TX_EXTMVC_Exception_StopUncachedAction $ignoredException) {
112 $this->cObj->convertToUserIntObject();
113 }
114
115 $session->commit();
116 $session->clear();
117
118 $GLOBALS['TSFE']->additionalHeaderData[$request->getControllerExtensionKey()] = implode("\n", $response->getAdditionalHeaderTags());
119
120 return $response->getContent();
121 }
122
123 /**
124 * Loads php files containing classes or interfaces found in the classes directory of
125 * an extension.
126 *
127 * @param string $className: Name of the class/interface to load
128 * @return void
129 */
130 protected function autoLoadClasses($className) {
131 $classNameParts = explode('_', $className);
132 if ($classNameParts[0] === 'ux') {
133 array_shift($classNameParts);
134 }
135 if (count($classNameParts) > 2 && $classNameParts[0] === 'TX') {
136 $classFilePathAndName = t3lib_extMgm::extPath(strtolower($classNameParts[1])) . 'Classes/';
137 $classFilePathAndName .= implode(array_slice($classNameParts, 2, -1), '/') . '/';
138 $classFilePathAndName .= implode('_', $classNameParts) . '.php';
139 }
140 if (isset($classFilePathAndName) && file_exists($classFilePathAndName)) require_once($classFilePathAndName);
141 }
142
143 }
144 ?>