argument registration
[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 // SK: fill initializeArguments() so it parses the arguments for a given view. We need to discuss how this parsing can be
33 // SK: done effectively.
34 class Tx_ExtBase_MVC_Controller_ActionController extends Tx_ExtBase_MVC_Controller_AbstractController {
35
36 /**
37 * @var boolean If initializeView() should be called on an action invocation.
38 */
39 protected $initializeView = TRUE;
40
41 /**
42 * @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.
43 */
44 protected $view = NULL;
45
46 /**
47 * By default a matching view will be resolved. If this property is set, automatic resolving is disabled and the specified object is used instead.
48 * @var string
49 */
50 protected $defaultViewObjectName = NULL;
51
52 /**
53 * Pattern after which the view object name is built
54 *
55 * @var string
56 */
57 // SK: Decision: Do we support "format"?
58 protected $viewObjectNamePattern = 'Tx_@extension_View_@controller@action';
59
60 /**
61 * Name of the action method
62 * @var string
63 */
64 protected $actionMethodName = 'indexAction';
65
66 /**
67 * Handles a request. The result output is returned by altering the given response.
68 *
69 * @param Tx_ExtBase_MVC_Request $request The request object
70 * @param Tx_ExtBase_MVC_Response $response The response, modified by this handler
71 * @return void
72 */
73 public function processRequest(Tx_ExtBase_MVC_Request $request, Tx_ExtBase_MVC_Response $response) {
74 $this->request = $request;
75 $this->request->setDispatched(TRUE);
76 $this->response = $response;
77
78 $this->actionMethodName = $this->resolveActionMethodName();
79 $this->initializeActionMethodArguments();
80 $this->initializeArguments();
81 $this->mapRequestArgumentsToLocalArguments();
82 if ($this->initializeView) $this->initializeView();
83 $this->initializeAction();
84 $this->callActionMethod();
85 }
86
87 /**
88 * Implementation of the arguments initilization in the action controller:
89 * Automatically registers arguments of the current action
90 *
91 * Don't override this method - use initializeArguments() instead.
92 *
93 * @return void
94 * @author Robert Lemke <robert@typo3.org>
95 * @see initializeArguments()
96 * @internal
97 */
98 protected function initializeActionMethodArguments() {
99 $reflectionService = t3lib_div::makeInstance('Tx_ExtBase_Reflection_Service');
100 $methodParameters = $reflectionService->getMethodParameters(get_class($this), $this->actionMethodName);
101 foreach ($methodParameters as $parameterName => $parameterInfo) {
102 $dataType = 'Text';
103 if (isset($parameterInfo['type'])) {
104 $dataType = $parameterInfo['type'];
105 } elseif ($parameterInfo['array']) {
106 $dataType = 'array';
107 }
108
109 switch($dataType) {
110 case 'string':
111 $dataType = 'Text';
112 break;
113 case 'integer':
114 case 'int':
115 $dataType = 'Integer';
116 break;
117 }
118
119 $this->arguments->addNewArgument($parameterName, $dataType, ($parameterInfo['optional'] === FALSE));
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
160 $actionResult = call_user_func_array(array($this, $this->actionMethodName), $preparedArguments);
161 if ($actionResult === NULL && $this->view instanceof Tx_ExtBase_MVC_View_ViewInterface) {
162 $this->response->appendContent($this->view->render());
163 } elseif (is_string($actionResult) && strlen($actionResult) > 0) {
164 $this->response->appendContent($actionResult);
165 }
166 }
167
168 /**
169 * Prepares a view for the current action and stores it in $this->view.
170 * By default, this method tries to locate a view with a name matching
171 * the current action.
172 *
173 * @return void
174 */
175 protected function initializeView() {
176 $viewObjectName = ($this->defaultViewObjectName === NULL) ? $this->resolveViewObjectName() : $this->defaultViewObjectName;
177 if (!class_exists($viewObjectName)) $viewObjectName = 'Tx_ExtBase_MVC_View_EmptyView';
178
179 $this->view = t3lib_div::makeInstance($viewObjectName);
180 $this->view->setRequest($this->request);
181 $this->view->initializeView();
182 }
183
184 /**
185 * Determines the fully qualified view object name.
186 *
187 * @return string The fully qualified view object name
188 */
189 protected function resolveViewObjectName() {
190 $possibleViewName = $this->viewObjectNamePattern;
191 $extensionName = $this->request->getExtensionName();
192 $possibleViewName = str_replace('@extension', $extensionName, $possibleViewName);
193 $possibleViewName = str_replace('@controller', $this->request->getControllerName(), $possibleViewName);
194 $possibleViewName = str_replace('@action', ucfirst($this->request->getControllerActionName()), $possibleViewName);
195 return $possibleViewName;
196 }
197
198 /**
199 * Initializes the controller before invoking an action method.
200 *
201 * Override this method to solve tasks which all actions have in
202 * common.
203 *
204 * @return void
205 */
206 protected function initializeAction() {
207 }
208
209 /**
210 * The default action of this controller.
211 *
212 * This method should always be overridden by the concrete action
213 * controller implementation.
214 *
215 * @return void
216 */
217 protected function indexAction() {
218 return 'No index action has been implemented yet for this controller.';
219 }
220 }
221 ?>