‪TYPO3CMS  ‪main
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 
59  {
60  $this->commandIdentifier = ‪$commandIdentifier;
61  }
62 
63  public function ‪getCommandIdentifier(): string
64  {
66  }
67 
79  public function ‪execute(): bool
80  {
81  try {
82  $commandRegistry = GeneralUtility::makeInstance(CommandRegistry::class);
83  $schedulableCommand = $commandRegistry->getCommandByIdentifier($this->commandIdentifier);
84  } catch (UnknownCommandException $e) {
85  throw new \RuntimeException(
86  sprintf(
87  $this->‪getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.unregisteredCommand'),
88  $this->commandIdentifier
89  ),
90  1505055445,
91  $e
92  );
93  }
94 
95  $input = new ArrayInput($this->‪getParameters(false));
96  $input->setInteractive(false);
97 
98  ‪$output = new NullOutput();
99 
100  return $schedulableCommand->run($input, ‪$output) === 0;
101  }
102 
108  public function ‪getAdditionalInformation(): string
109  {
111 
112  try {
113  $commandRegistry = GeneralUtility::makeInstance(CommandRegistry::class);
114  $schedulableCommand = $commandRegistry->getCommandByIdentifier($this->commandIdentifier);
115  } catch (UnknownCommandException $e) {
116  return sprintf(
117  $this->‪getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.unregisteredCommand'),
118  $this->commandIdentifier
119  );
120  }
121 
122  try {
123  $input = new ArrayInput($this->‪getParameters(true), $schedulableCommand->getDefinition());
124  ‪$arguments = $input->__toString();
125  } catch (\Symfony\Component\Console\Exception\RuntimeException|InvalidArgumentException $e) {
126  return $label . "\n"
127  . sprintf(
128  $this->‪getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.errorParsingArguments'),
129  $e->getMessage()
130  );
131  } catch (InvalidOptionException $e) {
132  return $label . "\n"
133  . sprintf(
134  $this->‪getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.errorParsingOptions'),
135  $e->getMessage()
136  );
137  }
138  if (‪$arguments !== '') {
139  $label .= ' ' . ‪$arguments;
140  }
141 
142  return $label;
143  }
144 
145  public function ‪getArguments(): array
146  {
147  return ‪$this->arguments;
148  }
149 
150  public function ‪setArguments(array ‪$arguments)
151  {
152  $this->arguments = ‪$arguments;
153  }
154 
155  public function ‪getOptions(): array
156  {
157  return ‪$this->options;
158  }
159 
160  public function ‪setOptions(array ‪$options)
161  {
162  $this->options = ‪$options;
163  }
164 
165  public function ‪getOptionValues(): array
166  {
167  return ‪$this->optionValues;
168  }
169 
170  public function ‪setOptionValues(array ‪$optionValues)
171  {
172  $this->optionValues = ‪$optionValues;
173  }
174 
178  public function ‪addDefaultValue(string $argumentName, $argumentValue)
179  {
180  if (is_bool($argumentValue)) {
181  $argumentValue = (int)$argumentValue;
182  }
183  $this->defaults[$argumentName] = $argumentValue;
184  }
185 
186  private function ‪getParameters(bool $forDisplay): array
187  {
188  ‪$options = [];
189  foreach ($this->options as $name => $enabled) {
190  if ($enabled) {
191  $value = $this->optionValues[$name] ?? null;
192  ‪$options['--' . $name] = ($forDisplay && $value === true) ? '' : $value;
193  }
194  }
195  return array_merge($this->arguments, ‪$options);
196  }
197 }
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\execute
‪bool execute()
Definition: ExecuteSchedulableCommandTask.php:74
‪TYPO3\CMS\Core\Console\UnknownCommandException
Definition: UnknownCommandException.php:25
‪TYPO3\CMS\Scheduler\Task
Definition: AbstractTask.php:16
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\setArguments
‪setArguments(array $arguments)
Definition: ExecuteSchedulableCommandTask.php:145
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getOptionValues
‪getOptionValues()
Definition: ExecuteSchedulableCommandTask.php:160
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask
Definition: ExecuteSchedulableCommandTask.php:32
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getLanguageService
‪getLanguageService()
Definition: AbstractTask.php:431
‪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:173
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\$options
‪array $options
Definition: ExecuteSchedulableCommandTask.php:43
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:31
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getCommandIdentifier
‪getCommandIdentifier()
Definition: ExecuteSchedulableCommandTask.php:58
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:33
‪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:53
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getAdditionalInformation
‪string getAdditionalInformation()
Definition: ExecuteSchedulableCommandTask.php:103
‪TYPO3\CMS\Scheduler\Exception
Definition: Exception.php:25
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\setOptions
‪setOptions(array $options)
Definition: ExecuteSchedulableCommandTask.php:155
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\$commandIdentifier
‪string $commandIdentifier
Definition: ExecuteSchedulableCommandTask.php:35
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getOptions
‪getOptions()
Definition: ExecuteSchedulableCommandTask.php:150
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getParameters
‪getParameters(bool $forDisplay)
Definition: ExecuteSchedulableCommandTask.php:181
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\setOptionValues
‪setOptionValues(array $optionValues)
Definition: ExecuteSchedulableCommandTask.php:165
‪TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask\getArguments
‪getArguments()
Definition: ExecuteSchedulableCommandTask.php:140