Debian Codegraph @

vendor/symfony/console/DependencyInjection/AddConsoleCommandPass.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Console\DependencyInjection;
13
14 use Symfony\Component\Console\Attribute\AsCommand;
15 use Symfony\Component\Console\Command\Command;
16 use Symfony\Component\Console\Command\LazyCommand;
17 use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
18 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
19 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20 use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
21 use Symfony\Component\DependencyInjection\ContainerBuilder;
22 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
23 use Symfony\Component\DependencyInjection\Reference;
24 use Symfony\Component\DependencyInjection\TypedReference;
25
26 /**
27 * Registers console commands.
28 *
29 * @author GrĂ©goire Pineau <[email protected]>
30 */
31 class AddConsoleCommandPass implements CompilerPassInterface
32 {
33 public function process(ContainerBuilder $container): void
34 {
35 $commandServices = $container->findTaggedServiceIds('console.command', true);
36 $lazyCommandMap = [];
37 $lazyCommandRefs = [];
38 $serviceIds = [];
39
40 foreach ($commandServices as $id => $tags) {
41 $definition = $container->getDefinition($id);
42 $class = $container->getParameterBag()->resolveValue($definition->getClass());
43
44 if (!$r = $container->getReflectionClass($class)) {
45 throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
46 }
47
48 if (!$r->isSubclassOf(Command::class)) {
49 if (!$r->hasMethod('__invoke')) {
50 throw new InvalidArgumentException(\sprintf('The service "%s" tagged "%s" must either be a subclass of "%s" or have an "__invoke()" method.', $id, 'console.command', Command::class));
51 }
52
53 $invokableRef = new Reference($id);
54 $definition = $container->register($id .= '.command', $class = Command::class)
55 ->addMethodCall('setCode', [$invokableRef]);
56 } else {
57 $invokableRef = null;
58 }
59
60 $definition->addTag('container.no_preload');
61
62 /** @var AsCommand|null $attribute */
63 $attribute = ($r->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
64
65 if (Command::class !== (new \ReflectionMethod($class, 'getDefaultName'))->class) {
66 trigger_deprecation('symfony/console', '7.3', 'Overriding "Command::getDefaultName()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', $class);
67
68 $defaultName = $class::getDefaultName();
69 } else {
70 $defaultName = $attribute?->name;
71 }
72
73 $aliases = str_replace('%', '%%', $tags[0]['command'] ?? $defaultName ?? '');
74 $aliases = explode('|', $aliases);
75 $commandName = array_shift($aliases);
76
77 if ($isHidden = '' === $commandName) {
78 $commandName = array_shift($aliases);
79 }
80
81 if (null === $commandName) {
82 if ($definition->isPrivate() || $definition->hasTag('container.private')) {
83 $commandId = 'console.command.public_alias.'.$id;
84 $container->setAlias($commandId, $id)->setPublic(true);
85 $id = $commandId;
86 }
87 $serviceIds[] = $id;
88
89 continue;
90 }
91
92 $description = $tags[0]['description'] ?? null;
93 $help = $tags[0]['help'] ?? null;
94 $usages = $tags[0]['usages'] ?? null;
95
96 unset($tags[0]);
97 $lazyCommandMap[$commandName] = $id;
98 $lazyCommandRefs[$id] = new TypedReference($id, $class);
99
100 foreach ($aliases as $alias) {
101 $lazyCommandMap[$alias] = $id;
102 }
103
104 foreach ($tags as $tag) {
105 if (isset($tag['command'])) {
106 $aliases[] = $tag['command'];
107 $lazyCommandMap[$tag['command']] = $id;
108 }
109
110 $description ??= $tag['description'] ?? null;
111 $help ??= $tag['help'] ?? null;
112 $usages ??= $tag['usages'] ?? null;
113 }
114
115 $definition->addMethodCall('setName', [$commandName]);
116
117 if ($aliases) {
118 $definition->addMethodCall('setAliases', [$aliases]);
119 }
120
121 if ($isHidden) {
122 $definition->addMethodCall('setHidden', [true]);
123 }
124
125 if ($help && $invokableRef) {
126 $definition->addMethodCall('setHelp', [str_replace('%', '%%', $help)]);
127 }
128
129 if ($usages) {
130 foreach ($usages as $usage) {
131 $definition->addMethodCall('addUsage', [$usage]);
132 }
133 }
134
135 if (!$description) {
136 if (Command::class !== (new \ReflectionMethod($class, 'getDefaultDescription'))->class) {
137 trigger_deprecation('symfony/console', '7.3', 'Overriding "Command::getDefaultDescription()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', $class);
138
139 $description = $class::getDefaultDescription();
140 } else {
141 $description = $attribute?->description;
142 }
143 }
144
145 if ($description) {
146 $escapedDescription = str_replace('%', '%%', $description);
147 $definition->addMethodCall('setDescription', [$escapedDescription]);
148
149 $container->register('.'.$id.'.lazy', LazyCommand::class)
150 ->setArguments([$commandName, $aliases, $escapedDescription, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);
151
152 $lazyCommandRefs[$id] = new Reference('.'.$id.'.lazy');
153 }
154 }
155
156 $container
157 ->register('console.command_loader', ContainerCommandLoader::class)
158 ->setPublic(true)
159 ->addTag('container.no_preload')
160 ->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
161
162 $container->setParameter('console.command.ids', $serviceIds);
163 }
164 }