* some minor renaming (package -> extension)
[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 TX_EXTMVC_Web_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 = self::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 * Sets the request method
69 *
70 * @param string $method Name of the request method - one of the self::REQUEST_METHOD_* constants
71 * @return void
72 * @author Robert Lemke <robert@typo3.org>
73 * @throws TX_EXTMVC_Exception_InvalidRequestMethod if the request method is not supported
74 */
75 public function setMethod($method) {
76 if (array_search($method, array(
77 self::REQUEST_METHOD_GET,
78 self::REQUEST_METHOD_POST,
79 self::REQUEST_METHOD_DELETE,
80 self::REQUEST_METHOD_PUT,
81 self::REQUEST_METHOD_HEAD,
82 self::REQUEST_METHOD_OPTIONS,
83 self::REQUEST_METHOD_UNKNOWN
84 )) === FALSE) throw new TX_EXTMVC_Exception_InvalidRequestMethod('The request method "' . $method . '" is not supported.', 1217778382);
85 $this->method = $method;
86 }
87
88 /**
89 * Returns the name of the request method
90 *
91 * @return string Name of the request method - one of the F3_FLOW3_Utility_Environment::REQUEST_METHOD_* constants
92 * @author Robert Lemke <robert@typo3.org>
93 */
94 public function getMethod() {
95 return $this->method;
96 }
97
98 /**
99 * Sets the request URI
100 *
101 * @param F3_FLOW3_Property_DataType_URI $requestURI URI of this web request
102 * @return void
103 * @author Robert Lemke <robert@typo3.org>
104 */
105 public function setRequestURI(F3_FLOW3_Property_DataType_URI $requestURI) {
106 $this->requestURI = clone $requestURI;
107 $this->baseURI = $this->detectBaseURI($requestURI);
108 }
109
110 /**
111 * Returns the request URI
112 *
113 * @return F3_FLOW3_Property_DataType_URI URI of this web request
114 * @author Robert Lemke <robert@typo3.org>
115 */
116 public function getRequestURI() {
117 return $this->requestURI;
118 }
119
120 /**
121 * Sets the base URI for this request.
122 *
123 * @param F3_FLOW3_Property_DataType_URI $baseURI New base URI
124 * @return void
125 * @author Robert Lemke <robert@typo3.org>
126 */
127 public function setBaseURI(F3_FLOW3_Property_DataType_URI $baseURI) {
128 $this->baseURI = clone $baseURI;
129 }
130
131 /**
132 * Returns the base URI
133 *
134 * @return F3_FLOW3_Property_DataType_URI Base URI of this web request
135 * @author Robert Lemke <robert@typo3.org>
136 */
137 public function getBaseURI() {
138 return $this->baseURI;
139 }
140
141 /**
142 * Tries to detect the base URI of this request and returns it.
143 *
144 * @param F3_FLOW3_Property_DataType_URI $requestURI URI of this web request
145 * @return F3_FLOW3_Property_DataType_URI The detected base URI
146 * @author Robert Lemke <robert@typo3.org>
147 */
148 protected function detectBaseURI(F3_FLOW3_Property_DataType_URI $requestURI) {
149 $baseURI = clone $requestURI;
150 $baseURI->setQuery(NULL);
151 $baseURI->setFragment(NULL);
152
153 $requestPathSegments = explode('/', $this->environment->getScriptRequestPathAndName());
154 array_pop($requestPathSegments);
155 $baseURI->setPath(implode('/', $requestPathSegments) . '/');
156 return $baseURI;
157 }
158 }
159 ?>