‪TYPO3CMS  10.4
ExecuteSchedulableCommandTask.php
Go to the documentation of this file.
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 
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;
27 
32 {
36  protected ‪$commandIdentifier = '';
37 
41  protected ‪$arguments = [];
42 
46  protected ‪$options = [];
47 
51  protected ‪$optionValues = [];
52 
56  protected ‪$defaults = [];
57 
61  public function ‪setCommandIdentifier(string ‪$commandIdentifier)
62  {
63  $this->commandIdentifier = ‪$commandIdentifier;
64  }
65 
69  public function ‪getCommandIdentifier(): string
70  {
72  }
73 
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 
112  public function ‪getAdditionalInformation(): string
113  {
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 
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 }
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getCommandIdentifier
‪string getCommandIdentifier()
Definition: ExecuteSchedulableCommandTask.php:64
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\execute
‪bool execute()
Definition: ExecuteSchedulableCommandTask.php:80
‪TYPO3\CMS\Core\Console\UnknownCommandException
Definition: UnknownCommandException.php:26
‪TYPO3\CMS\Scheduler\Task
Definition: AbstractTask.php:16
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\setArguments
‪setArguments(array $arguments)
Definition: ExecuteSchedulableCommandTask.php:149
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getOptionValues
‪getOptionValues()
Definition: ExecuteSchedulableCommandTask.php:164
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask
Definition: ExecuteSchedulableCommandTask.php:32
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\$optionValues
‪array $optionValues
Definition: ExecuteSchedulableCommandTask.php:47
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\addDefaultValue
‪addDefaultValue(string $argumentName, $argumentValue)
Definition: ExecuteSchedulableCommandTask.php:178
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\$options
‪array $options
Definition: ExecuteSchedulableCommandTask.php:43
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:32
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getLanguageService
‪LanguageService null getLanguageService()
Definition: AbstractTask.php:605
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:35
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\$arguments
‪array $arguments
Definition: ExecuteSchedulableCommandTask.php:39
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\$defaults
‪array $defaults
Definition: ExecuteSchedulableCommandTask.php:51
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\setCommandIdentifier
‪setCommandIdentifier(string $commandIdentifier)
Definition: ExecuteSchedulableCommandTask.php:56
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getAdditionalInformation
‪string getAdditionalInformation()
Definition: ExecuteSchedulableCommandTask.php:107
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\setOptions
‪setOptions(array $options)
Definition: ExecuteSchedulableCommandTask.php:159
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\$commandIdentifier
‪string $commandIdentifier
Definition: ExecuteSchedulableCommandTask.php:35
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getOptions
‪getOptions()
Definition: ExecuteSchedulableCommandTask.php:154
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getParameters
‪getParameters(bool $forDisplay)
Definition: ExecuteSchedulableCommandTask.php:186
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\setOptionValues
‪setOptionValues(array $optionValues)
Definition: ExecuteSchedulableCommandTask.php:169
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getArguments
‪getArguments()
Definition: ExecuteSchedulableCommandTask.php:144