* added some fixtures for the unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Controller / TX_EXTMVC_Controller_AbstractController.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 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Controller/TX_EXTMVC_Controller_ControllerInterface.php');
25 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Exception/TX_EXTMVC_Exception_StopAction.php');
26
27 /**
28 * An abstract base class for Controllers
29 *
30 * @version $Id:$
31 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
32 */
33 abstract class TX_EXTMVC_Controller_AbstractController implements TX_EXTMVC_Controller_ControllerInterface {
34
35 /**
36 * @var string Key of the extension this controller belongs to
37 */
38 protected $extensionKey;
39
40 /**
41 * Contains the settings of the current extension
42 *
43 * @var array
44 */
45 protected $settings;
46
47 /**
48 * @var TX_EXTMVC_Request The current request
49 */
50 protected $request;
51
52 /**
53 * @var TX_EXTMVC_Response The response which will be returned by this action controller
54 */
55 protected $response;
56
57 /**
58 * @var TX_EXTMVC_Controller_Arguments Arguments passed to the controller
59 */
60 protected $arguments;
61
62 /**
63 * Constructs the controller.
64 *
65 * @param F3_FLOW3_Object_FactoryInterface $objectFactory A reference to the Object Factory
66 * @param F3_FLOW3_Package_ManagerInterface $packageManager A reference to the Package Manager
67 * @author Robert Lemke <robert@typo3.org>
68 */
69 public function __construct() {
70 // $this->arguments = $objectFactory->create('TX_EXTMVC_Controller_Arguments');
71 list(, $this->extensionKey) = explode('_', strtolower(get_class($this)));
72 // debug($this->extensionKey));
73 // $this->extension = $packageManager->getPackage($this->extensionKey);
74 }
75
76 /**
77 * Injects the settings of the extension.
78 *
79 * @param array $settings Settings container of the current extension
80 * @return void
81 * @author Robert Lemke <robert@typo3.org>
82 */
83 public function injectSettings(array $settings) {
84 $this->settings = $settings;
85 }
86
87 /**
88 * Processes a general request. The result can be returned by altering the given response.
89 *
90 * @param TX_EXTMVC_Request $request The request object
91 * @param TX_EXTMVC_Response $response The response, modified by this handler
92 * @return void
93 * @throws TX_EXTMVC_Exception_UnsupportedRequestType if the controller doesn't support the current request type
94 * @author Robert Lemke <robert@typo3.org>
95 */
96 public function processRequest(TX_EXTMVC_Request $request, TX_EXTMVC_Response $response) {
97 $this->request = $request;
98 $this->request->setDispatched(TRUE);
99 $this->response = $response;
100
101 // $this->initializeArguments();
102 // $this->mapRequestArgumentsToLocalArguments();
103 }
104
105 /**
106 * Initializes (registers / defines) arguments of this controller.
107 *
108 * Override this method to add arguments which can later be accessed
109 * by the action methods.
110 *
111 * @return void
112 * @author Robert Lemke <robert@typo3.org>
113 */
114 protected function initializeArguments() {
115 }
116
117 /**
118 * Forwards the request to another controller.
119 *
120 * @return void
121 * @throws TX_EXTMVC_Exception_StopAction
122 * @author Robert Lemke <robert@typo3.org>
123 */
124 public function forward($actionName, $controllerName = NULL, $extensionKey = NULL, TX_EXTMVC_Controller_Arguments $arguments = NULL) {
125 $this->request->setDispatched(FALSE);
126 $this->request->setControllerActionName($actionName);
127 if ($controllerName !== NULL) $this->request->setControllerName($controllerName);
128 if ($extensionKey !== NULL) $this->request->setControllerExtensionKey($extensionKey);
129 if ($arguments !== NULL) $this->request->setArguments($arguments);
130 throw new TX_EXTMVC_Exception_StopAction();
131 }
132
133 /**
134 * Redirects the web request to another uri.
135 *
136 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
137 *
138 * @param mixed $uri Either a string representation of a URI or a F3_FLOW3_Property_DataType_URI object
139 * @param integer $delay (optional) The delay in seconds. Default is no delay.
140 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
141 * @throws TX_EXTMVC_Exception_UnsupportedRequestType If the request is not a web request
142 * @throws TX_EXTMVC_Exception_StopAction
143 * @author Robert Lemke <robert@typo3.org>
144 */
145 public function redirect($uri, $delay = 0, $statusCode = 303) {
146 if (!$this->request instanceof TX_EXTMVC_Web_Request) throw new TX_EXTMVC_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
147
148 $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
149 $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . intval($delay) . ';url=' . $escapedUri . '"/></head></html>');
150 $this->response->setStatus($statusCode);
151 $this->response->setHeader('Location', (string)$uri);
152 throw new TX_EXTMVC_Exception_StopAction();
153 }
154
155 /**
156 * Sends the specified HTTP status immediately.
157 *
158 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
159 *
160 * @param integer $statusCode The HTTP status code
161 * @param string $statusMessage A custom HTTP status message
162 * @param string $content Body content which further explains the status
163 * @throws TX_EXTMVC_Exception_UnsupportedRequestType If the request is not a web request
164 * @throws TX_EXTMVC_Exception_StopAction
165 * @author Robert Lemke <robert@typo3.org>
166 */
167 public function throwStatus($statusCode, $statusMessage = NULL, $content = NULL) {
168 if (!$this->request instanceof TX_EXTMVC_Web_Request) throw new TX_EXTMVC_Exception_UnsupportedRequestType('throwStatus() only supports web requests.', 1220539739);
169
170 $this->response->setStatus($statusCode, $statusMessage);
171 if ($content === NULL) $content = $this->response->getStatus();
172 $this->response->setContent($content);
173 throw new TX_EXTMVC_Exception_StopAction();
174 }
175
176 /**
177 * Maps arguments delivered by the request object to the local controller arguments.
178 *
179 * @return void
180 * @author Robert Lemke <robert@typo3.org>
181 */
182 protected function mapRequestArgumentsToLocalArguments() {
183 $this->propertyMapper->setTarget($this->arguments);
184 foreach ($this->arguments as $argument) {
185 if ($argument->getFilter() !== NULL) $this->propertyMapper->registerFilter($argument->getFilter(), $argument->getName());
186 if ($argument->getPropertyConverter() !== NULL) $this->propertyMapper->registerPropertyConverter($argument->getPropertyConverter(), $argument->getName(), $argument->getPropertyConverterInputFormat());
187 }
188
189 $argumentsValidator = t3lib_div::makeInstance('TX_EXTMVC_Controller_ArgumentsValidator', $this->arguments);
190 $this->propertyMapper->registerValidator($argumentsValidator);
191 $this->propertyMapper->setAllowedProperties(array_merge($this->arguments->getArgumentNames(), $this->arguments->getArgumentShortNames()));
192 $this->propertyMapper->map($this->request->getArguments());
193
194 $this->argumentMappingResults = $this->propertyMapper->getMappingResults();
195
196 foreach ($this->argumentMappingResults->getErrors() as $propertyName => $error) {
197 if (isset($this->arguments[$propertyName])) {
198 $this->arguments[$propertyName]->setValidity(FALSE);
199 $this->arguments[$propertyName]->addError($error);
200 }
201 }
202
203 foreach ($this->argumentMappingResults->getWarnings() as $propertyName => $warning) {
204 if (isset($this->arguments[$propertyName])) $this->arguments[$propertyName]->addWarning($warning);
205 }
206
207 foreach ($this->argumentMappingResults->getUids() as $propertyName => $uid) {
208 if (isset($this->arguments[$propertyName])) $this->arguments[$propertyName]->setUid($uid);
209 }
210 }
211
212 }
213
214 ?>