‪TYPO3CMS  11.5
CommandRegistry.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 Psr\Container\ContainerInterface;
21 use Symfony\Component\Console\Command\Command;
22 use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
23 use Symfony\Component\Console\Descriptor\ApplicationDescription;
24 use Symfony\Component\Console\Exception\CommandNotFoundException;
26 
30 class ‪CommandRegistry implements CommandLoaderInterface, ‪SingletonInterface
31 {
35  protected ‪$container;
36 
42  protected ‪$commandConfigurations = [];
43 
49  protected ‪$aliases = [];
50 
54  public function ‪__construct(ContainerInterface ‪$container)
55  {
56  $this->container = ‪$container;
57  }
58 
62  public function ‪has($name)
63  {
64  return array_key_exists($name, $this->commandConfigurations);
65  }
66 
70  public function get($name)
71  {
72  try {
73  return $this->‪getCommandByIdentifier($name);
74  } catch (‪UnknownCommandException $e) {
75  throw new CommandNotFoundException($e->getMessage(), [], 1567969355, $e);
76  }
77  }
78 
82  public function ‪getNames()
83  {
84  return array_keys($this->commandConfigurations);
85  }
86 
92  public function ‪getSchedulableCommands(): \Generator
93  {
94  foreach ($this->commandConfigurations as $commandName => $configuration) {
95  if ($configuration['schedulable'] ?? true) {
96  yield $commandName => $this->‪getInstance($configuration['serviceName']);
97  }
98  }
99  }
100 
106  public function ‪getCommandByIdentifier(string $identifier): Command
107  {
108  if (!isset($this->commandConfigurations[$identifier])) {
109  throw new ‪UnknownCommandException(
110  sprintf('Command "%s" has not been registered.', $identifier),
111  1510906768
112  );
113  }
114 
115  return $this->‪getInstance($this->commandConfigurations[$identifier]['serviceName']);
116  }
117 
118  protected function ‪getInstance(string $service): Command
119  {
120  return $this->container->get($service);
121  }
122 
126  public function ‪getNamespaces(): array
127  {
128  $namespaces = [];
129  foreach ($this->commandConfigurations as $commandName => $configuration) {
130  if ($configuration['hidden']) {
131  continue;
132  }
133  if ($configuration['aliasFor'] !== null) {
134  continue;
135  }
136  $namespace = $configuration['namespace'];
137  $namespaces[$namespace]['id'] = $namespace;
138  $namespaces[$namespace]['commands'][] = $commandName;
139  }
140 
141  ksort($namespaces);
142  foreach ($namespaces as &$commands) {
143  ksort($commands);
144  }
145 
146  return $namespaces;
147  }
148 
157  public function ‪filter(string $namespace = null): array
158  {
159  $commands = [];
160  foreach ($this->commandConfigurations as $commandName => $configuration) {
161  if ($configuration['hidden']) {
162  continue;
163  }
164  if ($namespace !== null && $namespace !== $this->‪extractNamespace($commandName, substr_count($namespace, ':') + 1)) {
165  continue;
166  }
167  if ($configuration['aliasFor'] !== null) {
168  continue;
169  }
170 
171  $commands[$commandName] = $configuration;
172  $commands[$commandName]['aliases'] = $this->aliases[$commandName] ?? [];
173  }
174 
175  return $commands;
176  }
177 
181  public function ‪addLazyCommand(
182  string $commandName,
183  string $serviceName,
184  string $description = null,
185  bool $hidden = false,
186  bool $schedulable = false,
187  string $aliasFor = null
188  ): void {
189  $this->commandConfigurations[$commandName] = [
190  'name' => $aliasFor ?? $commandName,
191  'serviceName' => $serviceName,
192  'description' => $description,
193  'hidden' => $hidden,
194  'schedulable' => $schedulable,
195  'aliasFor' => $aliasFor,
196  'namespace' => $this->‪extractNamespace($commandName, 1),
197  ];
198 
199  if ($aliasFor !== null) {
200  $this->aliases[$aliasFor][] = $commandName;
201  }
202  }
203 
211  private function ‪extractNamespace(string $name, int $limit = null): string
212  {
213  $parts = explode(':', $name, -1);
214  if (count($parts) === 0) {
215  return ApplicationDescription::GLOBAL_NAMESPACE;
216  }
217 
218  return implode(':', $limit === null ? $parts : array_slice($parts, 0, $limit));
219  }
220 }
‪TYPO3\CMS\Core\Console\CommandRegistry\__construct
‪__construct(ContainerInterface $container)
Definition: CommandRegistry.php:51
‪TYPO3\CMS\Core\Console\CommandRegistry\getInstance
‪getInstance(string $service)
Definition: CommandRegistry.php:115
‪TYPO3\CMS\Core\Console\UnknownCommandException
Definition: UnknownCommandException.php:25
‪TYPO3\CMS\Core\Console\CommandRegistry\$commandConfigurations
‪array[] $commandConfigurations
Definition: CommandRegistry.php:40
‪TYPO3\CMS\Core\Console\CommandRegistry\$container
‪ContainerInterface $container
Definition: CommandRegistry.php:34
‪TYPO3\CMS\Core\Console
Definition: Application.php:18
‪TYPO3\CMS\Core\Console\CommandRegistry\extractNamespace
‪string extractNamespace(string $name, int $limit=null)
Definition: CommandRegistry.php:208
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:31
‪TYPO3\CMS\Core\Console\CommandRegistry\addLazyCommand
‪addLazyCommand(string $commandName, string $serviceName, string $description=null, bool $hidden=false, bool $schedulable=false, string $aliasFor=null)
Definition: CommandRegistry.php:178
‪TYPO3\CMS\Core\Console\CommandRegistry\getCommandByIdentifier
‪Command getCommandByIdentifier(string $identifier)
Definition: CommandRegistry.php:103
‪TYPO3\CMS\Core\Console\CommandRegistry\getSchedulableCommands
‪Generator getSchedulableCommands()
Definition: CommandRegistry.php:89
‪TYPO3\CMS\Core\Console\CommandRegistry\has
‪has($name)
Definition: CommandRegistry.php:59
‪TYPO3\CMS\Core\Console\CommandRegistry\getNamespaces
‪getNamespaces()
Definition: CommandRegistry.php:123
‪TYPO3\CMS\Core\Console\CommandRegistry\getNames
‪getNames()
Definition: CommandRegistry.php:79
‪TYPO3\CMS\Core\Console\CommandRegistry\$aliases
‪array[] $aliases
Definition: CommandRegistry.php:46
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Console\CommandRegistry\filter
‪array filter(string $namespace=null)
Definition: CommandRegistry.php:154