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