Merge "[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 format of the response object
67 *
68 * @var string
69 */
70 protected $defaultFormat = 'html';
71
72 /**
73 * The allowed actions of the controller. This actions can be called via $_GET and $_POST.
74 *
75 * @var array
76 */
77 protected $allowedControllerActions = array();
78
79 /**
80 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
81 */
82 protected $configurationManager;
83
84 /**
85 * @var Tx_Extbase_Service_ExtensionService
86 */
87 protected $extensionService;
88
89 /**
90 * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
91 */
92 public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
93 $this->configurationManager = $configurationManager;
94 }
95
96 /**
97 * Injects the object manager
98 *
99 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
100 * @return void
101 */
102 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
103 $this->objectManager = $objectManager;
104 }
105
106 /**
107 * @param Tx_Extbase_Service_ExtensionService $extensionService
108 * @return void
109 */
110 public function injectExtensionService(Tx_Extbase_Service_ExtensionService $extensionService) {
111 $this->extensionService = $extensionService;
112 }
113
114 /**
115 * @return void
116 */
117 protected function loadDefaultValues() {
118 $configuration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
119 if (empty($configuration['extensionName'])) {
120 throw new Tx_Extbase_MVC_Exception('"extensionName" is not properly configured. Request can\'t be dispatched!', 1289843275);
121 }
122 if (empty($configuration['pluginName'])) {
123 throw new Tx_Extbase_MVC_Exception('"pluginName" is not properly configured. Request can\'t be dispatched!', 1289843277);
124 }
125 $this->extensionName = $configuration['extensionName'];
126 $this->pluginName = $configuration['pluginName'];
127 $this->defaultControllerName = current(array_keys($configuration['controllerConfiguration']));
128
129 $this->allowedControllerActions = array();
130 foreach ($configuration['controllerConfiguration'] as $controllerName => $controllerActions) {
131 $this->allowedControllerActions[$controllerName] = $controllerActions['actions'];
132 }
133 if (!empty($configuration['format'])) {
134 $this->defaultFormat = $configuration['format'];
135 }
136 }
137
138 /**
139 * Builds a web request object from the raw HTTP information and the configuration
140 *
141 * @return Tx_Extbase_MVC_Web_Request The web request as an object
142 */
143 public function build() {
144 $this->loadDefaultValues();
145 $pluginNamespace = $this->extensionService->getPluginNamespace($this->extensionName, $this->pluginName);
146 $parameters = t3lib_div::_GPmerged($pluginNamespace);
147
148 $controllerName = $this->resolveControllerName($parameters);
149 $actionName = $this->resolveActionName($controllerName, $parameters);
150
151 $request = $this->objectManager->create('Tx_Extbase_MVC_Web_Request');
152 $request->setPluginName($this->pluginName);
153 $request->setControllerExtensionName($this->extensionName);
154 $request->setControllerName($controllerName);
155 $request->setControllerActionName($actionName);
156 $request->setRequestUri(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
157 $request->setBaseUri(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
158 $request->setMethod((isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : NULL);
159
160 if (is_string($parameters['format']) && (strlen($parameters['format']))) {
161 $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING));
162 } else {
163 $request->setFormat($this->defaultFormat);
164 }
165
166 foreach ($parameters as $argumentName => $argumentValue) {
167 $request->setArgument($argumentName, $argumentValue);
168 }
169
170 return $request;
171 }
172
173 /**
174 * Returns the current ControllerName extracted from given $parameters.
175 * If no controller is specified, the defaultControllerName will be returned.
176 * If that's not available, an exception is thrown.
177 *
178 * @param array $parameters
179 * @return string
180 * @throws Tx_Extbase_MVC_Exception if the controller could not be resolved
181 */
182 protected function resolveControllerName(array $parameters) {
183 if (!isset($parameters['controller']) || strlen($parameters['controller']) === 0) {
184 if (strlen($this->defaultControllerName) === 0) {
185 throw new Tx_Extbase_MVC_Exception(
186 'The default controller can not be determined. Please check for Tx_Extbase_Utility_Extension::configurePlugin() in your ext_localconf.php.',
187 1316104317
188 );
189 }
190 return $this->defaultControllerName;
191 }
192 $allowedControllerNames = array_keys($this->allowedControllerActions);
193 if (!in_array($parameters['controller'], $allowedControllerNames)) {
194 $configuration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
195 if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (boolean)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
196 throw new t3lib_error_http_PageNotFoundException(
197 'The requested resource was not found',
198 1313857897
199 );
200 }
201 throw new Tx_Extbase_MVC_Exception_InvalidControllerName(
202 'The controller "' . $parameters['controller'] . '" is not allowed by this plugin. Please check for Tx_Extbase_Utility_Extension::configurePlugin() in your ext_localconf.php.',
203 1313855173
204 );
205 }
206 return filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
207 }
208
209 /**
210 * Returns the current actionName extracted from given $parameters.
211 * If no action is specified, the defaultActionName will be returned.
212 * If that's not available or the specified action is not defined in the current plugin, an exception is thrown.
213 *
214 * @param $controllerName
215 * @param array $parameters
216 * @return string
217 * @throws t3lib_error_http_PageNotFoundException|Tx_Extbase_MVC_Exception|Tx_Extbase_MVC_Exception_InvalidActionName if the action could not be resolved
218 */
219 protected function resolveActionName($controllerName, array $parameters) {
220 if (!isset($parameters['action']) || strlen($parameters['action']) === 0) {
221 $defaultActionName = is_array($this->allowedControllerActions[$controllerName]) ? current($this->allowedControllerActions[$controllerName]) : '';
222 if (strlen($defaultActionName) === 0) {
223 throw new Tx_Extbase_MVC_Exception(
224 'The default action can not be determined for controller "' . $controllerName . '". Please check Tx_Extbase_Utility_Extension::configurePlugin() in your ext_localconf.php.',
225 1295479651
226 );
227 }
228 return $defaultActionName;
229 }
230 $actionName = $parameters['action'];
231 $allowedActionNames = $this->allowedControllerActions[$controllerName];
232 if (!in_array($actionName, $allowedActionNames)) {
233 $configuration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
234 if (isset($configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) && (boolean)$configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved']) {
235 throw new t3lib_error_http_PageNotFoundException(
236 'The requested resource was not found',
237 1313857897
238 );
239 }
240 throw new Tx_Extbase_MVC_Exception_InvalidActionName(
241 'The action "' . $actionName . '" (controller "' . $controllerName . '") is not allowed by this plugin. Please check Tx_Extbase_Utility_Extension::configurePlugin() in your ext_localconf.php.',
242 1313855173
243 );
244 }
245 return filter_var($actionName, FILTER_SANITIZE_STRING);
246 }
247
248
249 }
250 ?>