AddConsoleCommandPass.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
  31. */
  32. public function process($container)
  33. {
  34. $commandServices = $container->findTaggedServiceIds('console.command', true);
  35. $lazyCommandMap = [];
  36. $lazyCommandRefs = [];
  37. $serviceIds = [];
  38. foreach ($commandServices as $id => $tags) {
  39. $definition = $container->getDefinition($id);
  40. $definition->addTag('container.no_preload');
  41. $class = $container->getParameterBag()->resolveValue($definition->getClass());
  42. if (isset($tags[0]['command'])) {
  43. $aliases = $tags[0]['command'];
  44. } else {
  45. if (!$r = $container->getReflectionClass($class)) {
  46. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  47. }
  48. if (!$r->isSubclassOf(Command::class)) {
  49. throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, 'console.command', Command::class));
  50. }
  51. $aliases = str_replace('%', '%%', $class::getDefaultName() ?? '');
  52. }
  53. $aliases = explode('|', $aliases ?? '');
  54. $commandName = array_shift($aliases);
  55. if ($isHidden = '' === $commandName) {
  56. $commandName = array_shift($aliases);
  57. }
  58. if (null === $commandName) {
  59. if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag('container.private')) {
  60. $commandId = 'console.command.public_alias.'.$id;
  61. $container->setAlias($commandId, $id)->setPublic(true);
  62. $id = $commandId;
  63. }
  64. $serviceIds[] = $id;
  65. continue;
  66. }
  67. $description = $tags[0]['description'] ?? null;
  68. unset($tags[0]);
  69. $lazyCommandMap[$commandName] = $id;
  70. $lazyCommandRefs[$id] = new TypedReference($id, $class);
  71. foreach ($aliases as $alias) {
  72. $lazyCommandMap[$alias] = $id;
  73. }
  74. foreach ($tags as $tag) {
  75. if (isset($tag['command'])) {
  76. $aliases[] = $tag['command'];
  77. $lazyCommandMap[$tag['command']] = $id;
  78. }
  79. $description = $description ?? $tag['description'] ?? null;
  80. }
  81. $definition->addMethodCall('setName', [$commandName]);
  82. if ($aliases) {
  83. $definition->addMethodCall('setAliases', [$aliases]);
  84. }
  85. if ($isHidden) {
  86. $definition->addMethodCall('setHidden', [true]);
  87. }
  88. if (!$description) {
  89. if (!$r = $container->getReflectionClass($class)) {
  90. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  91. }
  92. if (!$r->isSubclassOf(Command::class)) {
  93. throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, 'console.command', Command::class));
  94. }
  95. $description = str_replace('%', '%%', $class::getDefaultDescription() ?? '');
  96. }
  97. if ($description) {
  98. $definition->addMethodCall('setDescription', [$description]);
  99. $container->register('.'.$id.'.lazy', LazyCommand::class)
  100. ->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);
  101. $lazyCommandRefs[$id] = new Reference('.'.$id.'.lazy');
  102. }
  103. }
  104. $container
  105. ->register('console.command_loader', ContainerCommandLoader::class)
  106. ->setPublic(true)
  107. ->addTag('container.no_preload')
  108. ->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
  109. $container->setParameter('console.command.ids', $serviceIds);
  110. }
  111. }