[BUGFIX] ConfigurationManager use FrontendSimulator only if neccessary
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Web / RequestBuilder.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Builds a web request.
30 *
31 * @package Extbase
32 * @subpackage MVC\Web
33 * @version $ID:$
34 *
35 * @scope prototype
36 */
37 class Tx_Extbase_MVC_Web_RequestBuilder implements t3lib_Singleton {
38
39 /**
40 * @var Tx_Extbase_Object_ObjectManagerInterface
41 */
42 protected $objectManager;
43
44 /**
45 * This is a unique key for a plugin (not the extension key!)
46 *
47 * @var string
48 */
49 protected $pluginName = 'plugin';
50
51 /**
52 * The name of the extension (in UpperCamelCase)
53 *
54 * @var string
55 */
56 protected $extensionName;
57
58 /**
59 * The default controller name
60 *
61 * @var string
62 */
63 protected $defaultControllerName;
64
65 /**
66 * The default action of the default controller
67 *
68 * @var string
69 */
70 protected $defaultActionName;
71
72 /**
73 * The default format of the response object
74 *
75 * @var string
76 */
77 protected $defaultFormat = 'html';
78
79 /**
80 * The allowed actions of the controller. This actions can be called via $_GET and $_POST.
81 *
82 * @var array
83 */
84 protected $allowedControllerActions = array();
85
86 /**
87 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
88 */
89 protected $configurationManager;
90
91 /**
92 * @var Tx_Extbase_Service_ExtensionService
93 */
94 protected $extensionService;
95
96 /**
97 * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
98 */
99 public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
100 $this->configurationManager = $configurationManager;
101 }
102
103 /**
104 * Injects the object manager
105 *
106 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
107 * @return void
108 */
109 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
110 $this->objectManager = $objectManager;
111 }
112
113 /**
114 * @param Tx_Extbase_Service_ExtensionService $extensionService
115 * @return void
116 */
117 public function injectExtensionService(Tx_Extbase_Service_ExtensionService $extensionService) {
118 $this->extensionService = $extensionService;
119 }
120
121 /**
122 * @return void
123 */
124 protected function loadDefaultValues() {
125 $configuration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
126 if (empty($configuration['extensionName'])) {
127 throw new Tx_Extbase_MVC_Exception('"extensionName" is not properly configured. Request can\'t be dispatched!', 1289843275);
128 }
129 if (empty($configuration['pluginName'])) {
130 throw new Tx_Extbase_MVC_Exception('"pluginName" is not properly configured. Request can\'t be dispatched!', 1289843277);
131 }
132 $this->extensionName = $configuration['extensionName'];
133 $this->pluginName = $configuration['pluginName'];
134 $this->defaultControllerName = current(array_keys($configuration['controllerConfiguration']));
135 $this->defaultActionName = current($configuration['controllerConfiguration'][$this->defaultControllerName]['actions']);
136
137 $allowedControllerActions = array();
138 foreach ($configuration['controllerConfiguration'] as $controllerName => $controllerActions) {
139 $allowedControllerActions[$controllerName] = $controllerActions['actions'];
140 }
141 $this->allowedControllerActions = $allowedControllerActions;
142 if (!empty($configuration['format'])) {
143 $this->defaultFormat = $configuration['format'];
144 }
145 }
146
147 /**
148 * Builds a web request object from the raw HTTP information and the configuration
149 *
150 * @return Tx_Extbase_MVC_Web_Request The web request as an object
151 */
152 public function build() {
153 $this->loadDefaultValues();
154 $pluginNamespace = $this->extensionService->getPluginNamespace($this->extensionName, $this->pluginName);
155 $parameters = t3lib_div::_GPmerged($pluginNamespace);
156
157 if (is_string($parameters['controller']) && array_key_exists($parameters['controller'], $this->allowedControllerActions)) {
158 $controllerName = filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
159 } elseif (!empty($this->defaultControllerName)) {
160 $controllerName = $this->defaultControllerName;
161 } else {
162 throw new Tx_Extbase_MVC_Exception(
163 'The default controller can not be determined.<br />'
164 . 'Please check for Tx_Extbase_Utility_Extension::configurePlugin() in your ext_localconf.php.',
165 1295479650
166 );
167 }
168
169 $allowedActions = $this->allowedControllerActions[$controllerName];
170 if (is_string($parameters['action']) && is_array($allowedActions) && in_array($parameters['action'], $allowedActions)) {
171 $actionName = filter_var($parameters['action'], FILTER_SANITIZE_STRING);
172 } elseif (!empty($this->defaultActionName)) {
173 $actionName = $this->defaultActionName;
174 } else {
175 throw new Tx_Extbase_MVC_Exception(
176 'The default action can not be determined for controller "' . $controllerName . '".<br />'
177 . 'Please check Tx_Extbase_Utility_Extension::configurePlugin() in your ext_localconf.php.',
178 1295479651
179 );
180 }
181
182 $request = $this->objectManager->create('Tx_Extbase_MVC_Web_Request');
183 $request->setPluginName($this->pluginName);
184 $request->setControllerExtensionName($this->extensionName);
185 $request->setControllerName($controllerName);
186 $request->setControllerActionName($actionName);
187 $request->setRequestUri(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
188 $request->setBaseUri(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
189 $request->setMethod((isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : NULL);
190
191 if (is_string($parameters['format']) && (strlen($parameters['format']))) {
192 $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING));
193 } else {
194 $request->setFormat($this->defaultFormat);
195 }
196
197 foreach ($parameters as $argumentName => $argumentValue) {
198 $request->setArgument($argumentName, $argumentValue);
199 }
200
201 return $request;
202 }
203
204
205 }
206 ?>