| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
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\Command\Command;
|
|
|
15 |
use Symfony\Component\Console\Command\LazyCommand;
|
|
|
16 |
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
|
|
|
17 |
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
|
|
|
18 |
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
|
|
19 |
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
|
|
|
20 |
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
21 |
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
|
|
22 |
use Symfony\Component\DependencyInjection\Reference;
|
|
|
23 |
use Symfony\Component\DependencyInjection\TypedReference;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Registers console commands.
|
|
|
27 |
*
|
|
|
28 |
* @author Grégoire Pineau <lyrixx@lyrixx.info>
|
|
|
29 |
*/
|
|
|
30 |
class AddConsoleCommandPass implements CompilerPassInterface
|
|
|
31 |
{
|
|
|
32 |
public function process(ContainerBuilder $container)
|
|
|
33 |
{
|
|
|
34 |
$commandServices = $container->findTaggedServiceIds('console.command', true);
|
|
|
35 |
$lazyCommandMap = [];
|
|
|
36 |
$lazyCommandRefs = [];
|
|
|
37 |
$serviceIds = [];
|
|
|
38 |
|
|
|
39 |
foreach ($commandServices as $id => $tags) {
|
|
|
40 |
$definition = $container->getDefinition($id);
|
|
|
41 |
$definition->addTag('container.no_preload');
|
|
|
42 |
$class = $container->getParameterBag()->resolveValue($definition->getClass());
|
|
|
43 |
|
|
|
44 |
if (isset($tags[0]['command'])) {
|
|
|
45 |
$aliases = $tags[0]['command'];
|
|
|
46 |
} else {
|
|
|
47 |
if (!$r = $container->getReflectionClass($class)) {
|
|
|
48 |
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
|
|
49 |
}
|
|
|
50 |
if (!$r->isSubclassOf(Command::class)) {
|
|
|
51 |
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, 'console.command', Command::class));
|
|
|
52 |
}
|
|
|
53 |
$aliases = str_replace('%', '%%', $class::getDefaultName() ?? '');
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
$aliases = explode('|', $aliases ?? '');
|
|
|
57 |
$commandName = array_shift($aliases);
|
|
|
58 |
|
|
|
59 |
if ($isHidden = '' === $commandName) {
|
|
|
60 |
$commandName = array_shift($aliases);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if (null === $commandName) {
|
|
|
64 |
if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag('container.private')) {
|
|
|
65 |
$commandId = 'console.command.public_alias.'.$id;
|
|
|
66 |
$container->setAlias($commandId, $id)->setPublic(true);
|
|
|
67 |
$id = $commandId;
|
|
|
68 |
}
|
|
|
69 |
$serviceIds[] = $id;
|
|
|
70 |
|
|
|
71 |
continue;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$description = $tags[0]['description'] ?? null;
|
|
|
75 |
|
|
|
76 |
unset($tags[0]);
|
|
|
77 |
$lazyCommandMap[$commandName] = $id;
|
|
|
78 |
$lazyCommandRefs[$id] = new TypedReference($id, $class);
|
|
|
79 |
|
|
|
80 |
foreach ($aliases as $alias) {
|
|
|
81 |
$lazyCommandMap[$alias] = $id;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
foreach ($tags as $tag) {
|
|
|
85 |
if (isset($tag['command'])) {
|
|
|
86 |
$aliases[] = $tag['command'];
|
|
|
87 |
$lazyCommandMap[$tag['command']] = $id;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
$description ??= $tag['description'] ?? null;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
$definition->addMethodCall('setName', [$commandName]);
|
|
|
94 |
|
|
|
95 |
if ($aliases) {
|
|
|
96 |
$definition->addMethodCall('setAliases', [$aliases]);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
if ($isHidden) {
|
|
|
100 |
$definition->addMethodCall('setHidden', [true]);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
if (!$description) {
|
|
|
104 |
if (!$r = $container->getReflectionClass($class)) {
|
|
|
105 |
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
|
|
106 |
}
|
|
|
107 |
if (!$r->isSubclassOf(Command::class)) {
|
|
|
108 |
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, 'console.command', Command::class));
|
|
|
109 |
}
|
|
|
110 |
$description = str_replace('%', '%%', $class::getDefaultDescription() ?? '');
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
if ($description) {
|
|
|
114 |
$definition->addMethodCall('setDescription', [$description]);
|
|
|
115 |
|
|
|
116 |
$container->register('.'.$id.'.lazy', LazyCommand::class)
|
|
|
117 |
->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);
|
|
|
118 |
|
|
|
119 |
$lazyCommandRefs[$id] = new Reference('.'.$id.'.lazy');
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
$container
|
|
|
124 |
->register('console.command_loader', ContainerCommandLoader::class)
|
|
|
125 |
->setPublic(true)
|
|
|
126 |
->addTag('container.no_preload')
|
|
|
127 |
->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
|
|
|
128 |
|
|
|
129 |
$container->setParameter('console.command.ids', $serviceIds);
|
|
|
130 |
}
|
|
|
131 |
}
|