dde853693160d6536f2929659feecef7b9e89f76
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Request.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 * Represents a generic request.
30 *
31 * @package Extbase
32 * @subpackage MVC
33 * @version $ID:$
34 * @scope prototype
35 * @api
36 */
37 class Tx_Extbase_MVC_Request implements Tx_Extbase_MVC_RequestInterface {
38
39 const PATTERN_MATCH_FORMAT = '/^[a-z0-9]{1,5}$/';
40
41 /**
42 * Pattern after which the controller object name is built
43 *
44 * @var string
45 */
46 protected $controllerObjectNamePattern = 'Tx_@extension_Controller_@controllerController';
47
48 /**
49 * @var string Key of the plugin which identifies the plugin. It must be a string containing [a-z0-9]
50 */
51 protected $pluginName = '';
52
53 /**
54 * @var string Name of the extension which is supposed to handle this request. This is the extension name converted to UpperCamelCase
55 */
56 protected $controllerExtensionName = NULL;
57
58 /**
59 * @var string Object name of the controller which is supposed to handle this request.
60 */
61 protected $controllerName = 'Standard';
62
63 /**
64 * @var string Name of the action the controller is supposed to take.
65 */
66 protected $controllerActionName = 'index';
67
68 /**
69 * @var array The arguments for this request
70 */
71 protected $arguments = array();
72
73 /**
74 * @var string The requested representation format
75 */
76 protected $format = 'txt';
77
78 /**
79 * @var boolean If this request has been changed and needs to be dispatched again
80 */
81 protected $dispatched = FALSE;
82
83 /**
84 * @var array Errors that occured during this request
85 */
86 protected $errors = array();
87
88 /**
89 * Sets the dispatched flag
90 *
91 * @param boolean $flag If this request has been dispatched
92 * @return void
93 * @api
94 */
95 public function setDispatched($flag) {
96 $this->dispatched = $flag ? TRUE : FALSE;
97 }
98
99 /**
100 * If this request has been dispatched and addressed by the responsible
101 * controller and the response is ready to be sent.
102 *
103 * The dispatcher will try to dispatch the request again if it has not been
104 * addressed yet.
105 *
106 * @return boolean TRUE if this request has been disptached sucessfully
107 * @api
108 */
109 public function isDispatched() {
110 return $this->dispatched;
111 }
112
113 /**
114 * Returns the object name of the controller defined by the extension name and
115 * controller name
116 *
117 * @return string The controller's Object Name
118 * @throws Tx_Extbase_MVC_Exception_NoSuchController if the controller does not exist
119 * @api
120 */
121 public function getControllerObjectName() {
122 $lowercaseObjectName = str_replace('@extension', $this->controllerExtensionName, $this->controllerObjectNamePattern);
123 $lowercaseObjectName = str_replace('@controller', $this->controllerName, $lowercaseObjectName);
124 // TODO implement getCaseSensitiveObjectName()
125 $objectName = $lowercaseObjectName;
126 if ($objectName === FALSE) throw new Tx_Extbase_MVC_Exception_NoSuchController('The controller object "' . $lowercaseObjectName . '" does not exist.', 1220884009);
127
128 return $objectName;
129 }
130
131 /**
132 * Sets the plugin name.
133 *
134 * @param string $extensionName The plugin name.
135 * @return void
136 */
137 public function setPluginName($pluginName = NULL) {
138 if ($pluginName !== NULL) {
139 $this->pluginName = $pluginName;
140 }
141 }
142
143 /**
144 * Returns the plugin key.
145 *
146 * @return string The plugin key
147 * @api
148 */
149 public function getPluginName() {
150 return $this->pluginName;
151 }
152
153 /**
154 * Sets the extension name of the controller.
155 *
156 * @param string $controllerExtensionName The extension name.
157 * @return void
158 * @throws Tx_Extbase_MVC_Exception_InvalidExtensionName if the extension name is not valid
159 */
160 public function setControllerExtensionName($controllerExtensionName) {
161 if ($controllerExtensionName !== NULL) {
162 $this->controllerExtensionName = $controllerExtensionName;
163 }
164 }
165
166 /**
167 * Returns the extension name of the specified controller.
168 *
169 * @return string The extension name
170 * @api
171 */
172 public function getControllerExtensionName() {
173 return $this->controllerExtensionName;
174 }
175
176 /**
177 * Returns the extension name of the specified controller.
178 *
179 * @return string The extension key
180 * @api
181 */
182 public function getControllerExtensionKey() {
183 return Tx_Extbase_Utility_Extension::convertCamelCaseToLowerCaseUnderscored($this->controllerExtensionName);
184 }
185
186 /**
187 * Sets the name of the controller which is supposed to handle the request.
188 * Note: This is not the object name of the controller!
189 *
190 * @param string $controllerName Name of the controller
191 * @return void
192 */
193 public function setControllerName($controllerName) {
194 if (!is_string($controllerName) && $controllerName !== NULL) throw new Tx_Extbase_MVC_Exception_InvalidControllerName('The controller name must be a valid string, ' . gettype($controllerName) . ' given.', 1187176358);
195 if (strpos($controllerName, '_') !== FALSE) throw new Tx_Extbase_MVC_Exception_InvalidControllerName('The controller name must not contain underscores.', 1217846412);
196 if ($controllerName !== NULL) {
197 $this->controllerName = $controllerName;
198 }
199 }
200
201 /**
202 * Returns the object name of the controller supposed to handle this request, if one
203 * was set already (if not, the name of the default controller is returned)
204 *
205 * @return string Object name of the controller
206 * @api
207 */
208 public function getControllerName() {
209 return $this->controllerName;
210 }
211
212 /**
213 * Sets the name of the action contained in this request.
214 *
215 * Note that the action name must start with a lower case letter and is case sensitive.
216 *
217 * @param string $actionName: Name of the action to execute by the controller
218 * @return void
219 * @throws Tx_Extbase_MVC_Exception_InvalidActionName if the action name is not valid
220 */
221 public function setControllerActionName($actionName) {
222 if (!is_string($actionName) && $actionName !== NULL) throw new Tx_Extbase_MVC_Exception_InvalidActionName('The action name must be a valid string, ' . gettype($actionName) . ' given (' . $actionName . ').', 1187176358);
223 if (($actionName{0} !== strtolower($actionName{0})) && $actionName !== NULL) throw new Tx_Extbase_MVC_Exception_InvalidActionName('The action name must start with a lower case letter, "' . $actionName . '" does not match this criteria.', 1218473352);
224 if ($actionName !== NULL) {
225 $this->controllerActionName = $actionName;
226 }
227 }
228
229 /**
230 * Returns the name of the action the controller is supposed to execute.
231 *
232 * @return string Action name
233 * @api
234 */
235 public function getControllerActionName() {
236 $controllerObjectName = $this->getControllerObjectName();
237 if ($controllerObjectName !== '' && ($this->controllerActionName === strtolower($this->controllerActionName))) {
238 $actionMethodName = $this->controllerActionName . 'Action';
239 foreach (get_class_methods($controllerObjectName) as $existingMethodName) {
240 if (strtolower($existingMethodName) === strtolower($actionMethodName)) {
241 $this->controllerActionName = substr($existingMethodName, 0, -6);
242 break;
243 }
244 }
245 }
246 return $this->controllerActionName;
247 }
248
249 /**
250 * Sets the value of the specified argument
251 *
252 * @param string $argumentName Name of the argument to set
253 * @param mixed $value The new value
254 * @return void
255 */
256 public function setArgument($argumentName, $value) {
257 if (!is_string($argumentName) || strlen($argumentName) == 0) throw new Tx_Extbase_MVC_Exception_InvalidArgumentName('Invalid argument name.', 1210858767);
258 $this->arguments[$argumentName] = $value;
259 }
260
261 /**
262 * Sets the whole arguments array and therefore replaces any arguments
263 * which existed before.
264 *
265 * @param array $arguments An array of argument names and their values
266 * @return void
267 */
268 public function setArguments(array $arguments) {
269 $this->arguments = $arguments;
270 }
271
272 /**
273 * Returns an array of arguments and their values
274 *
275 * @return array Associative array of arguments and their values (which may be arguments and values as well)
276 * @api
277 */
278 public function getArguments() {
279 return $this->arguments;
280 }
281
282 /**
283 * Returns the value of the specified argument
284 *
285 * @param string $argumentName Name of the argument
286 * @return string Value of the argument
287 * @throws Tx_Extbase_MVC_Exception_NoSuchArgument if such an argument does not exist
288 * @api
289 */
290 public function getArgument($argumentName) {
291 if (!isset($this->arguments[$argumentName])) throw new Tx_Extbase_MVC_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist for this request.', 1176558158);
292 return $this->arguments[$argumentName];
293 }
294
295 /**
296 * Checks if an argument of the given name exists (is set)
297 *
298 * @param string $argumentName Name of the argument to check
299 * @return boolean TRUE if the argument is set, otherwise FALSE
300 * @api
301 */
302 public function hasArgument($argumentName) {
303 return isset($this->arguments[$argumentName]);
304 }
305
306 /**
307 * Sets the requested representation format
308 *
309 * @param string $format The desired format, something like "html", "xml", "png", "json" or the like. Can even be something like "rss.xml".
310 * @return void
311 * @author Robert Lemke <robert@typo3.org>
312 */
313 public function setFormat($format) {
314 $this->format = $format;
315 }
316
317 /**
318 * Returns the requested representation format
319 *
320 * @return string The desired format, something like "html", "xml", "png", "json" or the like.
321 * @author Robert Lemke <robert@typo3.org>
322 * @api
323 */
324 public function getFormat() {
325 return $this->format;
326 }
327
328 /**
329 * Set errors that occured during the request (e.g. argument mapping errors)
330 *
331 * @param array $errors An array of Tx_Extbase_Error_Error objects
332 * @return void
333 */
334 public function setErrors(array $errors) {
335 $this->errors = $errors;
336 }
337
338 /**
339 * Get errors that occured during the request (e.g. argument mapping errors)
340 *
341 * @return array The errors that occured during the request
342 */
343 public function getErrors() {
344 return $this->errors;
345 }
346
347 }