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