ExtBase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Controller / ActionController.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 * A multi action controller
27 *
28 * @package TYPO3
29 * @subpackage extbase
30 * @version $ID:$
31 */
32 class Tx_ExtBase_MVC_Controller_ActionController extends Tx_ExtBase_MVC_Controller_AbstractController {
33
34 /**
35 * @var boolean If initializeView() should be called on an action invocation.
36 */
37 protected $initializeView = TRUE;
38
39 /**
40 * @var Tx_ExtBase_MVC_View_AbstractView By default a view with the same name as the current action is provided. Contains NULL if none was found.
41 */
42 protected $view = NULL;
43
44 /**
45 * By default a matching view will be resolved. If this property is set, automatic resolving is disabled and the specified object is used instead.
46 * @var string
47 */
48 protected $defaultViewObjectName = 'Tx_Fluid_View_TemplateView';
49
50 /**
51 * Pattern after which the view object name is built
52 *
53 * @var string
54 */
55 // SK: Decision: Do we support "format"?
56 protected $viewObjectNamePattern = 'Tx_@extension_View_@controller@action';
57
58 /**
59 * Name of the action method
60 * @var string
61 */
62 protected $actionMethodName = 'indexAction';
63
64 /**
65 * Handles a request. The result output is returned by altering the given response.
66 *
67 * @param Tx_ExtBase_MVC_Request $request The request object
68 * @param Tx_ExtBase_MVC_Response $response The response, modified by this handler
69 * @return void
70 */
71 public function processRequest(Tx_ExtBase_MVC_Request $request, Tx_ExtBase_MVC_Response $response) {
72 $this->request = $request;
73 $this->request->setDispatched(TRUE);
74 $this->response = $response;
75
76 $this->actionMethodName = $this->resolveActionMethodName();
77 $this->initializeActionMethodArguments();
78 $this->initializeArguments();
79 $this->mapRequestArgumentsToLocalArguments();
80 if ($this->initializeView) $this->initializeView();
81 $this->initializeAction();
82 $this->callActionMethod();
83 }
84
85 /**
86 * Implementation of the arguments initilization in the action controller:
87 * Automatically registers arguments of the current action
88 *
89 * Don't override this method - use initializeArguments() instead.
90 *
91 * @return void
92 * @author Robert Lemke <robert@typo3.org>
93 * @see initializeArguments()
94 * @internal
95 */
96 protected function initializeActionMethodArguments() {
97 $reflectionService = t3lib_div::makeInstance('Tx_ExtBase_Reflection_Service');
98 $methodParameters = $reflectionService->getMethodParameters(get_class($this), $this->actionMethodName);
99 foreach ($methodParameters as $parameterName => $parameterInfo) {
100 $dataType = 'Text';
101 if (isset($parameterInfo['type'])) {
102 $dataType = $parameterInfo['type'];
103 } elseif ($parameterInfo['array']) {
104 $dataType = 'array';
105 }
106
107 switch($dataType) {
108 case 'string':
109 $dataType = 'Text';
110 break;
111 case 'integer':
112 case 'int':
113 $dataType = 'Integer';
114 break;
115 }
116
117 $defaultValue = (isset($parameterInfo['defaultValue']) ? $parameterInfo['defaultValue'] : NULL);
118
119 $this->arguments->addNewArgument($parameterName, $dataType, ($parameterInfo['optional'] === FALSE), $defaultValue);
120 }
121 }
122
123 /**
124 * Determines the action method and assures that the method exists.
125 *
126 * @return string The action method name
127 * @throws Tx_ExtBase_Exception_NoSuchAction if the action specified in the request object does not exist (and if there's no default action either).
128 */
129 protected function resolveActionMethodName() {
130 $actionMethodName = $this->request->getControllerActionName() . 'Action';
131 if (!method_exists($this, $actionMethodName)) throw new Tx_ExtBase_Exception_NoSuchAction('An action "' . $actionMethodName . '" does not exist in controller "' . get_class($this) . '".', 1186669086);
132 return $actionMethodName;
133 }
134
135 /**
136 * Returns TRUE if the given action (a name of an action like 'show'; without
137 * trailing 'Action') should be cached, otherwise it returns FALSE.
138 *
139 * @param string $actionName
140 * @return void
141 * @author Jochen Rau <jochen.rau@typoplanet.de>
142 */
143 public function isCachableAction($actionName) {
144 return !in_array($actionName, $this->nonCachableActions);
145 }
146
147 /**
148 * Calls the specified action method and passes the arguments.
149 * If the action returns a string, it is appended to the content in the
150 * response object.
151 *
152 * @param string $actionMethodName Name of the action method
153 * @return void
154 */
155 protected function callActionMethod() {
156 foreach ($this->arguments as $argument) {
157 $preparedArguments[] = $argument->getValue();
158 }
159 $actionResult = call_user_func_array(array($this, $this->actionMethodName), $preparedArguments);
160 if ($actionResult === NULL && $this->view instanceof Tx_ExtBase_MVC_View_ViewInterface) {
161 $this->response->appendContent($this->view->render());
162 } elseif (is_string($actionResult) && strlen($actionResult) > 0) {
163 $this->response->appendContent($actionResult);
164 }
165 }
166
167 /**
168 * Prepares a view for the current action and stores it in $this->view.
169 * By default, this method tries to locate a view with a name matching
170 * the current action.
171 *
172 * @return void
173 */
174 protected function initializeView() {
175 $viewObjectName = ($this->defaultViewObjectName === NULL) ? $this->resolveViewObjectName() : $this->defaultViewObjectName;
176 if (!class_exists($viewObjectName)) $viewObjectName = 'Tx_ExtBase_MVC_View_EmptyView';
177
178 $this->view = t3lib_div::makeInstance($viewObjectName);
179 $this->view->setRequest($this->request);
180 $this->view->initializeView();
181 }
182
183 /**
184 * Determines the fully qualified view object name.
185 *
186 * @return string The fully qualified view object name
187 */
188 protected function resolveViewObjectName() {
189 $possibleViewName = $this->viewObjectNamePattern;
190 $extensionName = $this->request->getExtensionName();
191 $possibleViewName = str_replace('@extension', $extensionName, $possibleViewName);
192 $possibleViewName = str_replace('@controller', $this->request->getControllerName(), $possibleViewName);
193 $possibleViewName = str_replace('@action', ucfirst($this->request->getControllerActionName()), $possibleViewName);
194 return $possibleViewName;
195 }
196
197 /**
198 * Initializes the controller before invoking an action method.
199 *
200 * Override this method to solve tasks which all actions have in
201 * common.
202 *
203 * @return void
204 */
205 protected function initializeAction() {
206 }
207
208 /**
209 * The default action of this controller.
210 *
211 * This method should always be overridden by the concrete action
212 * controller implementation.
213 *
214 * @return void
215 */
216 protected function indexAction() {
217 return 'No index action has been implemented yet for this controller.';
218 }
219 }
220 ?>