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