TYPO3 CMS  TYPO3_6-2
CommandController.php
Go to the documentation of this file.
1 <?php
3 
17 
24 
25  const MAXIMUM_LINE_LENGTH = 79;
26 
30  protected $request;
31 
35  protected $response;
36 
40  protected $arguments;
41 
47  protected $commandMethodName = '';
48 
55  protected $requestAdminPermissions = FALSE;
56 
61 
66  protected $reflectionService;
67 
71  protected $objectManager;
72 
77  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
78  $this->objectManager = $objectManager;
79  $this->arguments = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments');
80  $this->userAuthentication = isset($GLOBALS['BE_USER']) ? $GLOBALS['BE_USER'] : NULL;
81  }
82 
89  public function canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request) {
90  return $request instanceof \TYPO3\CMS\Extbase\Mvc\Cli\Request;
91  }
92 
102  public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response) {
103  if (!$this->canProcessRequest($request)) {
104  throw new \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException(get_class($this) . ' does not support requests of type "' . get_class($request) . '".', 1300787096);
105  }
106  $this->request = $request;
107  $this->request->setDispatched(TRUE);
108  $this->response = $response;
109  $this->commandMethodName = $this->resolveCommandMethodName();
112  $this->callCommandMethod();
113  }
114 
124  protected function resolveCommandMethodName() {
125  $commandMethodName = $this->request->getControllerCommandName() . 'Command';
126  if (!is_callable(array($this, $commandMethodName))) {
127  throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException('A command method "' . $commandMethodName . '()" does not exist in controller "' . get_class($this) . '".', 1300902143);
128  }
129  return $commandMethodName;
130  }
131 
139  protected function initializeCommandMethodArguments() {
140  $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), $this->commandMethodName);
141  foreach ($methodParameters as $parameterName => $parameterInfo) {
142  $dataType = NULL;
143  if (isset($parameterInfo['type'])) {
144  $dataType = $parameterInfo['type'];
145  } elseif ($parameterInfo['array']) {
146  $dataType = 'array';
147  }
148  if ($dataType === NULL) {
149  throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentTypeException('The argument type for parameter $' . $parameterName . ' of method ' . get_class($this) . '->' . $this->commandMethodName . '() could not be detected.', 1306755296);
150  }
151  $defaultValue = isset($parameterInfo['defaultValue']) ? $parameterInfo['defaultValue'] : NULL;
152  $this->arguments->addNewArgument($parameterName, $dataType, $parameterInfo['optional'] === FALSE, $defaultValue);
153  }
154  }
155 
162  foreach ($this->arguments as $argument) {
163  $argumentName = $argument->getName();
164  if ($this->request->hasArgument($argumentName)) {
165  $argument->setValue($this->request->getArgument($argumentName));
166  } elseif ($argument->isRequired()) {
167  $commandArgumentDefinition = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandArgumentDefinition', $argumentName, TRUE, NULL);
168  $exception = new \TYPO3\CMS\Extbase\Mvc\Exception\CommandException('Required argument "' . $commandArgumentDefinition->getDashedName() . '" is not set.', 1306755520);
169  $this->forward('error', 'TYPO3\\CMS\\Extbase\\Command\\HelpCommandController', array('exception' => $exception));
170  }
171  }
172  }
173 
186  protected function forward($commandName, $controllerObjectName = NULL, array $arguments = array()) {
187  $this->request->setDispatched(FALSE);
188  $this->request->setControllerCommandName($commandName);
189  if ($controllerObjectName !== NULL) {
190  $this->request->setControllerObjectName($controllerObjectName);
191  }
192  $this->request->setArguments($arguments);
193  $this->arguments->removeAll();
194  throw new \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException();
195  }
196 
206  protected function callCommandMethod() {
207  $preparedArguments = array();
208  foreach ($this->arguments as $argument) {
209  $preparedArguments[] = $argument->getValue();
210  }
211  $originalRole = $this->ensureAdminRoleIfRequested();
212  $commandResult = call_user_func_array(array($this, $this->commandMethodName), $preparedArguments);
213  $this->restoreUserRole($originalRole);
214  if (is_string($commandResult) && strlen($commandResult) > 0) {
215  $this->response->appendContent($commandResult);
216  } elseif (is_object($commandResult) && method_exists($commandResult, '__toString')) {
217  $this->response->appendContent((string) $commandResult);
218  }
219  }
220 
227  protected function ensureAdminRoleIfRequested() {
228  if (!$this->requestAdminPermissions || !$this->userAuthentication || !isset($this->userAuthentication->user['admin'])) {
229  return NULL;
230  }
231  $originalRole = $this->userAuthentication->user['admin'];
232  $this->userAuthentication->user['admin'] = 1;
233  return $originalRole;
234  }
235 
241  protected function restoreUserRole($originalRole) {
242  if ($originalRole !== NULL) {
243  $this->userAuthentication->user['admin'] = $originalRole;
244  }
245  }
246 
256  protected function output($text, array $arguments = array()) {
257  if ($arguments !== array()) {
258  $text = vsprintf($text, $arguments);
259  }
260  $this->response->appendContent($text);
261  }
262 
271  protected function outputLine($text = '', array $arguments = array()) {
272  return $this->output($text . PHP_EOL, $arguments);
273  }
274 
283  protected function quit($exitCode = 0) {
284  $this->response->setExitCode($exitCode);
285  throw new \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException();
286  }
287 
295  protected function sendAndExit($exitCode = 0) {
296  $this->response->send();
297  die($exitCode);
298  }
299 }
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
forward($commandName, $controllerObjectName=NULL, array $arguments=array())
die
Definition: index.php:6
outputLine($text='', array $arguments=array())
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)