[TASK] Use younger selenium/standalone-chrome
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / ExecuteSchedulableCommandTask.php
1 <?php
2
3 declare(strict_types=1);
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 namespace TYPO3\CMS\Scheduler\Task;
19
20 use Symfony\Component\Console\Exception\InvalidArgumentException;
21 use Symfony\Component\Console\Exception\InvalidOptionException;
22 use Symfony\Component\Console\Input\ArrayInput;
23 use Symfony\Component\Console\Output\NullOutput;
24 use TYPO3\CMS\Core\Console\CommandRegistry;
25 use TYPO3\CMS\Core\Console\UnknownCommandException;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28 /**
29 * @internal This class is a specific scheduler task implementation is not considered part of the Public TYPO3 API.
30 */
31 class ExecuteSchedulableCommandTask extends AbstractTask
32 {
33 /**
34 * @var string
35 */
36 protected $commandIdentifier = '';
37
38 /**
39 * @var array
40 */
41 protected $arguments = [];
42
43 /**
44 * @var array
45 */
46 protected $options = [];
47
48 /**
49 * @var array
50 */
51 protected $optionValues = [];
52
53 /**
54 * @var array
55 */
56 protected $defaults = [];
57
58 /**
59 * @param string $commandIdentifier
60 */
61 public function setCommandIdentifier(string $commandIdentifier)
62 {
63 $this->commandIdentifier = $commandIdentifier;
64 }
65
66 /**
67 * @return string
68 */
69 public function getCommandIdentifier(): string
70 {
71 return $this->commandIdentifier;
72 }
73
74 /**
75 * This is the main method that is called when a task is executed
76 * It MUST be implemented by all classes inheriting from this one
77 * Note that there is no error handling, errors and failures are expected
78 * to be handled and logged by the client implementations.
79 * Should return TRUE on successful execution, FALSE on error.
80 *
81 * @throws \Exception
82 *
83 * @return bool Returns TRUE on successful execution, FALSE on error
84 */
85 public function execute(): bool
86 {
87 try {
88 $commandRegistry = GeneralUtility::makeInstance(CommandRegistry::class);
89 $schedulableCommand = $commandRegistry->getCommandByIdentifier($this->commandIdentifier);
90 } catch (UnknownCommandException $e) {
91 throw new \RuntimeException(
92 sprintf(
93 $this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.unregisteredCommand'),
94 $this->commandIdentifier
95 ),
96 1505055445,
97 $e
98 );
99 }
100
101 $input = new ArrayInput($this->getParameters(false));
102 $output = new NullOutput();
103
104 return $schedulableCommand->run($input, $output) === 0;
105 }
106
107 /**
108 * Return a text representation of the selected command and arguments
109 *
110 * @return string Information to display
111 */
112 public function getAdditionalInformation(): string
113 {
114 $label = $this->commandIdentifier;
115
116 try {
117 $commandRegistry = GeneralUtility::makeInstance(CommandRegistry::class);
118 $schedulableCommand = $commandRegistry->getCommandByIdentifier($this->commandIdentifier);
119 } catch (UnknownCommandException $e) {
120 return sprintf(
121 $this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.unregisteredCommand'),
122 $this->commandIdentifier
123 );
124 }
125
126 try {
127 $input = new ArrayInput($this->getParameters(true), $schedulableCommand->getDefinition());
128 $arguments = $input->__toString();
129 } catch (\Symfony\Component\Console\Exception\RuntimeException|InvalidArgumentException $e) {
130 return $label . "\n"
131 . sprintf(
132 $this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.errorParsingArguments'),
133 $e->getMessage()
134 );
135 } catch (InvalidOptionException $e) {
136 return $label . "\n"
137 . sprintf(
138 $this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.errorParsingOptions'),
139 $e->getMessage()
140 );
141 }
142 if ($arguments !== '') {
143 $label .= ' ' . $arguments;
144 }
145
146 return $label;
147 }
148
149 public function getArguments(): array
150 {
151 return $this->arguments;
152 }
153
154 public function setArguments(array $arguments)
155 {
156 $this->arguments = $arguments;
157 }
158
159 public function getOptions(): array
160 {
161 return $this->options;
162 }
163
164 public function setOptions(array $options)
165 {
166 $this->options = $options;
167 }
168
169 public function getOptionValues(): array
170 {
171 return $this->optionValues;
172 }
173
174 public function setOptionValues(array $optionValues)
175 {
176 $this->optionValues = $optionValues;
177 }
178
179 /**
180 * @param string $argumentName
181 * @param mixed $argumentValue
182 */
183 public function addDefaultValue(string $argumentName, $argumentValue)
184 {
185 if (is_bool($argumentValue)) {
186 $argumentValue = (int)$argumentValue;
187 }
188 $this->defaults[$argumentName] = $argumentValue;
189 }
190
191 private function getParameters(bool $forDisplay): array
192 {
193 $options = [];
194 foreach ($this->options as $name => $enabled) {
195 if ($enabled) {
196 $value = $this->optionValues[$name] ?? null;
197 $options['--' . $name] = ($forDisplay && $value === true) ? '' : $value;
198 }
199 }
200 return array_merge($this->arguments, $options);
201 }
202 }