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