TYPO3 CMS  TYPO3_8-7
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 
80  public function __wakeup()
81  {
82  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
83  $this->commandManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class);
84  $this->taskExecutor = $this->objectManager->get(\TYPO3\CMS\Extbase\Scheduler\TaskExecutor::class);
85  }
86 
93  public function execute()
94  {
95  try {
96  $this->taskExecutor->execute($this);
97  } catch (\Exception $e) {
98  $this->logException($e);
99  // Make sure the Scheduler gets exception details
100  throw $e;
101  }
102  return true;
103  }
104 
109  {
110  $this->commandIdentifier = $commandIdentifier;
111  }
112 
116  public function getCommandIdentifier()
117  {
119  }
120 
124  public function setArguments($arguments)
125  {
126  $this->arguments = $arguments;
127  }
128 
132  public function getArguments()
133  {
134  return $this->arguments;
135  }
136 
140  public function setDefaults(array $defaults)
141  {
142  $this->defaults = $defaults;
143  }
144 
148  public function getDefaults()
149  {
150  return $this->defaults;
151  }
152 
157  public function addDefaultValue($argumentName, $argumentValue)
158  {
159  if (is_bool($argumentValue)) {
160  $argumentValue = (int)$argumentValue;
161  }
162  $this->defaults[$argumentName] = $argumentValue;
163  }
164 
170  public function getAdditionalInformation()
171  {
172  $label = $this->commandIdentifier;
173  if (!empty($this->arguments)) {
174  $arguments = [];
175  foreach ($this->arguments as $argumentName => $argumentValue) {
176  if ($argumentValue != $this->defaults[$argumentName]) {
177  $arguments[] = $argumentName . '=' . $argumentValue;
178  }
179  }
180  $label .= ' ' . implode(', ', $arguments);
181  }
182  return $label;
183  }
184 
188  protected function logException(\Exception $e)
189  {
190  \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog($e->getMessage(), $this->commandIdentifier, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
191  }
192 }
setDefaults(array $defaults)
Definition: Task.php:140
static makeInstance($className,... $constructorArguments)
addDefaultValue($argumentName, $argumentValue)
Definition: Task.php:157
setCommandIdentifier($commandIdentifier)
Definition: Task.php:108
logException(\Exception $e)
Definition: Task.php:188