AddConsoleCommandPass.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. private $privateTagName;
  29. public function __construct(string $commandLoaderServiceId = 'console.command_loader', string $commandTag = 'console.command', string $noPreloadTag = 'container.no_preload', string $privateTagName = 'container.private')
  30. {
  31. $this->commandLoaderServiceId = $commandLoaderServiceId;
  32. $this->commandTag = $commandTag;
  33. $this->noPreloadTag = $noPreloadTag;
  34. $this->privateTagName = $privateTagName;
  35. }
  36. public function process(ContainerBuilder $container)
  37. {
  38. $commandServices = $container->findTaggedServiceIds($this->commandTag, true);
  39. $lazyCommandMap = [];
  40. $lazyCommandRefs = [];
  41. $serviceIds = [];
  42. foreach ($commandServices as $id => $tags) {
  43. $definition = $container->getDefinition($id);
  44. $definition->addTag($this->noPreloadTag);
  45. $class = $container->getParameterBag()->resolveValue($definition->getClass());
  46. if (isset($tags[0]['command'])) {
  47. $commandName = $tags[0]['command'];
  48. } else {
  49. if (!$r = $container->getReflectionClass($class)) {
  50. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  51. }
  52. if (!$r->isSubclassOf(Command::class)) {
  53. throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
  54. }
  55. $commandName = $class::getDefaultName();
  56. }
  57. if (null === $commandName) {
  58. if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag($this->privateTagName)) {
  59. $commandId = 'console.command.public_alias.'.$id;
  60. $container->setAlias($commandId, $id)->setPublic(true);
  61. $id = $commandId;
  62. }
  63. $serviceIds[] = $id;
  64. continue;
  65. }
  66. unset($tags[0]);
  67. $lazyCommandMap[$commandName] = $id;
  68. $lazyCommandRefs[$id] = new TypedReference($id, $class);
  69. $aliases = [];
  70. foreach ($tags as $tag) {
  71. if (isset($tag['command'])) {
  72. $aliases[] = $tag['command'];
  73. $lazyCommandMap[$tag['command']] = $id;
  74. }
  75. }
  76. $definition->addMethodCall('setName', [$commandName]);
  77. if ($aliases) {
  78. $definition->addMethodCall('setAliases', [$aliases]);
  79. }
  80. }
  81. $container
  82. ->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
  83. ->setPublic(true)
  84. ->addTag($this->noPreloadTag)
  85. ->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
  86. $container->setParameter('console.command.ids', $serviceIds);
  87. }
  88. }