[TASK] Make TaskTest notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Scheduler / Task.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Scheduler;
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Mvc\Cli\CommandManager;
19 use TYPO3\CMS\Extbase\Object\ObjectManager;
20 use TYPO3\CMS\Scheduler\Task\AbstractTask;
21
22 /**
23 * Scheduler task to execute CommandController commands
24 */
25 class Task extends AbstractTask
26 {
27 /**
28 * @var string
29 */
30 protected $commandIdentifier;
31
32 /**
33 * @var array
34 */
35 protected $arguments = [];
36
37 /**
38 * @var array
39 */
40 protected $defaults = [];
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
44 */
45 protected $objectManager;
46
47 /**
48 * @var CommandManager
49 */
50 protected $commandManager;
51
52 /**
53 * @var TaskExecutor
54 */
55 protected $taskExecutor;
56
57 /**
58 * Instantiates the Object Manager
59 */
60 public function __construct()
61 {
62 parent::__construct();
63 $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
64 $this->commandManager = $this->objectManager->get(CommandManager::class);
65 $this->taskExecutor = $this->objectManager->get(TaskExecutor::class);
66 }
67
68 /**
69 * Sleep
70 *
71 * @return array Properties to serialize
72 */
73 public function __sleep()
74 {
75 $properties = get_object_vars($this);
76 unset($properties['commandManager']);
77 unset($properties['objectManager']);
78 unset($properties['taskExecutor']);
79 return array_keys($properties);
80 }
81
82 /**
83 * Wakeup
84 */
85 public function __wakeup()
86 {
87 $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
88 $this->commandManager = $this->objectManager->get(CommandManager::class);
89 $this->taskExecutor = $this->objectManager->get(TaskExecutor::class);
90 }
91
92 /**
93 * Function execute from the Scheduler
94 *
95 * @return bool TRUE on successful execution
96 * @throws \Exception If an error occurs
97 */
98 public function execute()
99 {
100 try {
101 $this->taskExecutor->execute($this);
102 } catch (\Exception $e) {
103 $this->logException($e);
104 // Make sure the Scheduler gets exception details
105 throw $e;
106 }
107 return true;
108 }
109
110 /**
111 * @param string $commandIdentifier
112 */
113 public function setCommandIdentifier($commandIdentifier)
114 {
115 $this->commandIdentifier = $commandIdentifier;
116 }
117
118 /**
119 * @return string
120 */
121 public function getCommandIdentifier()
122 {
123 return $this->commandIdentifier;
124 }
125
126 /**
127 * @param array $arguments
128 */
129 public function setArguments($arguments)
130 {
131 $this->arguments = $arguments;
132 }
133
134 /**
135 * @return array
136 */
137 public function getArguments()
138 {
139 return $this->arguments;
140 }
141
142 /**
143 * @param array $defaults
144 */
145 public function setDefaults(array $defaults)
146 {
147 $this->defaults = $defaults;
148 }
149
150 /**
151 * @return array
152 */
153 public function getDefaults()
154 {
155 return $this->defaults;
156 }
157
158 /**
159 * @param string $argumentName
160 * @param mixed $argumentValue
161 */
162 public function addDefaultValue($argumentName, $argumentValue)
163 {
164 if (is_bool($argumentValue)) {
165 $argumentValue = (int)$argumentValue;
166 }
167 $this->defaults[$argumentName] = $argumentValue;
168 }
169
170 /**
171 * Return a text representation of the selected command and arguments
172 *
173 * @return string Information to display
174 */
175 public function getAdditionalInformation()
176 {
177 $label = $this->commandIdentifier;
178 if (!empty($this->arguments)) {
179 $arguments = [];
180 foreach ($this->arguments as $argumentName => $argumentValue) {
181 if (!isset($this->defaults[$argumentName]) || $argumentValue != $this->defaults[$argumentName]) {
182 $arguments[] = $argumentName . '=' . $argumentValue;
183 }
184 }
185 $label .= ' ' . implode(', ', $arguments);
186 }
187 return $label;
188 }
189
190 /**
191 * @param \Exception $e
192 */
193 protected function logException(\Exception $e)
194 {
195 $this->logger->error('A Task (' . $this->commandIdentifier . ') Exception was captured: ' . $e->getMessage() . ' (' . $e->getCode() . ')', ['exception' => $e]);
196 }
197 }