‪TYPO3CMS  ‪main
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 
51  public function ‪__construct(ContainerInterface ‪$container)
52  {
53  $this->container = ‪$container;
54  }
55 
59  public function ‪has(string $name): bool
60  {
61  return array_key_exists($name, $this->commandConfigurations);
62  }
63 
67  public function get(string $name): Command
68  {
69  try {
70  return $this->‪getCommandByIdentifier($name);
71  } catch (‪UnknownCommandException $e) {
72  throw new CommandNotFoundException($e->getMessage(), [], 1567969355, $e);
73  }
74  }
75 
79  public function ‪getNames(): array
80  {
81  return array_keys($this->commandConfigurations);
82  }
83 
87  public function ‪getSchedulableCommands(): \Generator
88  {
89  foreach ($this->commandConfigurations as $commandName => $configuration) {
90  if ($configuration['schedulable'] ?? true) {
91  yield $commandName => $this->‪getInstance($configuration['serviceName']);
92  }
93  }
94  }
95 
99  public function ‪getCommandByIdentifier(string ‪$identifier): Command
100  {
101  if (!isset($this->commandConfigurations[‪$identifier])) {
102  throw new ‪UnknownCommandException(
103  sprintf('Command "%s" has not been registered.', ‪$identifier),
104  1510906768
105  );
106  }
107 
108  return $this->‪getInstance($this->commandConfigurations[‪$identifier]['serviceName']);
109  }
110 
111  protected function ‪getInstance(string $service): Command
112  {
113  return $this->container->get($service);
114  }
115 
119  public function ‪getNamespaces(): array
120  {
121  $namespaces = [];
122  foreach ($this->commandConfigurations as $commandName => $configuration) {
123  if ($configuration['hidden']) {
124  continue;
125  }
126  if ($configuration['aliasFor'] !== null) {
127  continue;
128  }
129  $namespace = $configuration['namespace'];
130  $namespaces[$namespace]['id'] = $namespace;
131  $namespaces[$namespace]['commands'][] = $commandName;
132  }
133 
134  ksort($namespaces);
135  foreach ($namespaces as &$commands) {
136  ksort($commands);
137  }
138 
139  return $namespaces;
140  }
141 
150  public function ‪filter(string $namespace = null): array
151  {
152  $commands = [];
153  foreach ($this->commandConfigurations as $commandName => $configuration) {
154  if ($configuration['hidden']) {
155  continue;
156  }
157  if ($namespace !== null && $namespace !== $this->‪extractNamespace($commandName, substr_count($namespace, ':') + 1)) {
158  continue;
159  }
160  if ($configuration['aliasFor'] !== null) {
161  continue;
162  }
163 
164  $commands[$commandName] = $configuration;
165  $commands[$commandName]['aliases'] = $this->aliases[$commandName] ?? [];
166  }
167 
168  return $commands;
169  }
170 
174  public function ‪addLazyCommand(
175  string $commandName,
176  string $serviceName,
177  string $description = null,
178  bool $hidden = false,
179  bool $schedulable = false,
180  string $aliasFor = null
181  ): void {
182  $this->commandConfigurations[$commandName] = [
183  'name' => $aliasFor ?? $commandName,
184  'serviceName' => $serviceName,
185  'description' => $description,
186  'hidden' => $hidden,
187  'schedulable' => $schedulable,
188  'aliasFor' => $aliasFor,
189  'namespace' => $this->‪extractNamespace($commandName, 1),
190  ];
191 
192  if ($aliasFor !== null) {
193  $this->aliases[$aliasFor][] = $commandName;
194  }
195  }
196 
204  private function ‪extractNamespace(string $name, int $limit = null): string
205  {
206  $parts = explode(':', $name, -1);
207  if (count($parts) === 0) {
208  return ApplicationDescription::GLOBAL_NAMESPACE;
209  }
210 
211  return implode(':', $limit === null ? $parts : array_slice($parts, 0, $limit));
212  }
213 }
‪TYPO3\CMS\Core\Console\CommandRegistry\__construct
‪__construct(ContainerInterface $container)
Definition: CommandRegistry.php:48
‪TYPO3\CMS\Core\Console\CommandRegistry\getInstance
‪getInstance(string $service)
Definition: CommandRegistry.php:108
‪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\getSchedulableCommands
‪getSchedulableCommands()
Definition: CommandRegistry.php:84
‪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:201
‪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:171
‪TYPO3\CMS\Core\Console\CommandRegistry\has
‪has(string $name)
Definition: CommandRegistry.php:56
‪TYPO3\CMS\Core\Console\CommandRegistry\getNamespaces
‪getNamespaces()
Definition: CommandRegistry.php:116
‪TYPO3\CMS\Core\Console\CommandRegistry\getNames
‪getNames()
Definition: CommandRegistry.php:76
‪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\getCommandByIdentifier
‪getCommandByIdentifier(string $identifier)
Definition: CommandRegistry.php:96
‪TYPO3\CMS\Core\Console\CommandRegistry\filter
‪array filter(string $namespace=null)
Definition: CommandRegistry.php:147
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37