e35d65f44340a346aec7a448728288d0b3b13ce7
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / 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_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 * Pattern after which the view object name is built
46 *
47 * @var string
48 */
49 protected $viewObjectNamePattern = 'Tx_@extension_View_@controller@action';
50
51 /**
52 * Extension key of the controller which is supposed to handle this request.
53 *
54 * @var string
55 */
56 protected $controllerExtensionKey = 'ExtBase';
57
58 /**
59 * @var string Object name of the controller which is supposed to handle this request.
60 */
61 protected $controllerName = 'Default';
62
63 /**
64 * @var string Name of the action the controller is supposed to take.
65 */
66 protected $controllerActionName = 'index';
67
68 /**
69 * @var ArrayObject The arguments for this request
70 */
71 protected $arguments;
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 * Constructs this request
85 *
86 */
87 public function __construct() {
88 $this->arguments = new ArrayObject;
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 key 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->controllerExtensionKey, $this->controllerObjectNamePattern);
123 $lowercaseObjectName = str_replace('@controller', $this->controllerName, $lowercaseObjectName);
124 $objectName = $lowercaseObjectName; //$this->objectManager->getCaseSensitiveObjectName($lowercaseObjectName); // TODO implement getCaseSensitiveObjectName()
125 if ($objectName === FALSE) throw new Tx_ExtBase_Exception_NoSuchController('The controller object "' . $lowercaseObjectName . '" does not exist.', 1220884009);
126
127 return $objectName;
128 }
129
130 /**
131 * Sets the pattern for building the controller object name.
132 *
133 * The pattern may contain the placeholders "@extension" and "@controller" which will be substituted
134 * by the real extension key and controller name.
135 *
136 * @param string $pattern The pattern
137 * @return void
138 */
139 public function setControllerObjectNamePattern($pattern) {
140 $this->controllerObjectNamePattern = $pattern;
141 }
142
143 /**
144 * Returns the pattern for building the controller object name.
145 *
146 * @return string $pattern The pattern
147 */
148 public function getControllerObjectNamePattern() {
149 return $this->controllerObjectNamePattern;
150 }
151
152 /**
153 * Sets the pattern for building the view object name
154 *
155 * @param string $pattern The view object name pattern, eg. F3_@extension_View::@controller@action
156 * @return void
157 */
158 public function setViewObjectNamePattern($pattern) {
159 if (!is_string($pattern)) throw new InvalidArgumentException('The view object name pattern must be a valid string, ' . gettype($pattern) . ' given.', 1221563219);
160 $this->viewObjectNamePattern = $pattern;
161 }
162
163 /**
164 * Returns the View Object Name Pattern
165 *
166 * @return string The pattern
167 */
168 public function getViewObjectNamePattern() {
169 return $this->viewObjectNamePattern;
170 }
171
172 /**
173 * Returns the view's (possible) object name according to the defined view object
174 * name pattern and the specified values for extension, controller, action and format.
175 *
176 * If no valid view object name could be resolved, FALSE is returned
177 *
178 * @return mixed Either the view object name or FALSE
179 */
180 public function getViewObjectName() {
181 $possibleViewName = $this->viewObjectNamePattern;
182 $possibleViewName = str_replace('@extension', $this->controllerExtensionKey, $possibleViewName);
183 $possibleViewName = str_replace('@controller', $this->controllerName, $possibleViewName);
184 $possibleViewName = str_replace('@action', ucfirst($this->controllerActionName), $possibleViewName);
185
186 $viewObjectName = $possibleViewName;
187 // $viewObjectName = str_replace('@format', $this->format, $possibleViewName); //$this->objectManager->getCaseSensitiveObjectName(str_replace('@format', $this->format, $possibleViewName)); // TODO
188 // if ($viewObjectName === FALSE) {
189 // $viewObjectName = str_replace('@format', '', $possibleViewName); //$this->objectManager->getCaseSensitiveObjectName(str_replace('@format', '', $possibleViewName));
190 // }
191 return $viewObjectName;
192 }
193
194 /**
195 * Sets the extension key of the controller.
196 *
197 * @param string $extensionKey The extension key.
198 * @return void
199 * @throws Tx_ExtBase_Exception_InvalidExtensionKey if the extension key is not valid
200 */
201 public function setControllerExtensionKey($extensionKey) {
202 $upperCamelCasedExtensionKey = $extensionKey; //$this->packageManager->getCaseSensitiveExtensionKey($extensionKey); // TODO implement getCaseSensitiveExtensionKey()
203 if ($upperCamelCasedExtensionKey === FALSE) throw new Tx_ExtBase_Exception_InvalidExtensionKey('"' . $extensionKey . '" is not a valid extension key.', 1217961104);
204 $this->controllerExtensionKey = $upperCamelCasedExtensionKey;
205 }
206
207 /**
208 * Returns the extension key of the specified controller.
209 *
210 * @return string The extension key
211 */
212 public function getControllerExtensionKey() {
213 return $this->controllerExtensionKey;
214 }
215
216 /**
217 * Sets the name of the controller which is supposed to handle the request.
218 * Note: This is not the object name of the controller!
219 *
220 * @param string $controllerName Name of the controller
221 * @return void
222 */
223 public function setControllerName($controllerName) {
224 if (!is_string($controllerName)) throw new Tx_ExtBase_Exception_InvalidControllerName('The controller name must be a valid string, ' . gettype($controllerName) . ' given.', 1187176358);
225 if (strpos($controllerName, '_') !== FALSE) throw new Tx_ExtBase_Exception_InvalidControllerName('The controller name must not contain underscores.', 1217846412);
226 $this->controllerName = $controllerName;
227 }
228
229 /**
230 * Returns the object name of the controller supposed to handle this request, if one
231 * was set already (if not, the name of the default controller is returned)
232 *
233 * @return string Object name of the controller
234 */
235 public function getControllerName() {
236 return $this->controllerName;
237 }
238
239 /**
240 * Sets the name of the action contained in this request.
241 *
242 * Note that the action name must start with a lower case letter.
243 *
244 * @param string $actionName: Name of the action to execute by the controller
245 * @return void
246 * @throws Tx_ExtBase_Exception_InvalidActionName if the action name is not valid
247 */
248 public function setControllerActionName($actionName) {
249 if (!is_string($actionName)) throw new Tx_ExtBase_Exception_InvalidActionName('The action name must be a valid string, ' . gettype($actionName) . ' given (' . $actionName . ').', 1187176358);
250 if ($actionName{0} !== strtolower($actionName{0})) throw new Tx_ExtBase_Exception_InvalidActionName('The action name must start with a lower case letter, "' . $actionName . '" does not match this criteria.', 1218473352);
251 $this->controllerActionName = $actionName;
252 }
253
254 /**
255 * Returns the name of the action the controller is supposed to execute.
256 *
257 * @return string Action name
258 */
259 public function getControllerActionName() {
260 return $this->controllerActionName;
261 }
262
263 /**
264 * Sets the value of the specified argument
265 *
266 * @param string $argumentName Name of the argument to set
267 * @param mixed $value The new value
268 * @return void
269 */
270 public function setArgument($argumentName, $value) {
271 if (!is_string($argumentName) || strlen($argumentName) == 0) throw new Tx_ExtBase_Exception_InvalidArgumentName('Invalid argument name.', 1210858767);
272 $this->arguments[$argumentName] = $value;
273 }
274
275 /**
276 * Sets the whole arguments ArrayObject and therefore replaces any arguments
277 * which existed before.
278 *
279 * @param ArrayObject $arguments An ArrayObject of argument names and their values
280 * @return void
281 */
282 public function setArguments(ArrayObject $arguments) {
283 $this->arguments = $arguments;
284 }
285
286 /**
287 * Returns an ArrayObject of arguments and their values
288 *
289 * @return ArrayObject ArrayObject of arguments and their values (which may be arguments and values as well)
290 */
291 public function getArguments() {
292 return $this->arguments;
293 }
294
295 /**
296 * Returns the value of the specified argument
297 *
298 * @param string $argumentName Name of the argument
299 * @return string Value of the argument
300 * @throws Tx_ExtBase_Exception_NoSuchArgument if such an argument does not exist
301 */
302 public function getArgument($argumentName) {
303 if (!isset($this->arguments[$argumentName])) throw new Tx_ExtBase_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist for this request.', 1176558158);
304 return $this->arguments[$argumentName];
305 }
306
307 /**
308 * Checks if an argument of the given name exists (is set)
309 *
310 * @param string $argumentName Name of the argument to check
311 * @return boolean TRUE if the argument is set, otherwise FALSE
312 */
313 public function hasArgument($argumentName) {
314 return isset($this->arguments[$argumentName]);
315 }
316 }
317 ?>