You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.7 KiB

3 years ago
  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. private $commandLoaderServiceId;
  29. private $commandTag;
  30. private $noPreloadTag;
  31. private $privateTagName;
  32. public function __construct(string $commandLoaderServiceId = 'console.command_loader', string $commandTag = 'console.command', string $noPreloadTag = 'container.no_preload', string $privateTagName = 'container.private')
  33. {
  34. if (0 < \func_num_args()) {
  35. trigger_deprecation('symfony/console', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  36. }
  37. $this->commandLoaderServiceId = $commandLoaderServiceId;
  38. $this->commandTag = $commandTag;
  39. $this->noPreloadTag = $noPreloadTag;
  40. $this->privateTagName = $privateTagName;
  41. }
  42. public function process(ContainerBuilder $container)
  43. {
  44. $commandServices = $container->findTaggedServiceIds($this->commandTag, true);
  45. $lazyCommandMap = [];
  46. $lazyCommandRefs = [];
  47. $serviceIds = [];
  48. foreach ($commandServices as $id => $tags) {
  49. $definition = $container->getDefinition($id);
  50. $definition->addTag($this->noPreloadTag);
  51. $class = $container->getParameterBag()->resolveValue($definition->getClass());
  52. if (isset($tags[0]['command'])) {
  53. $aliases = $tags[0]['command'];
  54. } else {
  55. if (!$r = $container->getReflectionClass($class)) {
  56. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  57. }
  58. if (!$r->isSubclassOf(Command::class)) {
  59. throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
  60. }
  61. $aliases = $class::getDefaultName();
  62. }
  63. $aliases = explode('|', $aliases ?? '');
  64. $commandName = array_shift($aliases);
  65. if ($isHidden = '' === $commandName) {
  66. $commandName = array_shift($aliases);
  67. }
  68. if (null === $commandName) {
  69. if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag($this->privateTagName)) {
  70. $commandId = 'console.command.public_alias.'.$id;
  71. $container->setAlias($commandId, $id)->setPublic(true);
  72. $id = $commandId;
  73. }
  74. $serviceIds[] = $id;
  75. continue;
  76. }
  77. $description = $tags[0]['description'] ?? null;
  78. unset($tags[0]);
  79. $lazyCommandMap[$commandName] = $id;
  80. $lazyCommandRefs[$id] = new TypedReference($id, $class);
  81. foreach ($aliases as $alias) {
  82. $lazyCommandMap[$alias] = $id;
  83. }
  84. foreach ($tags as $tag) {
  85. if (isset($tag['command'])) {
  86. $aliases[] = $tag['command'];
  87. $lazyCommandMap[$tag['command']] = $id;
  88. }
  89. $description = $description ?? $tag['description'] ?? 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 (!$description) {
  99. if (!$r = $container->getReflectionClass($class)) {
  100. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  101. }
  102. if (!$r->isSubclassOf(Command::class)) {
  103. throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
  104. }
  105. $description = $class::getDefaultDescription();
  106. }
  107. if ($description) {
  108. $definition->addMethodCall('setDescription', [$description]);
  109. $container->register('.'.$id.'.lazy', LazyCommand::class)
  110. ->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);
  111. $lazyCommandRefs[$id] = new Reference('.'.$id.'.lazy');
  112. }
  113. }
  114. $container
  115. ->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
  116. ->setPublic(true)
  117. ->addTag($this->noPreloadTag)
  118. ->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
  119. $container->setParameter('console.command.ids', $serviceIds);
  120. }
  121. }