4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\DependencyInjection;
14 use Symfony\Component\Console\Command\Command;
15 use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
16 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17 use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
18 use Symfony\Component\DependencyInjection\ContainerBuilder;
19 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
20 use Symfony\Component\DependencyInjection\TypedReference;
23 * Registers console commands.
25 * @author Grégoire Pineau <lyrixx@lyrixx.info>
27 class AddConsoleCommandPass implements CompilerPassInterface
29 private $commandLoaderServiceId;
32 public function __construct($commandLoaderServiceId = 'console.command_loader', $commandTag = 'console.command')
34 $this->commandLoaderServiceId = $commandLoaderServiceId;
35 $this->commandTag = $commandTag;
38 public function process(ContainerBuilder $container)
40 $commandServices = $container->findTaggedServiceIds($this->commandTag, true);
41 $lazyCommandMap = array();
42 $lazyCommandRefs = array();
43 $serviceIds = array();
44 $lazyServiceIds = array();
46 foreach ($commandServices as $id => $tags) {
47 $definition = $container->getDefinition($id);
48 $class = $container->getParameterBag()->resolveValue($definition->getClass());
50 $commandId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
52 if (isset($tags[0]['command'])) {
53 $commandName = $tags[0]['command'];
55 if (!$r = $container->getReflectionClass($class)) {
56 throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
58 if (!$r->isSubclassOf(Command::class)) {
59 throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
61 $commandName = $class::getDefaultName();
64 if (null === $commandName) {
65 if (isset($serviceIds[$commandId]) || $container->hasAlias($commandId)) {
66 $commandId = $commandId.'_'.$id;
68 if (!$definition->isPublic() || $definition->isPrivate()) {
69 $container->setAlias($commandId, $id)->setPublic(true);
72 $serviceIds[$commandId] = $id;
77 $serviceIds[$commandId] = $id;
78 $lazyServiceIds[$id] = true;
80 $lazyCommandMap[$commandName] = $id;
81 $lazyCommandRefs[$id] = new TypedReference($id, $class);
84 foreach ($tags as $tag) {
85 if (isset($tag['command'])) {
86 $aliases[] = $tag['command'];
87 $lazyCommandMap[$tag['command']] = $id;
91 $definition->addMethodCall('setName', array($commandName));
94 $definition->addMethodCall('setAliases', array($aliases));
99 ->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
101 ->setArguments(array(ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap));
103 $container->setParameter('console.command.ids', $serviceIds);
104 $container->setParameter('console.lazy_command.ids', $lazyServiceIds);