* added common extension files
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Web / TX_EXTMVC_Web_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 web request.
26 *
27 * @version $Id:$
28 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
29 *
30 * @scope prototype
31 */
32 class Request extends TX_EXTMVC_Request {
33
34 const REQUEST_METHOD_UNKNOWN = NULL;
35 const REQUEST_METHOD_GET = 'GET';
36 const REQUEST_METHOD_POST = 'POST';
37 const REQUEST_METHOD_HEAD = 'HEAD';
38 const REQUEST_METHOD_OPTIONS = 'OPTIONS';
39 const REQUEST_METHOD_PUT = 'PUT';
40 const REQUEST_METHOD_DELETE = 'DELETE';
41
42 /**
43 * @var string The requested representation format
44 */
45 protected $format = 'html';
46
47 /**
48 * @var string Contains the request method
49 */
50 protected $method = F3_FLOW3_Utility_Environment::REQUEST_METHOD_GET;
51
52 /**
53 * @var F3_FLOW3_Utility_Environment
54 */
55 protected $environment;
56
57 /**
58 * @var F3_FLOW3_Property_DataType_URI The request URI
59 */
60 protected $requestURI;
61
62 /**
63 * @var F3_FLOW3_Property_DataType_URI The base URI for this request - ie. the host and path leading to the index.php
64 */
65 protected $baseURI;
66
67 /**
68 * Injects the environment
69 *
70 * @param F3_FLOW3_Utility_Environment $environment
71 * @return void
72 * @author Robert Lemke <robert@typo3.org>
73 */
74 public function injectEnvironment(F3_FLOW3_Utility_Environment $environment) {
75 $this->environment = $environment;
76 }
77
78 /**
79 * Sets the request method
80 *
81 * @param string $method Name of the request method - one of the self::REQUEST_METHOD_* constants
82 * @return void
83 * @author Robert Lemke <robert@typo3.org>
84 * @throws TX_EXTMVC_Exception_InvalidRequestMethod if the request method is not supported
85 */
86 public function setMethod($method) {
87 if (array_search($method, array(
88 self::REQUEST_METHOD_GET,
89 self::REQUEST_METHOD_POST,
90 self::REQUEST_METHOD_DELETE,
91 self::REQUEST_METHOD_PUT,
92 self::REQUEST_METHOD_HEAD,
93 self::REQUEST_METHOD_OPTIONS,
94 self::REQUEST_METHOD_UNKNOWN
95 )) === FALSE) throw new TX_EXTMVC_Exception_InvalidRequestMethod('The request method "' . $method . '" is not supported.', 1217778382);
96 $this->method = $method;
97 }
98
99 /**
100 * Returns the name of the request method
101 *
102 * @return string Name of the request method - one of the F3_FLOW3_Utility_Environment::REQUEST_METHOD_* constants
103 * @author Robert Lemke <robert@typo3.org>
104 */
105 public function getMethod() {
106 return $this->method;
107 }
108
109 /**
110 * Sets the request URI
111 *
112 * @param F3_FLOW3_Property_DataType_URI $requestURI URI of this web request
113 * @return void
114 * @author Robert Lemke <robert@typo3.org>
115 */
116 public function setRequestURI(F3_FLOW3_Property_DataType_URI $requestURI) {
117 $this->requestURI = clone $requestURI;
118 $this->baseURI = $this->detectBaseURI($requestURI);
119 }
120
121 /**
122 * Returns the request URI
123 *
124 * @return F3_FLOW3_Property_DataType_URI URI of this web request
125 * @author Robert Lemke <robert@typo3.org>
126 */
127 public function getRequestURI() {
128 return $this->requestURI;
129 }
130
131 /**
132 * Sets the base URI for this request.
133 *
134 * @param F3_FLOW3_Property_DataType_URI $baseURI New base URI
135 * @return void
136 * @author Robert Lemke <robert@typo3.org>
137 */
138 public function setBaseURI(F3_FLOW3_Property_DataType_URI $baseURI) {
139 $this->baseURI = clone $baseURI;
140 }
141
142 /**
143 * Returns the base URI
144 *
145 * @return F3_FLOW3_Property_DataType_URI Base URI of this web request
146 * @author Robert Lemke <robert@typo3.org>
147 */
148 public function getBaseURI() {
149 return $this->baseURI;
150 }
151
152 /**
153 * Tries to detect the base URI of this request and returns it.
154 *
155 * @param F3_FLOW3_Property_DataType_URI $requestURI URI of this web request
156 * @return F3_FLOW3_Property_DataType_URI The detected base URI
157 * @author Robert Lemke <robert@typo3.org>
158 */
159 protected function detectBaseURI(F3_FLOW3_Property_DataType_URI $requestURI) {
160 $baseURI = clone $requestURI;
161 $baseURI->setQuery(NULL);
162 $baseURI->setFragment(NULL);
163
164 $requestPathSegments = explode('/', $this->environment->getScriptRequestPathAndName());
165 array_pop($requestPathSegments);
166 $baseURI->setPath(implode('/', $requestPathSegments) . '/');
167 return $baseURI;
168 }
169 }
170 ?>