LazyCommand.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. public function __construct(
  27. string $name,
  28. array $aliases,
  29. string $description,
  30. bool $isHidden,
  31. \Closure $commandFactory,
  32. private ?bool $isEnabled = true,
  33. ) {
  34. $this->setName($name)
  35. ->setAliases($aliases)
  36. ->setHidden($isHidden)
  37. ->setDescription($description);
  38. $this->command = $commandFactory;
  39. }
  40. public function ignoreValidationErrors(): void
  41. {
  42. $this->getCommand()->ignoreValidationErrors();
  43. }
  44. public function setApplication(?Application $application): void
  45. {
  46. if ($this->command instanceof parent) {
  47. $this->command->setApplication($application);
  48. }
  49. parent::setApplication($application);
  50. }
  51. public function setHelperSet(HelperSet $helperSet): void
  52. {
  53. if ($this->command instanceof parent) {
  54. $this->command->setHelperSet($helperSet);
  55. }
  56. parent::setHelperSet($helperSet);
  57. }
  58. public function isEnabled(): bool
  59. {
  60. return $this->isEnabled ?? $this->getCommand()->isEnabled();
  61. }
  62. public function run(InputInterface $input, OutputInterface $output): int
  63. {
  64. return $this->getCommand()->run($input, $output);
  65. }
  66. public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
  67. {
  68. $this->getCommand()->complete($input, $suggestions);
  69. }
  70. public function setCode(callable $code): static
  71. {
  72. $this->getCommand()->setCode($code);
  73. return $this;
  74. }
  75. /**
  76. * @internal
  77. */
  78. public function mergeApplicationDefinition(bool $mergeArgs = true): void
  79. {
  80. $this->getCommand()->mergeApplicationDefinition($mergeArgs);
  81. }
  82. public function setDefinition(array|InputDefinition $definition): static
  83. {
  84. $this->getCommand()->setDefinition($definition);
  85. return $this;
  86. }
  87. public function getDefinition(): InputDefinition
  88. {
  89. return $this->getCommand()->getDefinition();
  90. }
  91. public function getNativeDefinition(): InputDefinition
  92. {
  93. return $this->getCommand()->getNativeDefinition();
  94. }
  95. /**
  96. * @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
  97. */
  98. public function addArgument(string $name, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static
  99. {
  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. $this->getCommand()->addOption($name, $shortcut, $mode, $description, $default, $suggestedValues);
  109. return $this;
  110. }
  111. public function setProcessTitle(string $title): static
  112. {
  113. $this->getCommand()->setProcessTitle($title);
  114. return $this;
  115. }
  116. public function setHelp(string $help): static
  117. {
  118. $this->getCommand()->setHelp($help);
  119. return $this;
  120. }
  121. public function getHelp(): string
  122. {
  123. return $this->getCommand()->getHelp();
  124. }
  125. public function getProcessedHelp(): string
  126. {
  127. return $this->getCommand()->getProcessedHelp();
  128. }
  129. public function getSynopsis(bool $short = false): string
  130. {
  131. return $this->getCommand()->getSynopsis($short);
  132. }
  133. public function addUsage(string $usage): static
  134. {
  135. $this->getCommand()->addUsage($usage);
  136. return $this;
  137. }
  138. public function getUsages(): array
  139. {
  140. return $this->getCommand()->getUsages();
  141. }
  142. public function getHelper(string $name): HelperInterface
  143. {
  144. return $this->getCommand()->getHelper($name);
  145. }
  146. public function getCommand(): parent
  147. {
  148. if (!$this->command instanceof \Closure) {
  149. return $this->command;
  150. }
  151. $command = $this->command = ($this->command)();
  152. $command->setApplication($this->getApplication());
  153. if (null !== $this->getHelperSet()) {
  154. $command->setHelperSet($this->getHelperSet());
  155. }
  156. $command->setName($this->getName())
  157. ->setAliases($this->getAliases())
  158. ->setHidden($this->isHidden())
  159. ->setDescription($this->getDescription());
  160. // Will throw if the command is not correctly initialized.
  161. $command->getDefinition();
  162. return $command;
  163. }
  164. }