AddConsoleCommandPass.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 = array();
  36. $lazyCommandRefs = array();
  37. $serviceIds = array();
  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 = array();
  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', array($commandName));
  72. if ($aliases) {
  73. $definition->addMethodCall('setAliases', array($aliases));
  74. }
  75. }
  76. $container
  77. ->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
  78. ->setPublic(true)
  79. ->setArguments(array(ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap));
  80. $container->setParameter('console.command.ids', $serviceIds);
  81. }
  82. }