AddConsoleCommandPass.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. private $noPreloadTag;
  28. public function __construct(string $commandLoaderServiceId = 'console.command_loader', string $commandTag = 'console.command', string $noPreloadTag = 'container.no_preload')
  29. {
  30. $this->commandLoaderServiceId = $commandLoaderServiceId;
  31. $this->commandTag = $commandTag;
  32. $this->noPreloadTag = $noPreloadTag;
  33. }
  34. public function process(ContainerBuilder $container)
  35. {
  36. $commandServices = $container->findTaggedServiceIds($this->commandTag, true);
  37. $lazyCommandMap = [];
  38. $lazyCommandRefs = [];
  39. $serviceIds = [];
  40. foreach ($commandServices as $id => $tags) {
  41. $definition = $container->getDefinition($id);
  42. $definition->addTag($this->noPreloadTag);
  43. $class = $container->getParameterBag()->resolveValue($definition->getClass());
  44. if (isset($tags[0]['command'])) {
  45. $commandName = $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, $this->commandTag, Command::class));
  52. }
  53. $commandName = $class::getDefaultName();
  54. }
  55. if (null === $commandName) {
  56. if (!$definition->isPublic() || $definition->isPrivate()) {
  57. $commandId = 'console.command.public_alias.'.$id;
  58. $container->setAlias($commandId, $id)->setPublic(true);
  59. $id = $commandId;
  60. }
  61. $serviceIds[] = $id;
  62. continue;
  63. }
  64. unset($tags[0]);
  65. $lazyCommandMap[$commandName] = $id;
  66. $lazyCommandRefs[$id] = new TypedReference($id, $class);
  67. $aliases = [];
  68. foreach ($tags as $tag) {
  69. if (isset($tag['command'])) {
  70. $aliases[] = $tag['command'];
  71. $lazyCommandMap[$tag['command']] = $id;
  72. }
  73. }
  74. $definition->addMethodCall('setName', [$commandName]);
  75. if ($aliases) {
  76. $definition->addMethodCall('setAliases', [$aliases]);
  77. }
  78. }
  79. $container
  80. ->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
  81. ->setPublic(true)
  82. ->addTag($this->noPreloadTag)
  83. ->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
  84. $container->setParameter('console.command.ids', $serviceIds);
  85. }
  86. }