AddConsoleCommandPass.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if (Command::class !== (new \ReflectionMethod($class, 'getDefaultName'))->class) {
  55. trigger_deprecation('symfony/console', '7.3', 'Overriding "Command::getDefaultName()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', $class);
  56. $defaultName = $class::getDefaultName();
  57. } else {
  58. $defaultName = $attribute?->name;
  59. }
  60. $aliases = str_replace('%', '%%', $tags[0]['command'] ?? $defaultName ?? '');
  61. $aliases = explode('|', $aliases);
  62. $commandName = array_shift($aliases);
  63. if ($isHidden = '' === $commandName) {
  64. $commandName = array_shift($aliases);
  65. }
  66. if (null === $commandName) {
  67. if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag('container.private')) {
  68. $commandId = 'console.command.public_alias.'.$id;
  69. $container->setAlias($commandId, $id)->setPublic(true);
  70. $id = $commandId;
  71. }
  72. $serviceIds[] = $id;
  73. continue;
  74. }
  75. $description = $tags[0]['description'] ?? null;
  76. $help = $tags[0]['help'] ?? null;
  77. unset($tags[0]);
  78. $lazyCommandMap[$commandName] = $id;
  79. $lazyCommandRefs[$id] = new TypedReference($id, $class);
  80. foreach ($aliases as $alias) {
  81. $lazyCommandMap[$alias] = $id;
  82. }
  83. foreach ($tags as $tag) {
  84. if (isset($tag['command'])) {
  85. $aliases[] = $tag['command'];
  86. $lazyCommandMap[$tag['command']] = $id;
  87. }
  88. $description ??= $tag['description'] ?? null;
  89. $help ??= $tag['help'] ?? null;
  90. }
  91. $definition->addMethodCall('setName', [$commandName]);
  92. if ($aliases) {
  93. $definition->addMethodCall('setAliases', [$aliases]);
  94. }
  95. if ($isHidden) {
  96. $definition->addMethodCall('setHidden', [true]);
  97. }
  98. if ($help && $invokableRef) {
  99. $definition->addMethodCall('setHelp', [str_replace('%', '%%', $help)]);
  100. }
  101. if (!$description) {
  102. if (Command::class !== (new \ReflectionMethod($class, 'getDefaultDescription'))->class) {
  103. trigger_deprecation('symfony/console', '7.3', 'Overriding "Command::getDefaultDescription()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.', $class);
  104. $description = $class::getDefaultDescription();
  105. } else {
  106. $description = $attribute?->description;
  107. }
  108. }
  109. if ($description) {
  110. $definition->addMethodCall('setDescription', [str_replace('%', '%%', $description)]);
  111. $container->register('.'.$id.'.lazy', LazyCommand::class)
  112. ->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);
  113. $lazyCommandRefs[$id] = new Reference('.'.$id.'.lazy');
  114. }
  115. }
  116. $container
  117. ->register('console.command_loader', ContainerCommandLoader::class)
  118. ->setPublic(true)
  119. ->addTag('container.no_preload')
  120. ->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
  121. $container->setParameter('console.command.ids', $serviceIds);
  122. }
  123. }