* added some fixtures for the unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Controller / TX_EXTMVC_Controller_ActionController.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_AbstractController.php');
25
26 /**
27 * A multi action controller
28 *
29 * @version $Id:$
30 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31 */
32 class TX_EXTMVC_Controller_ActionController extends TX_EXTMVC_Controller_AbstractController {
33
34 /**
35 * @var TX_EXTMVC_View_AbstractView By default a view with the same name as the current action is provided. Contains NULL if none was found.
36 */
37 protected $view = NULL;
38
39 /**
40 * By default a matching view will be resolved. If this property is set, automatic resolving is disabled and the specified object is used instead.
41 * @var string
42 */
43 protected $viewObjectName = NULL;
44
45 /**
46 * Name of the action method
47 * @var string
48 */
49 protected $actionMethodName = 'indexAction';
50
51 /**
52 * Handles a request. The result output is returned by altering the given response.
53 *
54 * @param TX_EXTMVC_Request $request The request object
55 * @param TX_EXTMVC_Response $response The response, modified by this handler
56 * @return void
57 * @author Robert Lemke <robert@typo3.org>
58 */
59 public function processRequest(TX_EXTMVC_Request $request, TX_EXTMVC_Response $response) {
60 $this->request = $request;
61 $this->request->setDispatched(TRUE);
62 $this->response = $response;
63
64 $this->actionMethodName = $this->resolveActionMethodName();
65 // $this->initializeArguments();
66 // $this->mapRequestArgumentsToLocalArguments();
67 $this->initializeView();
68 $this->initializeAction();
69
70 $this->callActionMethod();
71 }
72
73 /**
74 * Implementation of the arguments initilization in the action controller:
75 * Automatically registers arguments of the current action
76 *
77 * IMPORTANT: If this method is overridden, make sure to call this parent
78 * _before_ your own code because otherwise the order of automatically
79 * registered arguments wouldn't match the action method signature.
80 *
81 * @return void
82 * @author Robert Lemke <robert@typo3.org>
83 */
84 protected function initializeArguments() {
85 $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), $this->actionMethodName);
86 $methodTagsAndValues = $this->reflectionService->getMethodTagsValues(get_class($this), $this->actionMethodName);
87 foreach ($methodParameters as $parameterName => $parameterInfo) {
88 $dataType = 'Text';
89 if (isset($methodTagsAndValues['param']) && count($methodTagsAndValues['param']) > 0) {
90 $explodedTagValue = explode(' ', array_shift($methodTagsAndValues['param']));
91 switch ($explodedTagValue[0]) {
92 case 'integer' :
93 $dataType = 'Integer';
94 break;
95 default:
96 if (strpos($dataType, '\\') !== FALSE) {
97 $dataType = $explodedTagValue[0];
98 }
99 }
100 }
101 $this->arguments->addNewArgument($parameterName, $dataType);
102 }
103 }
104
105 /**
106 * Determines the action method and assures that the method exists.
107 *
108 * @return string The action method name
109 * @throws TX_EXTMVC_Exception_NoSuchAction if the action specified in the request object does not exist (and if there's no default action either).
110 * @author Robert Lemke <robert@typo3.org>
111 */
112 protected function resolveActionMethodName() {
113 $actionMethodName = $this->request->getControllerActionName() . 'Action';
114 if (!method_exists($this, $actionMethodName)) throw new TX_EXTMVC_Exception_NoSuchAction('An action "' . $actionMethodName . '" does not exist in controller "' . get_class($this) . '".', 1186669086);
115 return $actionMethodName;
116 }
117
118 /**
119 * Calls the specified action method and passes the arguments.
120 * If the action returns a string, it is appended to the content in the
121 * response object.
122 *
123 * @param string $actionMethodName Name of the action method
124 * @return void
125 * @author Robert Lemke <robert@typo3.org>
126 */
127 protected function callActionMethod() {
128 $preparedArguments = array();
129 // foreach ($this->arguments as $argument) {
130 // $preparedArguments[] = $argument->getValue();
131 // }
132
133 $actionResult = call_user_func_array(array($this, $this->actionMethodName), $preparedArguments);
134 if (is_string($actionResult) && strlen($actionResult) > 0) {
135 $this->response->appendContent($actionResult);
136 }
137 }
138
139 /**
140 * Prepares a view for the current action and stores it in $this->view.
141 * By default, this method tries to locate a view with a name matching
142 * the current action.
143 *
144 * @return void
145 * @author Robert Lemke <robert@typo3.org>
146 */
147 protected function initializeView() {
148 $viewObjectName = ($this->viewObjectName === NULL) ? $this->request->getViewObjectName() : $this->viewObjectName;
149 if ($viewObjectName === FALSE) $viewObjectName = 'TX_EXTMVC_View_EmptyView';
150
151 $this->view = t3lib_div::makeInstance($viewObjectName);
152 $this->view->setRequest($this->request);
153 }
154
155 /**
156 * Initializes the controller before invoking an action method.
157 *
158 * Override this method to solve tasks which all actions have in
159 * common.
160 *
161 * @return void
162 * @author Robert Lemke <robert@typo3.org>
163 */
164 protected function initializeAction() {
165 }
166
167 /**
168 * The default action of this controller.
169 *
170 * This method should always be overridden by the concrete action
171 * controller implementation.
172 *
173 * @return void
174 * @author Robert Lemke <robert@typo3.org>
175 */
176 protected function indexAction() {
177 return 'No index action has been implemented yet for this controller.';
178 }
179 }
180 ?>