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