6a3fa7d2b0cd2b8d609281f842cddb2f693f9277
[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.
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 return $this->controllerActionName;
237 }
238
239 /**
240 * Sets the value of the specified argument
241 *
242 * @param string $argumentName Name of the argument to set
243 * @param mixed $value The new value
244 * @return void
245 */
246 public function setArgument($argumentName, $value) {
247 if (!is_string($argumentName) || strlen($argumentName) == 0) throw new Tx_Extbase_MVC_Exception_InvalidArgumentName('Invalid argument name.', 1210858767);
248 $this->arguments[$argumentName] = $value;
249 }
250
251 /**
252 * Sets the whole arguments array and therefore replaces any arguments
253 * which existed before.
254 *
255 * @param array $arguments An array of argument names and their values
256 * @return void
257 */
258 public function setArguments(array $arguments) {
259 $this->arguments = $arguments;
260 }
261
262 /**
263 * Returns an array of arguments and their values
264 *
265 * @return array Associative array of arguments and their values (which may be arguments and values as well)
266 * @api
267 */
268 public function getArguments() {
269 return $this->arguments;
270 }
271
272 /**
273 * Returns the value of the specified argument
274 *
275 * @param string $argumentName Name of the argument
276 * @return string Value of the argument
277 * @throws Tx_Extbase_MVC_Exception_NoSuchArgument if such an argument does not exist
278 * @api
279 */
280 public function getArgument($argumentName) {
281 if (!isset($this->arguments[$argumentName])) throw new Tx_Extbase_MVC_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist for this request.', 1176558158);
282 return $this->arguments[$argumentName];
283 }
284
285 /**
286 * Checks if an argument of the given name exists (is set)
287 *
288 * @param string $argumentName Name of the argument to check
289 * @return boolean TRUE if the argument is set, otherwise FALSE
290 * @api
291 */
292 public function hasArgument($argumentName) {
293 return isset($this->arguments[$argumentName]);
294 }
295
296 /**
297 * Sets the requested representation format
298 *
299 * @param string $format The desired format, something like "html", "xml", "png", "json" or the like. Can even be something like "rss.xml".
300 * @return void
301 * @author Robert Lemke <robert@typo3.org>
302 */
303 public function setFormat($format) {
304 $this->format = $format;
305 }
306
307 /**
308 * Returns the requested representation format
309 *
310 * @return string The desired format, something like "html", "xml", "png", "json" or the like.
311 * @author Robert Lemke <robert@typo3.org>
312 * @api
313 */
314 public function getFormat() {
315 return $this->format;
316 }
317
318 /**
319 * Set errors that occured during the request (e.g. argument mapping errors)
320 *
321 * @param array $errors An array of Tx_Extbase_Error_Error objects
322 * @return void
323 */
324 public function setErrors(array $errors) {
325 $this->errors = $errors;
326 }
327
328 /**
329 * Get errors that occured during the request (e.g. argument mapping errors)
330 *
331 * @return array The errors that occured during the request
332 */
333 public function getErrors() {
334 return $this->errors;
335 }
336
337 }