‪TYPO3CMS  9.5
CommandRegistry.php
Go to the documentation of this file.
1 <?php
2 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 use Symfony\Component\Console\Command\Command;
19 use TYPO3\CMS\Core\Package\PackageManager;
22 
26 class ‪CommandRegistry implements \IteratorAggregate, ‪SingletonInterface
27 {
31  protected ‪$packageManager;
32 
38  protected ‪$commands = [];
39 
45  protected ‪$commandConfigurations = [];
46 
50  public function ‪__construct(PackageManager ‪$packageManager = null)
51  {
52  $this->packageManager = ‪$packageManager ?: GeneralUtility::makeInstance(PackageManager::class);
53  }
54 
58  public function ‪getIterator(): \Generator
59  {
61  foreach ($this->commands as $commandName => $command) {
62  yield $commandName => $command;
63  }
64  }
65 
71  public function ‪getSchedulableCommands(): \Generator
72  {
74  foreach ($this->commands as $commandName => $command) {
75  if ($this->commandConfigurations[$commandName]['schedulable'] ?? true) {
76  yield $commandName => $command;
77  }
78  }
79  }
80 
87  public function ‪getCommandByIdentifier(string $identifier): ?Command
88  {
90 
91  if (!isset($this->commands[$identifier])) {
93  sprintf('Command "%s" has not been registered.', $identifier),
94  1510906768
95  );
96  }
97 
98  return $this->commands[$identifier] ?? null;
99  }
100 
116  protected function ‪populateCommandsFromPackages()
117  {
118  if ($this->commands) {
119  return;
120  }
121  foreach ($this->packageManager->getActivePackages() as $package) {
122  $commandsOfExtension = $package->getPackagePath() . 'Configuration/Commands.php';
123  if (@is_file($commandsOfExtension)) {
124  /*
125  * We use require instead of require_once here because it eases the testability as require_once returns
126  * a boolean from the second execution on. As this class is a singleton, this require is only called
127  * once per request anyway.
128  */
129  ‪$commands = require $commandsOfExtension;
130  if (is_array(‪$commands)) {
131  foreach (‪$commands as $commandName => $commandConfig) {
132  if (array_key_exists($commandName, $this->commands)) {
134  'Command "' . $commandName . '" registered by "' . $package->getPackageKey() . '" is already in use',
135  1484486383
136  );
137  }
138  $this->commands[$commandName] = GeneralUtility::makeInstance($commandConfig['class'], $commandName);
139  $this->commandConfigurations[$commandName] = $commandConfig;
140  }
141  }
142  }
143  }
144  }
145 }
‪TYPO3\CMS\Core\Console\CommandRegistry\getIterator
‪Generator getIterator()
Definition: CommandRegistry.php:55
‪TYPO3\CMS\Core\Console\CommandRegistry\$commands
‪Command[] $commands
Definition: CommandRegistry.php:36
‪TYPO3\CMS\Core\Console\UnknownCommandException
Definition: UnknownCommandException.php:24
‪TYPO3\CMS\Core\Console\CommandRegistry\getCommandByIdentifier
‪Command null getCommandByIdentifier(string $identifier)
Definition: CommandRegistry.php:84
‪TYPO3\CMS\Core\Console\CommandRegistry\$commandConfigurations
‪array[] $commandConfigurations
Definition: CommandRegistry.php:42
‪TYPO3\CMS\Core\Console
Definition: CommandApplication.php:2
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:27
‪TYPO3\CMS\Core\Console\CommandNameAlreadyInUseException
Definition: CommandNameAlreadyInUseException.php:24
‪TYPO3\CMS\Core\Console\CommandRegistry\__construct
‪__construct(PackageManager $packageManager=null)
Definition: CommandRegistry.php:47
‪TYPO3\CMS\Core\Console\CommandRegistry\getSchedulableCommands
‪Generator getSchedulableCommands()
Definition: CommandRegistry.php:68
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Console\CommandRegistry\populateCommandsFromPackages
‪populateCommandsFromPackages()
Definition: CommandRegistry.php:113
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Console\CommandRegistry\$packageManager
‪PackageManager $packageManager
Definition: CommandRegistry.php:30