annotate vendor/symfony/console/DependencyInjection/AddConsoleCommandPass.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 /*
Chris@14 4 * This file is part of the Symfony package.
Chris@14 5 *
Chris@14 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@14 7 *
Chris@14 8 * For the full copyright and license information, please view the LICENSE
Chris@14 9 * file that was distributed with this source code.
Chris@14 10 */
Chris@14 11
Chris@14 12 namespace Symfony\Component\Console\DependencyInjection;
Chris@14 13
Chris@14 14 use Symfony\Component\Console\Command\Command;
Chris@14 15 use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
Chris@14 16 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Chris@14 17 use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
Chris@14 18 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@14 19 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Chris@14 20 use Symfony\Component\DependencyInjection\TypedReference;
Chris@14 21
Chris@14 22 /**
Chris@14 23 * Registers console commands.
Chris@14 24 *
Chris@14 25 * @author Grégoire Pineau <lyrixx@lyrixx.info>
Chris@14 26 */
Chris@14 27 class AddConsoleCommandPass implements CompilerPassInterface
Chris@14 28 {
Chris@14 29 private $commandLoaderServiceId;
Chris@14 30 private $commandTag;
Chris@14 31
Chris@14 32 public function __construct($commandLoaderServiceId = 'console.command_loader', $commandTag = 'console.command')
Chris@14 33 {
Chris@14 34 $this->commandLoaderServiceId = $commandLoaderServiceId;
Chris@14 35 $this->commandTag = $commandTag;
Chris@14 36 }
Chris@14 37
Chris@14 38 public function process(ContainerBuilder $container)
Chris@14 39 {
Chris@14 40 $commandServices = $container->findTaggedServiceIds($this->commandTag, true);
Chris@17 41 $lazyCommandMap = [];
Chris@17 42 $lazyCommandRefs = [];
Chris@17 43 $serviceIds = [];
Chris@17 44 $lazyServiceIds = [];
Chris@14 45
Chris@14 46 foreach ($commandServices as $id => $tags) {
Chris@14 47 $definition = $container->getDefinition($id);
Chris@14 48 $class = $container->getParameterBag()->resolveValue($definition->getClass());
Chris@14 49
Chris@14 50 $commandId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
Chris@14 51
Chris@14 52 if (isset($tags[0]['command'])) {
Chris@14 53 $commandName = $tags[0]['command'];
Chris@14 54 } else {
Chris@14 55 if (!$r = $container->getReflectionClass($class)) {
Chris@14 56 throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
Chris@14 57 }
Chris@14 58 if (!$r->isSubclassOf(Command::class)) {
Chris@14 59 throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
Chris@14 60 }
Chris@14 61 $commandName = $class::getDefaultName();
Chris@14 62 }
Chris@14 63
Chris@14 64 if (null === $commandName) {
Chris@14 65 if (isset($serviceIds[$commandId]) || $container->hasAlias($commandId)) {
Chris@14 66 $commandId = $commandId.'_'.$id;
Chris@14 67 }
Chris@14 68 if (!$definition->isPublic() || $definition->isPrivate()) {
Chris@14 69 $container->setAlias($commandId, $id)->setPublic(true);
Chris@14 70 $id = $commandId;
Chris@14 71 }
Chris@14 72 $serviceIds[$commandId] = $id;
Chris@14 73
Chris@14 74 continue;
Chris@14 75 }
Chris@14 76
Chris@14 77 $serviceIds[$commandId] = $id;
Chris@14 78 $lazyServiceIds[$id] = true;
Chris@14 79 unset($tags[0]);
Chris@14 80 $lazyCommandMap[$commandName] = $id;
Chris@14 81 $lazyCommandRefs[$id] = new TypedReference($id, $class);
Chris@17 82 $aliases = [];
Chris@14 83
Chris@14 84 foreach ($tags as $tag) {
Chris@14 85 if (isset($tag['command'])) {
Chris@14 86 $aliases[] = $tag['command'];
Chris@14 87 $lazyCommandMap[$tag['command']] = $id;
Chris@14 88 }
Chris@14 89 }
Chris@14 90
Chris@17 91 $definition->addMethodCall('setName', [$commandName]);
Chris@14 92
Chris@14 93 if ($aliases) {
Chris@17 94 $definition->addMethodCall('setAliases', [$aliases]);
Chris@14 95 }
Chris@14 96 }
Chris@14 97
Chris@14 98 $container
Chris@14 99 ->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
Chris@14 100 ->setPublic(true)
Chris@17 101 ->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
Chris@14 102
Chris@14 103 $container->setParameter('console.command.ids', $serviceIds);
Chris@14 104 $container->setParameter('console.lazy_command.ids', $lazyServiceIds);
Chris@14 105 }
Chris@14 106 }