85487c8700ef14aa0f93c6ee622bab173f3f1d2c
[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 $pluginKey = '';
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 $extensionName = 'ExtBase';
58
59 /**
60 * @var string Name of the controller which is supposed to handle this request.
61 */
62 protected $controllerName = 'Default';
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 string The requested representation format
76 */
77 protected $format = 'txt';
78
79 /**
80 * @var boolean If this request has been changed and needs to be dispatched again
81 */
82 protected $dispatched = FALSE;
83
84 /**
85 * Constructs this request
86 *
87 */
88 public function __construct() {
89 }
90
91 /**
92 * Sets the dispatched flag
93 *
94 * @param boolean $flag If this request has been dispatched
95 * @return void
96 */
97 public function setDispatched($flag) {
98 $this->dispatched = $flag ? TRUE : FALSE;
99 }
100
101 /**
102 * If this request has been dispatched and addressed by the responsible
103 * controller and the response is ready to be sent.
104 *
105 * The dispatcher will try to dispatch the request again if it has not been
106 * addressed yet.
107 *
108 * @return boolean TRUE if this request has been disptached sucessfully
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_Exception_NoSuchController if the controller does not exist
120 */
121 public function getControllerObjectName() {
122 $lowercaseObjectName = str_replace('@extension', $this->extensionName, $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_Exception_NoSuchController('The controller object "' . $lowercaseObjectName . '" does not exist.', 1220884009);
127
128 return $objectName;
129 }
130
131 /**
132 * Sets the pattern for building the controller object name.
133 *
134 * The pattern may contain the placeholders "@extension" and "@controller" which will be substituted
135 * by the real extension name and controller name.
136 *
137 * @param string $pattern The pattern
138 * @return void
139 */
140 public function setControllerObjectNamePattern($pattern) {
141 $this->controllerObjectNamePattern = $pattern;
142 }
143
144 /**
145 * Returns the pattern for building the controller object name.
146 *
147 * @return string $pattern The pattern
148 */
149 public function getControllerObjectNamePattern() {
150 return $this->controllerObjectNamePattern;
151 }
152
153 /**
154 * Sets the pattern for building the view object name
155 *
156 * @param string $pattern The view object name pattern, eg. F3_@extension_View::@controller@action
157 * @return void
158 */
159 public function setViewObjectNamePattern($pattern) {
160 if (!is_string($pattern)) throw new InvalidArgumentException('The view object name pattern must be a valid string, ' . gettype($pattern) . ' given.', 1221563219);
161 $this->viewObjectNamePattern = $pattern;
162 }
163
164 /**
165 * Returns the View Object Name Pattern
166 *
167 * @return string The pattern
168 */
169 public function getViewObjectNamePattern() {
170 return $this->viewObjectNamePattern;
171 }
172
173 /**
174 * Returns the view's (possible) object name according to the defined view object
175 * name pattern and the specified values for extension, controller, action and format.
176 *
177 * If no valid view object name could be resolved, FALSE is returned
178 *
179 * @return mixed Either the view object name or FALSE
180 */
181 public function getViewObjectName() {
182 $possibleViewName = $this->viewObjectNamePattern;
183 $possibleViewName = str_replace('@extension', $this->extensionName, $possibleViewName);
184 $possibleViewName = str_replace('@controller', $this->controllerName, $possibleViewName);
185 $possibleViewName = str_replace('@action', ucfirst($this->controllerActionName), $possibleViewName);
186
187 $viewObjectName = $possibleViewName;
188 // $viewObjectName = str_replace('@format', $this->format, $possibleViewName); //$this->objectManager->getCaseSensitiveObjectName(str_replace('@format', $this->format, $possibleViewName)); // TODO
189 // if ($viewObjectName === FALSE) {
190 // $viewObjectName = str_replace('@format', '', $possibleViewName); //$this->objectManager->getCaseSensitiveObjectName(str_replace('@format', '', $possibleViewName));
191 // }
192 return $viewObjectName;
193 }
194
195 /**
196 * Sets the plugin key.
197 *
198 * @param string $extensionName The plugin key.
199 * @return void
200 */
201 public function setPluginKey($pluginKey = NULL) {
202 if ($pluginKey !== NULL) {
203 $this->pluginKey = $pluginKey;
204 }
205 }
206
207 /**
208 * Returns the plugin key.
209 *
210 * @return string The plugin key
211 */
212 public function getPluginKey() {
213 return $this->pluginKey;
214 }
215
216 /**
217 * Sets the extension name of the controller.
218 *
219 * @param string $extensionName The extension name.
220 * @return void
221 * @throws Tx_ExtBase_Exception_InvalidExtensionName if the extension name is not valid
222 */
223 public function setExtensionName($extensionName = NULL) {
224 if ($extensionName !== NULL) {
225 $this->extensionName = $extensionName;
226 }
227 }
228
229 /**
230 * Returns the extension name of the specified controller.
231 *
232 * @return string The extension name
233 */
234 public function getExtensionName() {
235 return $this->extensionName;
236 }
237
238 /**
239 * Sets the name of the controller which is supposed to handle the request.
240 * Note: This is not the object name of the controller!
241 *
242 * @param string $controllerName Name of the controller
243 * @return void
244 */
245 public function setControllerName($controllerName = NULL) {
246 if (!is_string($controllerName) && $controllerName !== NULL) throw new Tx_ExtBase_Exception_InvalidControllerName('The controller name must be a valid string, ' . gettype($controllerName) . ' given.', 1187176358);
247 if (strpos($controllerName, '_') !== FALSE) throw new Tx_ExtBase_Exception_InvalidControllerName('The controller name must not contain underscores.', 1217846412);
248 if ($controllerName !== NULL) {
249 $this->controllerName = $controllerName;
250 }
251 }
252
253 /**
254 * Returns the object name of the controller supposed to handle this request, if one
255 * was set already (if not, the name of the default controller is returned)
256 *
257 * @return string Object name of the controller
258 */
259 public function getControllerName() {
260 return $this->controllerName;
261 }
262
263 /**
264 * Sets the name of the action contained in this request.
265 *
266 * Note that the action name must start with a lower case letter.
267 *
268 * @param string $actionName: Name of the action to execute by the controller
269 * @return void
270 * @throws Tx_ExtBase_Exception_InvalidActionName if the action name is not valid
271 */
272 public function setControllerActionName($actionName = NULL) {
273 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);
274 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);
275 if ($actionName !== NULL) {
276 $this->controllerActionName = $actionName;
277 }
278 }
279
280 /**
281 * Returns the name of the action the controller is supposed to execute.
282 *
283 * @return string Action name
284 */
285 public function getControllerActionName() {
286 return $this->controllerActionName;
287 }
288
289 /**
290 * Sets the value of the specified argument
291 *
292 * @param string $argumentName Name of the argument to set
293 * @param mixed $value The new value
294 * @return void
295 */
296 public function setArgument($argumentName, $value) {
297 if (!is_string($argumentName) || strlen($argumentName) == 0) throw new Tx_ExtBase_Exception_InvalidArgumentName('Invalid argument name.', 1210858767);
298 $this->arguments[$argumentName] = $value;
299 }
300
301 /**
302 * Sets the whole arguments ArrayObject and therefore replaces any arguments
303 * which existed before.
304 *
305 * @param ArrayObject $arguments An ArrayObject of argument names and their values
306 * @return void
307 */
308 public function setArguments(ArrayObject $arguments) {
309 $this->arguments = $arguments;
310 }
311
312 /**
313 * Returns an ArrayObject of arguments and their values
314 *
315 * @return ArrayObject ArrayObject of arguments and their values (which may be arguments and values as well)
316 */
317 public function getArguments() {
318 return $this->arguments;
319 }
320
321 /**
322 * Returns the value of the specified argument
323 *
324 * @param string $argumentName Name of the argument
325 * @return string Value of the argument
326 * @throws Tx_ExtBase_Exception_NoSuchArgument if such an argument does not exist
327 */
328 public function getArgument($argumentName) {
329 if (!isset($this->arguments[$argumentName])) throw new Tx_ExtBase_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist for this request.', 1176558158);
330 return $this->arguments[$argumentName];
331 }
332
333 /**
334 * Checks if an argument of the given name exists (is set)
335 *
336 * @param string $argumentName Name of the argument to check
337 * @return boolean TRUE if the argument is set, otherwise FALSE
338 */
339 public function hasArgument($argumentName) {
340 return isset($this->arguments[$argumentName]);
341 }
342 }
343 ?>