AddConsoleCommandPass.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Attribute\AsCommand;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Command\LazyCommand;
  14. use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
  15. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  16. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  17. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  20. use Symfony\Component\DependencyInjection\Reference;
  21. use Symfony\Component\DependencyInjection\TypedReference;
  22. /**
  23. * Registers console commands.
  24. *
  25. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  26. */
  27. class AddConsoleCommandPass implements CompilerPassInterface
  28. {
  29. public function process(ContainerBuilder $container): void
  30. {
  31. $commandServices = $container->findTaggedServiceIds('console.command', true);
  32. $lazyCommandMap = [];
  33. $lazyCommandRefs = [];
  34. $serviceIds = [];
  35. foreach ($commandServices as $id => $tags) {
  36. $definition = $container->getDefinition($id);
  37. $class = $container->getParameterBag()->resolveValue($definition->getClass());
  38. if (!$r = $container->getReflectionClass($class)) {
  39. throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  40. }
  41. if (!$r->isSubclassOf(Command::class)) {
  42. if (!$r->hasMethod('__invoke')) {
  43. throw new InvalidArgumentException(\sprintf('The service "%s" tagged "%s" must either be a subclass of "%s" or have an "__invoke()" method.', $id, 'console.command', Command::class));
  44. }
  45. $invokableRef = new Reference($id);
  46. $definition = $container->register($id .= '.command', $class = Command::class)
  47. ->addMethodCall('setCode', [$invokableRef]);
  48. } else {
  49. $invokableRef = null;
  50. }
  51. $definition->addTag('container.no_preload');
  52. /** @var AsCommand|null $attribute */
  53. $attribute = ($r->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
  54. $defaultName = $attribute?->name;
  55. $aliases = str_replace('%', '%%', $tags[0]['command'] ?? $defaultName ?? '');
  56. $aliases = explode('|', $aliases);
  57. $commandName = array_shift($aliases);
  58. if ($isHidden = '' === $commandName) {
  59. $commandName = array_shift($aliases);
  60. }
  61. if (null === $commandName) {
  62. if ($definition->isPrivate() || $definition->hasTag('container.private')) {
  63. $commandId = 'console.command.public_alias.'.$id;
  64. $container->setAlias($commandId, $id)->setPublic(true);
  65. $id = $commandId;
  66. }
  67. $serviceIds[] = $id;
  68. continue;
  69. }
  70. $description = $tags[0]['description'] ?? null;
  71. $help = $tags[0]['help'] ?? null;
  72. $usages = $tags[0]['usages'] ?? null;
  73. unset($tags[0]);
  74. $lazyCommandMap[$commandName] = $id;
  75. $lazyCommandRefs[$id] = new TypedReference($id, $class);
  76. foreach ($aliases as $alias) {
  77. $lazyCommandMap[$alias] = $id;
  78. }
  79. foreach ($tags as $tag) {
  80. if (isset($tag['command'])) {
  81. $aliases[] = $tag['command'];
  82. $lazyCommandMap[$tag['command']] = $id;
  83. }
  84. $description ??= $tag['description'] ?? null;
  85. $help ??= $tag['help'] ?? null;
  86. $usages ??= $tag['usages'] ?? null;
  87. }
  88. $definition->addMethodCall('setName', [$commandName]);
  89. if ($aliases) {
  90. $definition->addMethodCall('setAliases', [$aliases]);
  91. }
  92. if ($isHidden) {
  93. $definition->addMethodCall('setHidden', [true]);
  94. }
  95. if ($help && $invokableRef) {
  96. $definition->addMethodCall('setHelp', [str_replace('%', '%%', $help)]);
  97. }
  98. if ($usages) {
  99. foreach ($usages as $usage) {
  100. $definition->addMethodCall('addUsage', [$usage]);
  101. }
  102. }
  103. if ($description ??= $attribute?->description) {
  104. $escapedDescription = str_replace('%', '%%', $description);
  105. $definition->addMethodCall('setDescription', [$escapedDescription]);
  106. $container->register('.'.$id.'.lazy', LazyCommand::class)
  107. ->setArguments([$commandName, $aliases, $escapedDescription, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);
  108. $lazyCommandRefs[$id] = new Reference('.'.$id.'.lazy');
  109. }
  110. }
  111. $container
  112. ->register('console.command_loader', ContainerCommandLoader::class)
  113. ->setPublic(true)
  114. ->addTag('container.no_preload')
  115. ->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
  116. $container->setParameter('console.command.ids', $serviceIds);
  117. }
  118. }