1956d4167ec3be5f45a3a69e3ba527aec638b536
[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 array An array of registered classes (class files with path)
37 */
38 protected $registeredClassNames;
39
40 /**
41 * Constructs this dispatcher
42 *
43 */
44 public function __construct() {
45 spl_autoload_register(array($this, 'autoLoadClasses'));
46 }
47
48 /**
49 * Creates a request an dispatches it to a controller.
50 *
51 * @param String $content The content
52 * @param array|NULL $configuration The TS configuration array
53 * @return String $content The processed content
54 */
55 public function dispatch($content, $configuration) {
56 if (!is_array($configuration)) {
57 throw new Exception('Could not dispatch the request. Please configure your plugin in the TS Setup.', 1237879677);
58 }
59 $requestBuilder = t3lib_div::makeInstance('Tx_ExtBase_MVC_Web_RequestBuilder');
60 $request = $requestBuilder->build($configuration);
61 $response = t3lib_div::makeInstance('Tx_ExtBase_MVC_Web_Response');
62 $controller = $this->getPreparedController($request);
63 $persistenceSession = t3lib_div::makeInstance('Tx_ExtBase_Persistence_Session');
64 try {
65 $controller->processRequest($request, $response);
66 } catch (Tx_ExtBase_Exception_StopAction $ignoredException) {
67 }
68 // var_dump($persistenceSession);
69 $persistenceSession->commit();
70 $persistenceSession->clear();
71 if (count($response->getAdditionalHeaderData()) > 0) {
72 $GLOBALS['TSFE']->additionalHeaderData[$request->getExtensionName()] = implode("\n", $response->getAdditionalHeaderData());
73 }
74 // TODO Handle $response->getStatus()
75 $response->sendHeaders();
76 return $response->getContent();
77 }
78
79 /**
80 * Builds and returns a controller
81 *
82 * @param Tx_ExtBase_MVC_Web_Request $request
83 * @return Tx_ExtBase_MVC_Controller_ControllerInterface The prepared controller
84 */
85 protected function getPreparedController(Tx_ExtBase_MVC_Web_Request $request) {
86 $controllerObjectName = $request->getControllerObjectName();
87 $controller = t3lib_div::makeInstance($controllerObjectName);
88 if (!$controller instanceof Tx_ExtBase_MVC_Controller_ControllerInterface) {
89 throw new Tx_ExtBase_Exception_InvalidController('Invalid controller "' . $request->getControllerObjectName() . '". The controller must implement the Tx_ExtBase_MVC_Controller_ControllerInterface.', 1202921619);
90 }
91 $controller->injectSettings($this->getSettings($request));
92 return $controller;
93 }
94
95 /**
96 * Builds the settings by overlaying TS Setup with FlexForm values of the extension
97 * and returns them as a plain array (with no trailing dots).
98 *
99 * @param Tx_ExtBase_MVC_Web_Request $request
100 * @return array The settings array
101 */
102 protected function getSettings(Tx_ExtBase_MVC_Web_Request $request) {
103 $extensionName = $request->getExtensionName();
104 $configurationSources = array();
105 $configurationSources[] = t3lib_div::makeInstance('Tx_ExtBase_Configuration_Source_TypoScriptSource');
106 if (!empty($this->cObj->data['pi_flexform'])) {
107 $configurationSource = t3lib_div::makeInstance('Tx_ExtBase_Configuration_Source_FlexFormSource');
108 $configurationSource->setFlexFormContent($this->cObj->data['pi_flexform']);
109 $configurationSources[] = $configurationSource;
110 }
111 $configurationManager = t3lib_div::makeInstance('Tx_ExtBase_Configuration_Manager', $configurationSources);
112 $configurationManager->loadGlobalSettings($extensionName);
113 return $configurationManager->getSettings($extensionName);
114 }
115
116 /**
117 * Loads php files containing classes or interfaces found in the classes directory of
118 * an extension.
119 *
120 * @param string $className: Name of the class/interface to load
121 * @uses t3lib_extMgm::extPath()
122 * @return void
123 */
124 // TODO Remove autoloader as soon as we do not need it anymore
125 public function autoLoadClasses($className) {
126 if (empty($this->registeredClassNames[$className])) {
127 $classNameParts = explode('_', $className);
128 if ($classNameParts[0] === 'ux') {
129 array_shift($classNameParts);
130 }
131 if (count($classNameParts) > 2 && $classNameParts[0] === 'Tx') {
132 $classFilePathAndName = t3lib_extMgm::extPath(strtolower($classNameParts[1])) . 'Classes/';
133 $classFilePathAndName .= implode(array_slice($classNameParts, 2, -1), '/') . '/';
134 $classFilePathAndName .= array_pop($classNameParts) . '.php';
135 }
136 if (isset($classFilePathAndName) && file_exists($classFilePathAndName)) {
137 require_once($classFilePathAndName);
138 $this->registeredClassNames[$className] = $classFilePathAndName;
139 }
140 }
141 }
142
143 }
144 ?>