[FEATURE] Introduce scheduler task to execute console commands
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / ExecuteSchedulableCommandTask.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Scheduler\Task;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Symfony\Component\Console\Input\ArrayInput;
19 use Symfony\Component\Console\Output\NullOutput;
20 use TYPO3\CMS\Core\Console\CommandRegistry;
21 use TYPO3\CMS\Core\Console\UnknownCommandException;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Lang\LanguageService;
24
25 /**
26 * Class TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask
27 */
28 class ExecuteSchedulableCommandTask extends AbstractTask
29 {
30 /**
31 * @var string
32 */
33 protected $commandIdentifier = '';
34
35 /**
36 * @var array
37 */
38 protected $arguments = [];
39
40 /**
41 * @var array
42 */
43 protected $defaults = [];
44
45 /**
46 * @param string $commandIdentifier
47 */
48 public function setCommandIdentifier(string $commandIdentifier)
49 {
50 $this->commandIdentifier = $commandIdentifier;
51 }
52
53 /**
54 * @return string
55 */
56 public function getCommandIdentifier(): string
57 {
58 return $this->commandIdentifier;
59 }
60
61 /**
62 * This is the main method that is called when a task is executed
63 * It MUST be implemented by all classes inheriting from this one
64 * Note that there is no error handling, errors and failures are expected
65 * to be handled and logged by the client implementations.
66 * Should return TRUE on successful execution, FALSE on error.
67 *
68 * @throws \Exception
69 *
70 * @return bool Returns TRUE on successful execution, FALSE on error
71 */
72 public function execute(): bool
73 {
74 try {
75 $commandRegistry = GeneralUtility::makeInstance(CommandRegistry::class);
76 $schedulableCommand = $commandRegistry->getCommandByIdentifier($this->commandIdentifier);
77 } catch (UnknownCommandException $e) {
78 throw new \RuntimeException(
79 sprintf(
80 $this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.unregisteredCommand'),
81 $this->commandIdentifier
82 ),
83 1505055445,
84 $e
85 );
86 }
87
88 $input = new ArrayInput($this->getArguments(), $schedulableCommand->getDefinition());
89 $output = new NullOutput();
90
91 return $schedulableCommand->run($input, $output) === 0;
92 }
93
94 /**
95 * @return array
96 */
97 public function getArguments(): array
98 {
99 return $this->arguments;
100 }
101
102 /**
103 * @param array $arguments
104 */
105 public function setArguments(array $arguments)
106 {
107 $this->arguments = $arguments;
108 }
109
110 /**
111 * @param string $argumentName
112 * @param mixed $argumentValue
113 */
114 public function addDefaultValue(string $argumentName, $argumentValue)
115 {
116 if (is_bool($argumentValue)) {
117 $argumentValue = (int)$argumentValue;
118 }
119 $this->defaults[$argumentName] = $argumentValue;
120 }
121
122 /**
123 * @return LanguageService
124 */
125 public function getLanguageService(): LanguageService
126 {
127 return $GLOBALS['LANG'];
128 }
129 }