LazyCommand.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\Command;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Completion\CompletionInput;
  13. use Symfony\Component\Console\Completion\CompletionSuggestions;
  14. use Symfony\Component\Console\Completion\Suggestion;
  15. use Symfony\Component\Console\Helper\HelperInterface;
  16. use Symfony\Component\Console\Helper\HelperSet;
  17. use Symfony\Component\Console\Input\InputDefinition;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. /**
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. */
  23. final class LazyCommand extends Command
  24. {
  25. private \Closure|Command $command;
  26. private ?bool $isEnabled;
  27. public function __construct(string $name, array $aliases, string $description, bool $isHidden, \Closure $commandFactory, ?bool $isEnabled = true)
  28. {
  29. $this->setName($name)
  30. ->setAliases($aliases)
  31. ->setHidden($isHidden)
  32. ->setDescription($description);
  33. $this->command = $commandFactory;
  34. $this->isEnabled = $isEnabled;
  35. }
  36. public function ignoreValidationErrors(): void
  37. {
  38. $this->getCommand()->ignoreValidationErrors();
  39. }
  40. public function setApplication(?Application $application = null): void
  41. {
  42. if (1 > \func_num_args()) {
  43. trigger_deprecation('symfony/console', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
  44. }
  45. if ($this->command instanceof parent) {
  46. $this->command->setApplication($application);
  47. }
  48. parent::setApplication($application);
  49. }
  50. public function setHelperSet(HelperSet $helperSet): void
  51. {
  52. if ($this->command instanceof parent) {
  53. $this->command->setHelperSet($helperSet);
  54. }
  55. parent::setHelperSet($helperSet);
  56. }
  57. public function isEnabled(): bool
  58. {
  59. return $this->isEnabled ?? $this->getCommand()->isEnabled();
  60. }
  61. public function run(InputInterface $input, OutputInterface $output): int
  62. {
  63. return $this->getCommand()->run($input, $output);
  64. }
  65. public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
  66. {
  67. $this->getCommand()->complete($input, $suggestions);
  68. }
  69. public function setCode(callable $code): static
  70. {
  71. $this->getCommand()->setCode($code);
  72. return $this;
  73. }
  74. /**
  75. * @internal
  76. */
  77. public function mergeApplicationDefinition(bool $mergeArgs = true): void
  78. {
  79. $this->getCommand()->mergeApplicationDefinition($mergeArgs);
  80. }
  81. public function setDefinition(array|InputDefinition $definition): static
  82. {
  83. $this->getCommand()->setDefinition($definition);
  84. return $this;
  85. }
  86. public function getDefinition(): InputDefinition
  87. {
  88. return $this->getCommand()->getDefinition();
  89. }
  90. public function getNativeDefinition(): InputDefinition
  91. {
  92. return $this->getCommand()->getNativeDefinition();
  93. }
  94. /**
  95. * @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
  96. */
  97. public function addArgument(string $name, ?int $mode = null, string $description = '', mixed $default = null /* array|\Closure $suggestedValues = [] */): static
  98. {
  99. $suggestedValues = 5 <= \func_num_args() ? func_get_arg(4) : [];
  100. $this->getCommand()->addArgument($name, $mode, $description, $default, $suggestedValues);
  101. return $this;
  102. }
  103. /**
  104. * @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
  105. */
  106. public function addOption(string $name, string|array|null $shortcut = null, ?int $mode = null, string $description = '', mixed $default = null /* array|\Closure $suggestedValues = [] */): static
  107. {
  108. $suggestedValues = 6 <= \func_num_args() ? func_get_arg(5) : [];
  109. $this->getCommand()->addOption($name, $shortcut, $mode, $description, $default, $suggestedValues);
  110. return $this;
  111. }
  112. public function setProcessTitle(string $title): static
  113. {
  114. $this->getCommand()->setProcessTitle($title);
  115. return $this;
  116. }
  117. public function setHelp(string $help): static
  118. {
  119. $this->getCommand()->setHelp($help);
  120. return $this;
  121. }
  122. public function getHelp(): string
  123. {
  124. return $this->getCommand()->getHelp();
  125. }
  126. public function getProcessedHelp(): string
  127. {
  128. return $this->getCommand()->getProcessedHelp();
  129. }
  130. public function getSynopsis(bool $short = false): string
  131. {
  132. return $this->getCommand()->getSynopsis($short);
  133. }
  134. public function addUsage(string $usage): static
  135. {
  136. $this->getCommand()->addUsage($usage);
  137. return $this;
  138. }
  139. public function getUsages(): array
  140. {
  141. return $this->getCommand()->getUsages();
  142. }
  143. public function getHelper(string $name): HelperInterface
  144. {
  145. return $this->getCommand()->getHelper($name);
  146. }
  147. public function getCommand(): parent
  148. {
  149. if (!$this->command instanceof \Closure) {
  150. return $this->command;
  151. }
  152. $command = $this->command = ($this->command)();
  153. $command->setApplication($this->getApplication());
  154. if (null !== $this->getHelperSet()) {
  155. $command->setHelperSet($this->getHelperSet());
  156. }
  157. $command->setName($this->getName())
  158. ->setAliases($this->getAliases())
  159. ->setHidden($this->isHidden())
  160. ->setDescription($this->getDescription());
  161. // Will throw if the command is not correctly initialized.
  162. $command->getDefinition();
  163. return $command;
  164. }
  165. }