‪TYPO3CMS  ‪main
ConsoleCommandPass.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\DependencyInjection\Compiler\CompilerPassInterface;
21 use Symfony\Component\DependencyInjection\ContainerBuilder;
23 
27 final class ‪ConsoleCommandPass implements CompilerPassInterface
28 {
32  private ‪$tagName;
33 
34  public function ‪__construct(string ‪$tagName)
35  {
36  $this->tagName = ‪$tagName;
37  }
38 
42  public function ‪process(ContainerBuilder $container)
43  {
44  if (!$container->hasDefinition(CommandRegistry::class)) {
45  return;
46  }
47  $commandRegistryDefinition = $container->findDefinition(CommandRegistry::class);
48 
49  foreach ($container->findTaggedServiceIds($this->tagName) as $serviceName => $tags) {
50  $commandServiceDefinition = $container->findDefinition($serviceName)->setPublic(true);
51  $commandName = null;
52  $description = null;
53  $hidden = false;
54  $aliases = [];
55  foreach ($tags as $attributes) {
56  $command = $attributes['command'] ?? null;
57  $description = $attributes['description'] ?? $description;
58  $hidden = (bool)($attributes['hidden'] ?? $hidden);
59  $schedulable = (bool)($attributes['schedulable'] ?? true);
60  $aliasFor = null;
61  if ($command === null) {
62  continue;
63  }
64 
65  $isAlias = $commandName !== null || ($attributes['alias'] ?? false);
66  if (!$isAlias) {
67  $commandName = $attributes['command'];
68  } else {
69  $aliasFor = $commandName;
70  $aliases[] = $attributes['command'];
71  }
72 
73  $commandRegistryDefinition->addMethodCall('addLazyCommand', [
74  $command,
75  $serviceName,
76  $description,
77  $hidden,
78  $schedulable,
79  $aliasFor,
80  ]);
81  }
82  $commandServiceDefinition->addMethodCall('setName', [$commandName]);
83  if ($description) {
84  $commandServiceDefinition->addMethodCall('setDescription', [$description]);
85  }
86  if ($hidden) {
87  $commandServiceDefinition->addMethodCall('setHidden', [true]);
88  }
89  if ($aliases) {
90  $commandServiceDefinition->addMethodCall('setAliases', [$aliases]);
91  }
92  }
93  }
94 }
‪TYPO3\CMS\Core\DependencyInjection\ConsoleCommandPass\$tagName
‪string $tagName
Definition: ConsoleCommandPass.php:31
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:31
‪TYPO3\CMS\Core\DependencyInjection
Definition: AutowireInjectMethodsPass.php:18
‪TYPO3\CMS\Core\DependencyInjection\ConsoleCommandPass\__construct
‪__construct(string $tagName)
Definition: ConsoleCommandPass.php:33
‪TYPO3\CMS\Core\DependencyInjection\ConsoleCommandPass\process
‪process(ContainerBuilder $container)
Definition: ConsoleCommandPass.php:41
‪TYPO3\CMS\Core\DependencyInjection\ConsoleCommandPass
Definition: ConsoleCommandPass.php:28