[+FEATURE] Backport CommandController Implementation
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / CLI / Request.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 * All rights reserved
5 *
6 * This class is a backport of the corresponding class of FLOW3.
7 * All credits go to the v5 team.
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 *
18 * This script is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
25
26 /**
27 * Represents a CLI request.
28 *
29 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30 * @api
31 */
32 class Tx_Extbase_MVC_CLI_Request implements Tx_Extbase_MVC_RequestInterface {
33
34 /**
35 * @var Tx_Extbase_Object_ObjectManagerInterface
36 */
37 protected $objectManager;
38
39 /**
40 * @var string
41 */
42 protected $controllerObjectName;
43
44 /**
45 * @var string
46 */
47 protected $controllerCommandName = 'default';
48
49 /**
50 * @var string Name of the extension which is supposed to handle this request.
51 */
52 protected $controllerExtensionName = NULL;
53
54 /**
55 * The arguments for this request
56 * @var array
57 */
58 protected $arguments = array();
59
60 /**
61 * @var array
62 */
63 protected $exceedingArguments = array();
64
65 /**
66 * If this request has been changed and needs to be dispatched again
67 * @var boolean
68 */
69 protected $dispatched = FALSE;
70
71 /**
72 *
73 * @var array
74 */
75 protected $commandLineArguments;
76
77 /**
78 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager A reference to the object manager
79 * @return void
80 */
81 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
82 $this->objectManager = $objectManager;
83 }
84
85 /**
86 * Sets the dispatched flag
87 *
88 * @param boolean $flag If this request has been dispatched
89 * @return void
90 */
91 public function setDispatched($flag) {
92 $this->dispatched = $flag ? TRUE : FALSE;
93 }
94
95 /**
96 * If this request has been dispatched and addressed by the responsible
97 * controller and the response is ready to be sent.
98 *
99 * The dispatcher will try to dispatch the request again if it has not been
100 * addressed yet.
101 *
102 * @return boolean TRUE if this request has been disptached successfully
103 */
104 public function isDispatched() {
105 return $this->dispatched;
106 }
107
108 /**
109 * Sets the object name of the controller
110 *
111 * @param string $controllerObjectName The fully qualified controller object name
112 * @return void
113 */
114 public function setControllerObjectName($controllerObjectName) {
115 $matches = array();
116 preg_match('/
117 ^Tx
118 _(?P<extensionName>[^_]+)
119 _
120 (
121 Command
122 |
123 (?P<subpackageKey>.+)_Controller
124 )
125 _(?P<controllerName>[a-z_]+)Controller
126 $/ix', $controllerObjectName, $matches
127 );
128
129 $this->controllerExtensionName = $matches['extensionName'];
130 $this->controllerObjectName = $controllerObjectName;
131 $this->command = NULL;
132 }
133
134 /**
135 * Returns the object name of the controller
136 *
137 * @return string The controller's object name
138 */
139 public function getControllerObjectName() {
140 return $this->controllerObjectName;
141 }
142
143 /**
144 * Returns the extension name of the specified controller.
145 *
146 * @return string The extension name
147 */
148 public function getControllerExtensionName() {
149 return $this->controllerExtensionName;
150 }
151
152 /**
153 * Sets the name of the command contained in this request.
154 *
155 * Note that the command name must start with a lower case letter and is case sensitive.
156 *
157 * @param string $commandName Name of the command to execute by the controller
158 * @return void
159 */
160 public function setControllerCommandName($commandName) {
161 $this->controllerCommandName = $commandName;
162 $this->command = NULL;
163 }
164
165 /**
166 * Returns the name of the command the controller is supposed to execute.
167 *
168 * @return string Command name
169 */
170 public function getControllerCommandName() {
171 return $this->controllerCommandName;
172 }
173
174 /**
175 * Returns the command object for this request
176 *
177 * @return Tx_Extbase_MVC_CLI_Command
178 * @author Robert Lemke <robert@typo3.org>
179 */
180 public function getCommand() {
181 if ($this->command === NULL) {
182 $this->command = $this->objectManager->get('Tx_Extbase_MVC_CLI_Command', $this->controllerObjectName, $this->controllerCommandName);
183 }
184 return $this->command;
185 }
186
187 /**
188 * Sets the value of the specified argument
189 *
190 * @param string $argumentName Name of the argument to set
191 * @param mixed $value The new value
192 * @return void
193 */
194 public function setArgument($argumentName, $value) {
195 if (!is_string($argumentName) || $argumentName === '') throw new Tx_Extbase_MVC_Exception_InvalidArgumentName('Invalid argument name.', 1300893885);
196 $this->arguments[$argumentName] = $value;
197 }
198
199 /**
200 * Sets the whole arguments ArrayObject and therefore replaces any arguments
201 * which existed before.
202 *
203 * @param array $arguments An array of argument names and their values
204 * @return void
205 */
206 public function setArguments(array $arguments) {
207 $this->arguments = $arguments;
208 }
209
210 /**
211 * Returns the value of the specified argument
212 *
213 * @param string $argumentName Name of the argument
214 * @return string Value of the argument
215 * @throws Tx_Extbase_MVC_Exception_NoSuchArgument if such an argument does not exist
216 */
217 public function getArgument($argumentName) {
218 if (!isset($this->arguments[$argumentName])) throw new Tx_Extbase_MVC_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist for this request.', 1300893886);
219 return $this->arguments[$argumentName];
220 }
221
222 /**
223 * Checks if an argument of the given name exists (is set)
224 *
225 * @param string $argumentName Name of the argument to check
226 * @return boolean TRUE if the argument is set, otherwise FALSE
227 */
228 public function hasArgument($argumentName) {
229 return isset($this->arguments[$argumentName]);
230 }
231
232 /**
233 * Returns an ArrayObject of arguments and their values
234 *
235 * @return array Array of arguments and their values (which may be arguments and values as well)
236 */
237 public function getArguments() {
238 return $this->arguments;
239 }
240
241 /**
242 * Sets the exceeding arguments
243 *
244 * @param array $exceedingArguments Numeric array of exceeding arguments
245 * @return void
246 */
247 public function setExceedingArguments(array $exceedingArguments) {
248 $this->exceedingArguments = $exceedingArguments;
249 }
250
251 /**
252 * Returns additional unnamed arguments (if any) which have been passed through the command line after all
253 * required arguments (if any) have been specified.
254 *
255 * For a command method with the signature ($argument1, $argument2) and for the command line
256 * cli_dispatch.phpsh extbase some-key someaction acme:foo --argument1 Foo --argument2 Bar baz quux
257 * this method would return array(0 => 'baz', 1 => 'quux')
258 *
259 * @return array Numeric array of exceeding argument values
260 */
261 public function getExceedingArguments() {
262 return $this->exceedingArguments;
263 }
264 }
265 ?>