[TASK] Deprecate Extbase CommandControllers and @cli annotation
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Cli / Request.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Cli;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Represents a CLI request.
19 *
20 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
21 * @api
22 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0. Use symfony/console commands instead.
23 */
24 class Request implements \TYPO3\CMS\Extbase\Mvc\RequestInterface
25 {
26 /**
27 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
28 */
29 protected $objectManager;
30
31 /**
32 * @var string
33 */
34 protected $controllerObjectName;
35
36 /**
37 * @var string
38 */
39 protected $controllerCommandName = 'default';
40
41 /**
42 * @var string Name of the extension which is supposed to handle this request.
43 */
44 protected $controllerExtensionName;
45
46 /**
47 * The arguments for this request
48 *
49 * @var array
50 */
51 protected $arguments = [];
52
53 /**
54 * @var array
55 */
56 protected $exceedingArguments = [];
57
58 /**
59 * If this request has been changed and needs to be dispatched again
60 *
61 * @var bool
62 */
63 protected $dispatched = false;
64
65 /**
66 * @var array
67 */
68 protected $commandLineArguments;
69
70 /**
71 * @var \TYPO3\CMS\Extbase\Mvc\Cli\Command | NULL
72 */
73 protected $command;
74
75 /**
76 * @var string
77 */
78 protected $callingScript;
79
80 /**
81 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
82 */
83 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
84 {
85 $this->objectManager = $objectManager;
86 }
87
88 /**
89 * @param string $callingScript
90 */
91 public function setCallingScript($callingScript)
92 {
93 $this->callingScript = $callingScript;
94 }
95
96 /**
97 * @return string
98 */
99 public function getCallingScript()
100 {
101 return $this->callingScript;
102 }
103
104 /**
105 * Sets the dispatched flag
106 *
107 * @param bool $flag If this request has been dispatched
108 */
109 public function setDispatched($flag)
110 {
111 $this->dispatched = (bool)$flag;
112 }
113
114 /**
115 * If this request has been dispatched and addressed by the responsible
116 * controller and the response is ready to be sent.
117 *
118 * The dispatcher will try to dispatch the request again if it has not been
119 * addressed yet.
120 *
121 * @return bool TRUE if this request has been disptached successfully
122 */
123 public function isDispatched()
124 {
125 return $this->dispatched;
126 }
127
128 /**
129 * Sets the object name of the controller
130 *
131 * @param string $controllerObjectName The fully qualified controller object name
132 */
133 public function setControllerObjectName($controllerObjectName)
134 {
135 $nameParts = \TYPO3\CMS\Core\Utility\ClassNamingUtility::explodeObjectControllerName($controllerObjectName);
136
137 $this->controllerExtensionName = $nameParts['extensionName'];
138 $this->controllerObjectName = $controllerObjectName;
139 $this->command = null;
140 }
141
142 /**
143 * Returns the object name of the controller
144 *
145 * @return string The controller's object name
146 */
147 public function getControllerObjectName()
148 {
149 return $this->controllerObjectName;
150 }
151
152 /**
153 * Returns the extension name of the specified controller.
154 *
155 * @return string The extension name
156 */
157 public function getControllerExtensionName()
158 {
159 return $this->controllerExtensionName;
160 }
161
162 /**
163 * Sets the name of the command contained in this request.
164 *
165 * Note that the command name must start with a lower case letter and is case sensitive.
166 *
167 * @param string $commandName Name of the command to execute by the controller
168 */
169 public function setControllerCommandName($commandName)
170 {
171 $this->controllerCommandName = $commandName;
172 $this->command = null;
173 }
174
175 /**
176 * Returns the name of the command the controller is supposed to execute.
177 *
178 * @return string Command name
179 */
180 public function getControllerCommandName()
181 {
182 return $this->controllerCommandName;
183 }
184
185 /**
186 * Returns the command object for this request
187 *
188 * @return \TYPO3\CMS\Extbase\Mvc\Cli\Command
189 */
190 public function getCommand()
191 {
192 if ($this->command === null) {
193 $this->command = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, $this->controllerObjectName, $this->controllerCommandName);
194 }
195 return $this->command;
196 }
197
198 /**
199 * Sets the value of the specified argument
200 *
201 * @param string $argumentName Name of the argument to set
202 * @param mixed $value The new value
203 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException
204 */
205 public function setArgument($argumentName, $value)
206 {
207 if (!is_string($argumentName) || $argumentName === '') {
208 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException('Invalid argument name.', 1300893885);
209 }
210 $this->arguments[$argumentName] = $value;
211 }
212
213 /**
214 * Sets the whole arguments ArrayObject and therefore replaces any arguments
215 * which existed before.
216 *
217 * @param array $arguments An array of argument names and their values
218 */
219 public function setArguments(array $arguments)
220 {
221 $this->arguments = $arguments;
222 }
223
224 /**
225 * Returns the value of the specified argument
226 *
227 * @param string $argumentName Name of the argument
228 * @return string Value of the argument
229 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException if such an argument does not exist
230 */
231 public function getArgument($argumentName)
232 {
233 if (!isset($this->arguments[$argumentName])) {
234 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException('An argument "' . $argumentName . '" does not exist for this request.', 1300893886);
235 }
236 return $this->arguments[$argumentName];
237 }
238
239 /**
240 * Checks if an argument of the given name exists (is set)
241 *
242 * @param string $argumentName Name of the argument to check
243 * @return bool TRUE if the argument is set, otherwise FALSE
244 */
245 public function hasArgument($argumentName)
246 {
247 return isset($this->arguments[$argumentName]);
248 }
249
250 /**
251 * Returns an ArrayObject of arguments and their values
252 *
253 * @return array Array of arguments and their values (which may be arguments and values as well)
254 */
255 public function getArguments()
256 {
257 return $this->arguments;
258 }
259
260 /**
261 * Sets the exceeding arguments
262 *
263 * @param array $exceedingArguments Numeric array of exceeding arguments
264 */
265 public function setExceedingArguments(array $exceedingArguments)
266 {
267 $this->exceedingArguments = $exceedingArguments;
268 }
269
270 /**
271 * Returns additional unnamed arguments (if any) which have been passed through the command line after all
272 * required arguments (if any) have been specified.
273 *
274 * For a command method with the signature ($argument1, $argument2) and for the command line
275 * typo3/sysext/core/bin/typo3 acme:foo --argument1 Foo --argument2 Bar baz quux
276 * this method would return array(0 => 'baz', 1 => 'quux')
277 *
278 * @return array Numeric array of exceeding argument values
279 */
280 public function getExceedingArguments()
281 {
282 return $this->exceedingArguments;
283 }
284 }