* implemented support for additional header data
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / TX_EXTMVC_Request.php
1 <?php
2 declare(ENCODING = 'utf-8');
3
4 /* *
5 * This script belongs to the FLOW3 framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 /**
25 * Represents a generic request.
26 *
27 * @version $Id:$
28 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
29 * @scope prototype
30 */
31 class TX_EXTMVC_Request {
32
33 const PATTERN_MATCH_FORMAT = '/^[a-z0-9]{1,5}$/';
34
35 /**
36 * Pattern after which the controller object name is built
37 *
38 * @var string
39 */
40 protected $controllerObjectNamePattern = 'TX_@extension_Controller_@controllerController';
41
42 /**
43 * Pattern after which the view object name is built
44 *
45 * @var string
46 */
47 protected $viewObjectNamePattern = 'TX_@extension_View_@controller@action';
48
49 /**
50 * Extension key of the controller which is supposed to handle this request.
51 *
52 * @var string
53 */
54 protected $controllerExtensionKey = 'EXTMVC';
55
56 /**
57 * @var string Object name of the controller which is supposed to handle this request.
58 */
59 protected $controllerName = 'Default';
60
61 /**
62 * @var string Name of the action the controller is supposed to take.
63 */
64 protected $controllerActionName = 'index';
65
66 /**
67 * @var ArrayObject The arguments for this request
68 */
69 protected $arguments;
70
71 /**
72 * @var string The requested representation format
73 */
74 protected $format = 'txt';
75
76 /**
77 * @var boolean If this request has been changed and needs to be dispatched again
78 */
79 protected $dispatched = FALSE;
80
81 /**
82 * Constructs this request
83 *
84 * @author Robert Lemke <robert@typo3.org>
85 */
86 public function __construct() {
87 $this->arguments = new ArrayObject;
88 }
89
90 /**
91 * Sets the dispatched flag
92 *
93 * @param boolean $flag If this request has been dispatched
94 * @return void
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 * @author Robert Lemke <robert@typo3.org>
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_EXTMVC_Exception_NoSuchController if the controller does not exist
120 * @author Robert Lemke <robert@typo3.org>
121 */
122 public function getControllerObjectName() {
123 $lowercaseObjectName = str_replace('@extension', $this->controllerExtensionKey, $this->controllerObjectNamePattern);
124 $lowercaseObjectName = str_replace('@controller', $this->controllerName, $lowercaseObjectName);
125 $objectName = $lowercaseObjectName; //$this->objectManager->getCaseSensitiveObjectName($lowercaseObjectName); // TODO implement getCaseSensitiveObjectName()
126 if ($objectName === FALSE) throw new TX_EXTMVC_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 key and controller name.
136 *
137 * @param string $pattern The pattern
138 * @return void
139 * @author Robert Lemke <robert@typo3.org>
140 */
141 public function setControllerObjectNamePattern($pattern) {
142 $this->controllerObjectNamePattern = $pattern;
143 }
144
145 /**
146 * Returns the pattern for building the controller object name.
147 *
148 * @return string $pattern The pattern
149 * @author Robert Lemke <robert@typo3.org>
150 */
151 public function getControllerObjectNamePattern() {
152 return $this->controllerObjectNamePattern;
153 }
154
155 /**
156 * Sets the pattern for building the view object name
157 *
158 * @param string $pattern The view object name pattern, eg. F3_@extension_View::@controller@action
159 * @return void
160 * @author Robert Lemke <robert@typo3.org>
161 */
162 public function setViewObjectNamePattern($pattern) {
163 if (!is_string($pattern)) throw new InvalidArgumentException('The view object name pattern must be a valid string, ' . gettype($pattern) . ' given.', 1221563219);
164 $this->viewObjectNamePattern = $pattern;
165 }
166
167 /**
168 * Returns the View Object Name Pattern
169 *
170 * @return string The pattern
171 * @author Robert Lemke <robert@typo3.org>
172 */
173 public function getViewObjectNamePattern() {
174 return $this->viewObjectNamePattern;
175 }
176
177 /**
178 * Returns the view's (possible) object name according to the defined view object
179 * name pattern and the specified values for extension, controller, action and format.
180 *
181 * If no valid view object name could be resolved, FALSE is returned
182 *
183 * @return mixed Either the view object name or FALSE
184 * @author Robert Lemke <robert@typo3.org>
185 */
186 public function getViewObjectName() {
187 $possibleViewName = $this->viewObjectNamePattern;
188 $possibleViewName = str_replace('@extension', $this->controllerExtensionKey, $possibleViewName);
189 $possibleViewName = str_replace('@controller', $this->controllerName, $possibleViewName);
190 $possibleViewName = str_replace('@action', ucfirst($this->controllerActionName), $possibleViewName);
191
192 $viewObjectName = $possibleViewName;
193 // $viewObjectName = str_replace('@format', $this->format, $possibleViewName); //$this->objectManager->getCaseSensitiveObjectName(str_replace('@format', $this->format, $possibleViewName)); // TODO
194 // if ($viewObjectName === FALSE) {
195 // $viewObjectName = str_replace('@format', '', $possibleViewName); //$this->objectManager->getCaseSensitiveObjectName(str_replace('@format', '', $possibleViewName));
196 // }
197 return $viewObjectName;
198 }
199
200 /**
201 * Sets the extension key of the controller.
202 *
203 * @param string $extensionKey The extension key.
204 * @return void
205 * @throws TX_EXTMVC_Exception_InvalidExtensionKey if the extension key is not valid
206 * @author Robert Lemke <robert@typo3.org>
207 */
208 public function setControllerExtensionKey($extensionKey) {
209 $upperCamelCasedExtensionKey = $extensionKey; //$this->packageManager->getCaseSensitiveExtensionKey($extensionKey); // TODO implement getCaseSensitiveExtensionKey()
210 if ($upperCamelCasedExtensionKey === FALSE) throw new TX_EXTMVC_Exception_InvalidExtensionKey('"' . $extensionKey . '" is not a valid extension key.', 1217961104);
211 $this->controllerExtensionKey = $upperCamelCasedExtensionKey;
212 }
213
214 /**
215 * Returns the extension key of the specified controller.
216 *
217 * @return string The extension key
218 * @author Robert Lemke <robert@typo3.org>
219 */
220 public function getControllerExtensionKey() {
221 return $this->controllerExtensionKey;
222 }
223
224 /**
225 * Sets the name of the controller which is supposed to handle the request.
226 * Note: This is not the object name of the controller!
227 *
228 * @param string $controllerName Name of the controller
229 * @return void
230 * @author Robert Lemke <robert@typo3.org>
231 */
232 public function setControllerName($controllerName) {
233 if (!is_string($controllerName)) throw new TX_EXTMVC_Exception_InvalidControllerName('The controller name must be a valid string, ' . gettype($controllerName) . ' given.', 1187176358);
234 if (strpos($controllerName, '_') !== FALSE) throw new TX_EXTMVC_Exception_InvalidControllerName('The controller name must not contain underscores.', 1217846412);
235 $this->controllerName = $controllerName;
236 }
237
238 /**
239 * Returns the object name of the controller supposed to handle this request, if one
240 * was set already (if not, the name of the default controller is returned)
241 *
242 * @return string Object name of the controller
243 * @author Robert Lemke <robert@typo3.org>
244 */
245 public function getControllerName() {
246 return $this->controllerName;
247 }
248
249 /**
250 * Sets the name of the action contained in this request.
251 *
252 * Note that the action name must start with a lower case letter.
253 *
254 * @param string $actionName: Name of the action to execute by the controller
255 * @return void
256 * @throws TX_EXTMVC_Exception_InvalidActionName if the action name is not valid
257 * @author Robert Lemke <robert@typo3.org>
258 */
259 public function setControllerActionName($actionName) {
260 if (!is_string($actionName)) throw new TX_EXTMVC_Exception_InvalidActionName('The action name must be a valid string, ' . gettype($actionName) . ' given (' . $actionName . ').', 1187176358);
261 if ($actionName{0} !== strtolower($actionName{0})) throw new TX_EXTMVC_Exception_InvalidActionName('The action name must start with a lower case letter, "' . $actionName . '" does not match this criteria.', 1218473352);
262 $this->controllerActionName = $actionName;
263 }
264
265 /**
266 * Returns the name of the action the controller is supposed to execute.
267 *
268 * @return string Action name
269 * @author Robert Lemke <robert@typo3.org>
270 */
271 public function getControllerActionName() {
272 return $this->controllerActionName;
273 }
274
275 /**
276 * Sets the value of the specified argument
277 *
278 * @param string $argumentName Name of the argument to set
279 * @param mixed $value The new value
280 * @return void
281 * @author Robert Lemke <robert@typo3.org>
282 */
283 public function setArgument($argumentName, $value) {
284 if (!is_string($argumentName) || strlen($argumentName) == 0) throw new TX_EXTMVC_Exception_InvalidArgumentName('Invalid argument name.', 1210858767);
285 $this->arguments[$argumentName] = $value;
286 }
287
288 /**
289 * Sets the whole arguments ArrayObject and therefore replaces any arguments
290 * which existed before.
291 *
292 * @param ArrayObject $arguments An ArrayObject of argument names and their values
293 * @return void
294 * @author Robert Lemke <robert@typo3.org>
295 */
296 public function setArguments(_ArrayObject $arguments) {
297 $this->arguments = $arguments;
298 }
299
300 /**
301 * Returns an ArrayObject of arguments and their values
302 *
303 * @return ArrayObject ArrayObject of arguments and their values (which may be arguments and values as well)
304 * @author Robert Lemke <robert@typo3.org>
305 */
306 public function getArguments() {
307 return $this->arguments;
308 }
309
310 /**
311 * Sets the requested representation format
312 *
313 * @param string $format The desired format, something like "html", "xml", "png", "json" or the like.
314 * @return void
315 * @author Robert Lemke <robert@typo3.org>
316 */
317 public function setFormat($format) {
318 if (!preg_match(self::PATTERN_MATCH_FORMAT, $format)) throw new TX_EXTMVC_Exception_InvalidFormat('An invalid request format (' . $format . ') was given.', 1218015038);
319 $this->format = $format;
320 }
321
322 /**
323 * Returns the requested representation format
324 *
325 * @return string The desired format, something like "html", "xml", "png", "json" or the like.
326 * @author Robert Lemke <robert@typo3.org>
327 */
328 public function getFormat() {
329 return $this->format;
330 }
331
332 /**
333 * Returns the value of the specified argument
334 *
335 * @param string $argumentName Name of the argument
336 * @return string Value of the argument
337 * @author Robert Lemke <robert@typo3.org>
338 * @throws TX_EXTMVC_Exception_NoSuchArgument if such an argument does not exist
339 */
340 public function getArgument($argumentName) {
341 if (!isset($this->arguments[$argumentName])) throw new TX_EXTMVC_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist for this request.', 1176558158);
342 return $this->arguments[$argumentName];
343 }
344
345 /**
346 * Checks if an argument of the given name exists (is set)
347 *
348 * @param string $argumentName Name of the argument to check
349 * @return boolean TRUE if the argument is set, otherwise FALSE
350 * @author Robert Lemke <robert@typo3.org>
351 */
352 public function hasArgument($argumentName) {
353 return isset($this->arguments[$argumentName]);
354 }
355 }
356 ?>