[BUGFIX] Tests: Incomplete mock of $GLOBALS['TYPO3_DB']
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Scheduler / Task.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Scheduler;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Scheduler task to execute CommandController commands
32 */
33 class Task extends \TYPO3\CMS\Scheduler\Task\AbstractTask {
34
35 /**
36 * @var string
37 */
38 protected $commandIdentifier;
39
40 /**
41 * @var array
42 */
43 protected $arguments = array();
44
45 /**
46 * @var array
47 */
48 protected $defaults = array();
49
50 /**
51 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
52 */
53 protected $objectManager;
54
55 /**
56 * @var \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
57 */
58 protected $commandManager;
59
60 /**
61 * @var \TYPO3\CMS\Extbase\Scheduler\TaskExecutor
62 */
63 protected $taskExecutor;
64
65 /**
66 * Instantiates the Object Manager
67 */
68 public function __construct() {
69 parent::__construct();
70 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
71 $this->commandManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandManager');
72 $this->taskExecutor = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Scheduler\\TaskExecutor');
73 }
74
75 /**
76 * Sleep
77 *
78 * @return array Properties to serialize
79 */
80 public function __sleep() {
81 $properties = get_object_vars($this);
82 unset($properties['commandManager']);
83 unset($properties['objectManager']);
84 unset($properties['taskExecutor']);
85 return array_keys($properties);
86 }
87
88 /**
89 * Wakeup
90 *
91 * @return void
92 */
93 public function __wakeup() {
94 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
95 $this->commandManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandManager');
96 $this->taskExecutor = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Scheduler\\TaskExecutor');
97 }
98
99 /**
100 * Function execute from the Scheduler
101 *
102 * @return boolean TRUE on successful execution, FALSE on error
103 */
104 public function execute() {
105 try {
106 $this->taskExecutor->execute($this);
107 return TRUE;
108 } catch (\Exception $e) {
109 $this->logException($e);
110 return FALSE;
111 }
112 }
113
114 /**
115 * @param string $commandIdentifier
116 */
117 public function setCommandIdentifier($commandIdentifier) {
118 $this->commandIdentifier = $commandIdentifier;
119 }
120
121 /**
122 * @return string
123 */
124 public function getCommandIdentifier() {
125 return $this->commandIdentifier;
126 }
127
128 /**
129 * @param array $arguments
130 */
131 public function setArguments($arguments) {
132 $this->arguments = $arguments;
133 }
134
135 /**
136 * @return array
137 */
138 public function getArguments() {
139 return $this->arguments;
140 }
141
142 /**
143 * @param array $defaults
144 */
145 public function setDefaults(array $defaults) {
146 $this->defaults = $defaults;
147 }
148
149 /**
150 * @return array
151 */
152 public function getDefaults() {
153 return $this->defaults;
154 }
155
156 /**
157 * @param string $argumentName
158 * @param mixed $argumentValue
159 */
160 public function addDefaultValue($argumentName, $argumentValue) {
161 if (is_bool($argumentValue)) {
162 $argumentValue = (int)$argumentValue;
163 }
164 $this->defaults[$argumentName] = $argumentValue;
165 }
166
167 /**
168 * Return a text representation of the selected command and arguments
169 *
170 * @return string Information to display
171 */
172 public function getAdditionalInformation() {
173 $label = $this->commandIdentifier;
174 if (count($this->arguments) > 0) {
175 $arguments = array();
176 foreach ($this->arguments as $argumentName => $argumentValue) {
177 if ($argumentValue != $this->defaults[$argumentName]) {
178 array_push($arguments, $argumentName . '=' . $argumentValue);
179 }
180 }
181 $label .= ' ' . implode(', ', $arguments);
182 }
183 return $label;
184 }
185
186 /**
187 * @param \Exception $e
188 */
189 protected function logException(\Exception $e) {
190 \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog($e->getMessage(), $this->commandIdentifier, 3);
191 }
192 }