AddConsoleCommandPass.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\CommandLoader\ContainerCommandLoader;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  17. use Symfony\Component\DependencyInjection\TypedReference;
  18. /**
  19. * Registers console commands.
  20. *
  21. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  22. */
  23. class AddConsoleCommandPass implements CompilerPassInterface
  24. {
  25. private $commandLoaderServiceId;
  26. private $commandTag;
  27. public function __construct($commandLoaderServiceId = 'console.command_loader', $commandTag = 'console.command')
  28. {
  29. $this->commandLoaderServiceId = $commandLoaderServiceId;
  30. $this->commandTag = $commandTag;
  31. }
  32. public function process(ContainerBuilder $container)
  33. {
  34. $commandServices = $container->findTaggedServiceIds($this->commandTag, true);
  35. $lazyCommandMap = array();
  36. $lazyCommandRefs = array();
  37. $serviceIds = array();
  38. $lazyServiceIds = array();
  39. foreach ($commandServices as $id => $tags) {
  40. $definition = $container->getDefinition($id);
  41. $class = $container->getParameterBag()->resolveValue($definition->getClass());
  42. $commandId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
  43. if (isset($tags[0]['command'])) {
  44. $commandName = $tags[0]['command'];
  45. } else {
  46. if (!$r = $container->getReflectionClass($class)) {
  47. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  48. }
  49. if (!$r->isSubclassOf(Command::class)) {
  50. throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
  51. }
  52. $commandName = $class::getDefaultName();
  53. }
  54. if (null === $commandName) {
  55. if (isset($serviceIds[$commandId]) || $container->hasAlias($commandId)) {
  56. $commandId = $commandId.'_'.$id;
  57. }
  58. if (!$definition->isPublic() || $definition->isPrivate()) {
  59. $container->setAlias($commandId, $id)->setPublic(true);
  60. $id = $commandId;
  61. }
  62. $serviceIds[$commandId] = $id;
  63. continue;
  64. }
  65. $serviceIds[$commandId] = $id;
  66. $lazyServiceIds[$id] = true;
  67. unset($tags[0]);
  68. $lazyCommandMap[$commandName] = $id;
  69. $lazyCommandRefs[$id] = new TypedReference($id, $class);
  70. $aliases = array();
  71. foreach ($tags as $tag) {
  72. if (isset($tag['command'])) {
  73. $aliases[] = $tag['command'];
  74. $lazyCommandMap[$tag['command']] = $id;
  75. }
  76. }
  77. $definition->addMethodCall('setName', array($commandName));
  78. if ($aliases) {
  79. $definition->addMethodCall('setAliases', array($aliases));
  80. }
  81. }
  82. $container
  83. ->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
  84. ->setPublic(true)
  85. ->setArguments(array(ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap));
  86. $container->setParameter('console.command.ids', $serviceIds);
  87. $container->setParameter('console.lazy_command.ids', $lazyServiceIds);
  88. }
  89. }