‪TYPO3CMS  10.4
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  $commandRegistryDefinition = $container->findDefinition(CommandRegistry::class);
48  if (!$commandRegistryDefinition) {
49  return;
50  }
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  if (!isset($attributes['command'])) {
60  continue;
61  }
62  $description = $attributes['description'] ?? $description;
63  $hidden = (bool)($attributes['hidden'] ?? $hidden);
64  $commandRegistryDefinition->addMethodCall('addLazyCommand', [
65  $attributes['command'],
66  $serviceName,
67  (bool)($attributes['schedulable'] ?? true)
68  ]);
69  $isAlias = isset($commandName) || ($attributes['alias'] ?? false);
70  if (!$isAlias) {
71  $commandName = $attributes['command'];
72  } else {
73  $aliases[] = $attributes['command'];
74  }
75  }
76  $commandServiceDefinition->addMethodCall('setName', [$commandName]);
77  if ($description) {
78  $commandServiceDefinition->addMethodCall('setDescription', [$description]);
79  }
80  if ($hidden) {
81  $commandServiceDefinition->addMethodCall('setHidden', [true]);
82  }
83  if ($aliases) {
84  $commandServiceDefinition->addMethodCall('setAliases', [$aliases]);
85  }
86  }
87  }
88 }
‪TYPO3\CMS\Core\DependencyInjection\ConsoleCommandPass\$tagName
‪string $tagName
Definition: ConsoleCommandPass.php:31
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:32
‪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