AddConsoleCommandPass.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Command\LazyCommand;
  13. use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
  14. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  15. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  16. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  19. use Symfony\Component\DependencyInjection\Reference;
  20. use Symfony\Component\DependencyInjection\TypedReference;
  21. /**
  22. * Registers console commands.
  23. *
  24. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  25. */
  26. class AddConsoleCommandPass implements CompilerPassInterface
  27. {
  28. /**
  29. * @return void
  30. */
  31. public function process(ContainerBuilder $container)
  32. {
  33. $commandServices = $container->findTaggedServiceIds('console.command', true);
  34. $lazyCommandMap = [];
  35. $lazyCommandRefs = [];
  36. $serviceIds = [];
  37. foreach ($commandServices as $id => $tags) {
  38. $definition = $container->getDefinition($id);
  39. $definition->addTag('container.no_preload');
  40. $class = $container->getParameterBag()->resolveValue($definition->getClass());
  41. if (isset($tags[0]['command'])) {
  42. $aliases = $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, 'console.command', Command::class));
  49. }
  50. $aliases = str_replace('%', '%%', $class::getDefaultName() ?? '');
  51. }
  52. $aliases = explode('|', $aliases ?? '');
  53. $commandName = array_shift($aliases);
  54. if ($isHidden = '' === $commandName) {
  55. $commandName = array_shift($aliases);
  56. }
  57. if (null === $commandName) {
  58. if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag('container.private')) {
  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. $description = $tags[0]['description'] ?? null;
  67. unset($tags[0]);
  68. $lazyCommandMap[$commandName] = $id;
  69. $lazyCommandRefs[$id] = new TypedReference($id, $class);
  70. foreach ($aliases as $alias) {
  71. $lazyCommandMap[$alias] = $id;
  72. }
  73. foreach ($tags as $tag) {
  74. if (isset($tag['command'])) {
  75. $aliases[] = $tag['command'];
  76. $lazyCommandMap[$tag['command']] = $id;
  77. }
  78. $description ??= $tag['description'] ?? null;
  79. }
  80. $definition->addMethodCall('setName', [$commandName]);
  81. if ($aliases) {
  82. $definition->addMethodCall('setAliases', [$aliases]);
  83. }
  84. if ($isHidden) {
  85. $definition->addMethodCall('setHidden', [true]);
  86. }
  87. if (!$description) {
  88. if (!$r = $container->getReflectionClass($class)) {
  89. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  90. }
  91. if (!$r->isSubclassOf(Command::class)) {
  92. throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, 'console.command', Command::class));
  93. }
  94. $description = str_replace('%', '%%', $class::getDefaultDescription() ?? '');
  95. }
  96. if ($description) {
  97. $definition->addMethodCall('setDescription', [$description]);
  98. $container->register('.'.$id.'.lazy', LazyCommand::class)
  99. ->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);
  100. $lazyCommandRefs[$id] = new Reference('.'.$id.'.lazy');
  101. }
  102. }
  103. $container
  104. ->register('console.command_loader', ContainerCommandLoader::class)
  105. ->setPublic(true)
  106. ->addTag('container.no_preload')
  107. ->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
  108. $container->setParameter('console.command.ids', $serviceIds);
  109. }
  110. }