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