AddConsoleCommandPass.php 3.5 KB

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