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.

211 lines
5.0 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\Command;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Helper\HelperSet;
  13. use Symfony\Component\Console\Input\InputDefinition;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. /**
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. final class LazyCommand extends Command
  20. {
  21. private $command;
  22. private $isEnabled;
  23. public function __construct(string $name, array $aliases, string $description, bool $isHidden, \Closure $commandFactory, ?bool $isEnabled = true)
  24. {
  25. $this->setName($name)
  26. ->setAliases($aliases)
  27. ->setHidden($isHidden)
  28. ->setDescription($description);
  29. $this->command = $commandFactory;
  30. $this->isEnabled = $isEnabled;
  31. }
  32. public function ignoreValidationErrors(): void
  33. {
  34. $this->getCommand()->ignoreValidationErrors();
  35. }
  36. public function setApplication(Application $application = null): void
  37. {
  38. if ($this->command instanceof parent) {
  39. $this->command->setApplication($application);
  40. }
  41. parent::setApplication($application);
  42. }
  43. public function setHelperSet(HelperSet $helperSet): void
  44. {
  45. if ($this->command instanceof parent) {
  46. $this->command->setHelperSet($helperSet);
  47. }
  48. parent::setHelperSet($helperSet);
  49. }
  50. public function isEnabled(): bool
  51. {
  52. return $this->isEnabled ?? $this->getCommand()->isEnabled();
  53. }
  54. public function run(InputInterface $input, OutputInterface $output): int
  55. {
  56. return $this->getCommand()->run($input, $output);
  57. }
  58. /**
  59. * @return $this
  60. */
  61. public function setCode(callable $code): self
  62. {
  63. $this->getCommand()->setCode($code);
  64. return $this;
  65. }
  66. /**
  67. * @internal
  68. */
  69. public function mergeApplicationDefinition(bool $mergeArgs = true): void
  70. {
  71. $this->getCommand()->mergeApplicationDefinition($mergeArgs);
  72. }
  73. /**
  74. * @return $this
  75. */
  76. public function setDefinition($definition): self
  77. {
  78. $this->getCommand()->setDefinition($definition);
  79. return $this;
  80. }
  81. public function getDefinition(): InputDefinition
  82. {
  83. return $this->getCommand()->getDefinition();
  84. }
  85. public function getNativeDefinition(): InputDefinition
  86. {
  87. return $this->getCommand()->getNativeDefinition();
  88. }
  89. /**
  90. * @return $this
  91. */
  92. public function addArgument(string $name, int $mode = null, string $description = '', $default = null): self
  93. {
  94. $this->getCommand()->addArgument($name, $mode, $description, $default);
  95. return $this;
  96. }
  97. /**
  98. * @return $this
  99. */
  100. public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null): self
  101. {
  102. $this->getCommand()->addOption($name, $shortcut, $mode, $description, $default);
  103. return $this;
  104. }
  105. /**
  106. * @return $this
  107. */
  108. public function setProcessTitle(string $title): self
  109. {
  110. $this->getCommand()->setProcessTitle($title);
  111. return $this;
  112. }
  113. /**
  114. * @return $this
  115. */
  116. public function setHelp(string $help): self
  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. /**
  134. * @return $this
  135. */
  136. public function addUsage(string $usage): self
  137. {
  138. $this->getCommand()->addUsage($usage);
  139. return $this;
  140. }
  141. public function getUsages(): array
  142. {
  143. return $this->getCommand()->getUsages();
  144. }
  145. /**
  146. * @return mixed
  147. */
  148. public function getHelper(string $name)
  149. {
  150. return $this->getCommand()->getHelper($name);
  151. }
  152. public function getCommand(): parent
  153. {
  154. if (!$this->command instanceof \Closure) {
  155. return $this->command;
  156. }
  157. $command = $this->command = ($this->command)();
  158. $command->setApplication($this->getApplication());
  159. if (null !== $this->getHelperSet()) {
  160. $command->setHelperSet($this->getHelperSet());
  161. }
  162. $command->setName($this->getName())
  163. ->setAliases($this->getAliases())
  164. ->setHidden($this->isHidden())
  165. ->setDescription($this->getDescription());
  166. // Will throw if the command is not correctly initialized.
  167. $command->getDefinition();
  168. return $command;
  169. }
  170. }