[TASK] Replace inject methods with @inject
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Cli / Request.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Cli;
3
4 /***************************************************************
5 * Copyright notice
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of TYPO3 Flow.
9 * All credits go to the TYPO3 Flow team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Represents a CLI request.
29 *
30 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31 * @api
32 */
33 class Request implements \TYPO3\CMS\Extbase\Mvc\RequestInterface {
34
35 /**
36 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
37 * @inject
38 */
39 protected $objectManager;
40
41 /**
42 * @var string
43 */
44 protected $controllerObjectName;
45
46 /**
47 * @var string
48 */
49 protected $controllerCommandName = 'default';
50
51 /**
52 * @var string Name of the extension which is supposed to handle this request.
53 */
54 protected $controllerExtensionName = NULL;
55
56 /**
57 * The arguments for this request
58 *
59 * @var array
60 */
61 protected $arguments = array();
62
63 /**
64 * @var array
65 */
66 protected $exceedingArguments = array();
67
68 /**
69 * If this request has been changed and needs to be dispatched again
70 *
71 * @var boolean
72 */
73 protected $dispatched = FALSE;
74
75 /**
76 * @var array
77 */
78 protected $commandLineArguments;
79
80 /**
81 * @var \TYPO3\CMS\Extbase\Mvc\Cli\Command | NULL
82 */
83 protected $command = NULL;
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 $this->controllerExtensionName = $matches['extensionName'];
128 $this->controllerObjectName = $controllerObjectName;
129 $this->command = NULL;
130 }
131
132 /**
133 * Returns the object name of the controller
134 *
135 * @return string The controller's object name
136 */
137 public function getControllerObjectName() {
138 return $this->controllerObjectName;
139 }
140
141 /**
142 * Returns the extension name of the specified controller.
143 *
144 * @return string The extension name
145 */
146 public function getControllerExtensionName() {
147 return $this->controllerExtensionName;
148 }
149
150 /**
151 * Sets the name of the command contained in this request.
152 *
153 * Note that the command name must start with a lower case letter and is case sensitive.
154 *
155 * @param string $commandName Name of the command to execute by the controller
156 * @return void
157 */
158 public function setControllerCommandName($commandName) {
159 $this->controllerCommandName = $commandName;
160 $this->command = NULL;
161 }
162
163 /**
164 * Returns the name of the command the controller is supposed to execute.
165 *
166 * @return string Command name
167 */
168 public function getControllerCommandName() {
169 return $this->controllerCommandName;
170 }
171
172 /**
173 * Returns the command object for this request
174 *
175 * @return \TYPO3\CMS\Extbase\Mvc\Cli\Command
176 */
177 public function getCommand() {
178 if ($this->command === NULL) {
179 $this->command = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', $this->controllerObjectName, $this->controllerCommandName);
180 }
181 return $this->command;
182 }
183
184 /**
185 * Sets the value of the specified argument
186 *
187 * @param string $argumentName Name of the argument to set
188 * @param mixed $value The new value
189 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException
190 * @return void
191 */
192 public function setArgument($argumentName, $value) {
193 if (!is_string($argumentName) || $argumentName === '') {
194 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException('Invalid argument name.', 1300893885);
195 }
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 \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException if such an argument does not exist
216 */
217 public function getArgument($argumentName) {
218 if (!isset($this->arguments[$argumentName])) {
219 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException('An argument "' . $argumentName . '" does not exist for this request.', 1300893886);
220 }
221 return $this->arguments[$argumentName];
222 }
223
224 /**
225 * Checks if an argument of the given name exists (is set)
226 *
227 * @param string $argumentName Name of the argument to check
228 * @return boolean TRUE if the argument is set, otherwise FALSE
229 */
230 public function hasArgument($argumentName) {
231 return isset($this->arguments[$argumentName]);
232 }
233
234 /**
235 * Returns an ArrayObject of arguments and their values
236 *
237 * @return array Array of arguments and their values (which may be arguments and values as well)
238 */
239 public function getArguments() {
240 return $this->arguments;
241 }
242
243 /**
244 * Sets the exceeding arguments
245 *
246 * @param array $exceedingArguments Numeric array of exceeding arguments
247 * @return void
248 */
249 public function setExceedingArguments(array $exceedingArguments) {
250 $this->exceedingArguments = $exceedingArguments;
251 }
252
253 /**
254 * Returns additional unnamed arguments (if any) which have been passed through the command line after all
255 * required arguments (if any) have been specified.
256 *
257 * For a command method with the signature ($argument1, $argument2) and for the command line
258 * cli_dispatch.phpsh extbase some-key someaction acme:foo --argument1 Foo --argument2 Bar baz quux
259 * this method would return array(0 => 'baz', 1 => 'quux')
260 *
261 * @return array Numeric array of exceeding argument values
262 */
263 public function getExceedingArguments() {
264 return $this->exceedingArguments;
265 }
266 }
267
268 ?>