TYPO3 CMS  TYPO3_7-6
Task.php
Go to the documentation of this file.
1 <?php
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 
21 {
25  protected $commandIdentifier;
26 
30  protected $arguments = [];
31 
35  protected $defaults = [];
36 
40  protected $objectManager;
41 
45  protected $commandManager;
46 
50  protected $taskExecutor;
51 
55  public function __construct()
56  {
57  parent::__construct();
58  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
59  $this->commandManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class);
60  $this->taskExecutor = $this->objectManager->get(\TYPO3\CMS\Extbase\Scheduler\TaskExecutor::class);
61  }
62 
68  public function __sleep()
69  {
70  $properties = get_object_vars($this);
71  unset($properties['commandManager']);
72  unset($properties['objectManager']);
73  unset($properties['taskExecutor']);
74  return array_keys($properties);
75  }
76 
82  public function __wakeup()
83  {
84  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
85  $this->commandManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class);
86  $this->taskExecutor = $this->objectManager->get(\TYPO3\CMS\Extbase\Scheduler\TaskExecutor::class);
87  }
88 
95  public function execute()
96  {
97  try {
98  $this->taskExecutor->execute($this);
99  } catch (\Exception $e) {
100  $this->logException($e);
101  // Make sure the Scheduler gets exception details
102  throw $e;
103  }
104  return true;
105  }
106 
111  {
112  $this->commandIdentifier = $commandIdentifier;
113  }
114 
118  public function getCommandIdentifier()
119  {
121  }
122 
126  public function setArguments($arguments)
127  {
128  $this->arguments = $arguments;
129  }
130 
134  public function getArguments()
135  {
136  return $this->arguments;
137  }
138 
142  public function setDefaults(array $defaults)
143  {
144  $this->defaults = $defaults;
145  }
146 
150  public function getDefaults()
151  {
152  return $this->defaults;
153  }
154 
159  public function addDefaultValue($argumentName, $argumentValue)
160  {
161  if (is_bool($argumentValue)) {
162  $argumentValue = (int)$argumentValue;
163  }
164  $this->defaults[$argumentName] = $argumentValue;
165  }
166 
172  public function getAdditionalInformation()
173  {
174  $label = $this->commandIdentifier;
175  if (!empty($this->arguments)) {
176  $arguments = [];
177  foreach ($this->arguments as $argumentName => $argumentValue) {
178  if ($argumentValue != $this->defaults[$argumentName]) {
179  array_push($arguments, $argumentName . '=' . $argumentValue);
180  }
181  }
182  $label .= ' ' . implode(', ', $arguments);
183  }
184  return $label;
185  }
186 
190  protected function logException(\Exception $e)
191  {
192  \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog($e->getMessage(), $this->commandIdentifier, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
193  }
194 }
setDefaults(array $defaults)
Definition: Task.php:142
addDefaultValue($argumentName, $argumentValue)
Definition: Task.php:159
setCommandIdentifier($commandIdentifier)
Definition: Task.php:110
logException(\Exception $e)
Definition: Task.php:190