Extbase:
[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 script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Represents a generic request.
27 *
28 * @package TYPO3
29 * @subpackage extbase
30 * @version $ID:$
31 * @scope prototype
32 */
33 class Tx_Extbase_MVC_Request {
34
35 const PATTERN_MATCH_FORMAT = '/^[a-z0-9]{1,5}$/';
36
37 /**
38 * Pattern after which the controller object name is built
39 *
40 * @var string
41 */
42 protected $controllerObjectNamePattern = 'Tx_@extension_Controller_@controllerController';
43
44 /**
45 * @var string Pattern after which the view object name is built
46 */
47 protected $viewObjectNamePattern = 'Tx_@extension_View_@controller@action';
48
49 /**
50 * @var string Key of the plugin which identifies the plugin. It must be a string containing [a-z0-9]
51 */
52 protected $pluginName = '';
53
54 /**
55 * @var string Name of the extension which is supposed to handle this request. This is the extension name converted to UpperCamelCase
56 */
57 protected $controllerExtensionName = 'Extbase';
58
59 /**
60 * @var string Name of the controller which is supposed to handle this request.
61 */
62 protected $controllerName = 'Standard';
63
64 /**
65 * @var string Name of the action the controller is supposed to take.
66 */
67 protected $controllerActionName = 'index';
68
69 /**
70 * @var ArrayObject The arguments for this request
71 */
72 protected $arguments = array();
73
74 /**
75 * @var boolean If this request has been changed and needs to be dispatched again
76 */
77 protected $dispatched = FALSE;
78
79 /**
80 * Constructs this request
81 *
82 */
83 public function __construct() {
84 }
85
86 /**
87 * Sets the dispatched flag
88 *
89 * @param boolean $flag If this request has been dispatched
90 * @return void
91 */
92 public function setDispatched($flag) {
93 $this->dispatched = $flag ? TRUE : FALSE;
94 }
95
96 /**
97 * If this request has been dispatched and addressed by the responsible
98 * controller and the response is ready to be sent.
99 *
100 * The dispatcher will try to dispatch the request again if it has not been
101 * addressed yet.
102 *
103 * @return boolean TRUE if this request has been disptached sucessfully
104 */
105 public function isDispatched() {
106 return $this->dispatched;
107 }
108
109 /**
110 * Returns the object name of the controller defined by the extension name and
111 * controller name
112 *
113 * @return string The controller's Object Name
114 * @throws Tx_Extbase_Exception_NoSuchController if the controller does not exist
115 */
116 public function getControllerObjectName() {
117 $lowercaseObjectName = str_replace('@extension', $this->controllerExtensionName, $this->controllerObjectNamePattern);
118 $lowercaseObjectName = str_replace('@controller', $this->controllerName, $lowercaseObjectName);
119 // TODO implement getCaseSensitiveObjectName()
120 $objectName = $lowercaseObjectName;
121 if ($objectName === FALSE) throw new Tx_Extbase_Exception_NoSuchController('The controller object "' . $lowercaseObjectName . '" does not exist.', 1220884009);
122
123 return $objectName;
124 }
125
126 /**
127 * Sets the pattern for building the controller object name.
128 *
129 * The pattern may contain the placeholders "@extension" and "@controller" which will be substituted
130 * by the real extension name and controller name.
131 *
132 * @param string $pattern The pattern
133 * @return void
134 */
135 public function setControllerObjectNamePattern($pattern) {
136 $this->controllerObjectNamePattern = $pattern;
137 }
138
139 /**
140 * Returns the pattern for building the controller object name.
141 *
142 * @return string $pattern The pattern
143 */
144 public function getControllerObjectNamePattern() {
145 return $this->controllerObjectNamePattern;
146 }
147
148 /**
149 * Sets the pattern for building the view object name
150 *
151 * @param string $pattern The view object name pattern, eg. F3_@extension_View::@controller@action
152 * @return void
153 */
154 public function setViewObjectNamePattern($pattern) {
155 if (!is_string($pattern)) throw new InvalidArgumentException('The view object name pattern must be a valid string, ' . gettype($pattern) . ' given.', 1221563219);
156 $this->viewObjectNamePattern = $pattern;
157 }
158
159 /**
160 * Returns the View Object Name Pattern
161 *
162 * @return string The pattern
163 */
164 public function getViewObjectNamePattern() {
165 return $this->viewObjectNamePattern;
166 }
167
168 /**
169 * Returns the view's (possible) object name according to the defined view object
170 * name pattern and the specified values for extension, controller, action and format.
171 *
172 * If no valid view object name could be resolved, FALSE is returned
173 *
174 * @return mixed Either the view object name or FALSE
175 */
176 public function getViewObjectName() {
177 $viewObjectName = $this->viewObjectNamePattern;
178 $viewObjectName = str_replace('@extension', $this->controllerExtensionName, $viewObjectName);
179 $viewObjectName = str_replace('@controller', $this->controllerName, $viewObjectName);
180 $viewObjectName = str_replace('@action', ucfirst($this->controllerActionName), $viewObjectName);
181 return $viewObjectName;
182 }
183
184 /**
185 * Sets the plugin name.
186 *
187 * @param string $extensionName The plugin name.
188 * @return void
189 */
190 public function setPluginName($pluginName = NULL) {
191 if ($pluginName !== NULL) {
192 $this->pluginName = $pluginName;
193 }
194 }
195
196 /**
197 * Returns the plugin key.
198 *
199 * @return string The plugin key
200 */
201 public function getPluginName() {
202 return $this->pluginName;
203 }
204
205 /**
206 * Sets the extension name of the controller.
207 *
208 * @param string $controllerExtensionName The extension name.
209 * @return void
210 * @throws Tx_Extbase_Exception_InvalidExtensionName if the extension name is not valid
211 */
212 public function setControllerExtensionName($controllerExtensionName = NULL) {
213 if ($controllerExtensionName !== NULL) {
214 $this->controllerExtensionName = $controllerExtensionName;
215 }
216 }
217
218 /**
219 * Returns the extension name of the specified controller.
220 *
221 * @return string The extension name
222 */
223 public function getControllerExtensionName() {
224 return $this->controllerExtensionName;
225 }
226
227 /**
228 * Returns the extension name of the specified controller.
229 *
230 * @return string The extension name
231 */
232 public function getControllerExtensionKey() {
233 return Tx_Extbase_Utility_Strings::camelCaseToLowerCaseUnderscored($this->controllerExtensionName);
234 }
235
236 /**
237 * Sets the name of the controller which is supposed to handle the request.
238 * Note: This is not the object name of the controller!
239 *
240 * @param string $controllerName Name of the controller
241 * @return void
242 */
243 public function setControllerName($controllerName = NULL) {
244 if (!is_string($controllerName) && $controllerName !== NULL) throw new Tx_Extbase_Exception_InvalidControllerName('The controller name must be a valid string, ' . gettype($controllerName) . ' given.', 1187176358);
245 if (strpos($controllerName, '_') !== FALSE) throw new Tx_Extbase_Exception_InvalidControllerName('The controller name must not contain underscores.', 1217846412);
246 if ($controllerName !== NULL) {
247 $this->controllerName = $controllerName;
248 }
249 }
250
251 /**
252 * Returns the object name of the controller supposed to handle this request, if one
253 * was set already (if not, the name of the default controller is returned)
254 *
255 * @return string Object name of the controller
256 */
257 public function getControllerName() {
258 return $this->controllerName;
259 }
260
261 /**
262 * Sets the name of the action contained in this request.
263 *
264 * Note that the action name must start with a lower case letter.
265 *
266 * @param string $actionName: Name of the action to execute by the controller
267 * @return void
268 * @throws Tx_Extbase_Exception_InvalidActionName if the action name is not valid
269 */
270 public function setControllerActionName($actionName = NULL) {
271 if (!is_string($actionName) && $actionName !== NULL) throw new Tx_Extbase_Exception_InvalidActionName('The action name must be a valid string, ' . gettype($actionName) . ' given (' . $actionName . ').', 1187176358);
272 if (($actionName{0} !== strtolower($actionName{0})) && $actionName !== NULL) throw new Tx_Extbase_Exception_InvalidActionName('The action name must start with a lower case letter, "' . $actionName . '" does not match this criteria.', 1218473352);
273 if ($actionName !== NULL) {
274 $this->controllerActionName = $actionName;
275 }
276 }
277
278 /**
279 * Returns the name of the action the controller is supposed to execute.
280 *
281 * @return string Action name
282 */
283 public function getControllerActionName() {
284 return $this->controllerActionName;
285 }
286
287 /**
288 * Sets the value of the specified argument
289 *
290 * @param string $argumentName Name of the argument to set
291 * @param mixed $value The new value
292 * @return void
293 */
294 public function setArgument($argumentName, $value) {
295 if (!is_string($argumentName) || strlen($argumentName) == 0) throw new Tx_Extbase_Exception_InvalidArgumentName('Invalid argument name.', 1210858767);
296 $this->arguments[$argumentName] = $value;
297 }
298
299 /**
300 * Sets the whole arguments array and therefore replaces any arguments
301 * which existed before.
302 *
303 * @param array $arguments An array of argument names and their values
304 * @return void
305 */
306 public function setArguments(array $arguments) {
307 $this->arguments = $arguments;
308 }
309
310 /**
311 * Returns an array of arguments and their values
312 *
313 * @return array Associative array of arguments and their values (which may be arguments and values as well)
314 */
315 public function getArguments() {
316 return $this->arguments;
317 }
318
319 /**
320 * Returns the value of the specified argument
321 *
322 * @param string $argumentName Name of the argument
323 * @return string Value of the argument
324 * @throws Tx_Extbase_Exception_NoSuchArgument if such an argument does not exist
325 */
326 public function getArgument($argumentName) {
327 if (!isset($this->arguments[$argumentName])) throw new Tx_Extbase_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist for this request.', 1176558158);
328 return $this->arguments[$argumentName];
329 }
330
331 /**
332 * Checks if an argument of the given name exists (is set)
333 *
334 * @param string $argumentName Name of the argument to check
335 * @return boolean TRUE if the argument is set, otherwise FALSE
336 */
337 public function hasArgument($argumentName) {
338 return isset($this->arguments[$argumentName]);
339 }
340 }
341 ?>