| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Command; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Application; |
| 15 |
use Symfony\Component\Console\Completion\CompletionInput; |
| 16 |
use Symfony\Component\Console\Completion\CompletionSuggestions; |
| 17 |
use Symfony\Component\Console\Completion\Suggestion; |
| 18 |
use Symfony\Component\Console\Helper\HelperInterface; |
| 19 |
use Symfony\Component\Console\Helper\HelperSet; |
| 20 |
use Symfony\Component\Console\Input\InputDefinition; |
| 21 |
use Symfony\Component\Console\Input\InputInterface; |
| 22 |
use Symfony\Component\Console\Output\OutputInterface; |
| 23 |
|
| 24 |
/** |
| 25 |
* @author Nicolas Grekas <[email protected]> |
| 26 |
*/ |
| 27 |
final class LazyCommand extends Command |
| 28 |
{ |
| 29 |
private \Closure|Command $command; |
| 30 |
|
| 31 |
public function __construct( |
| 32 |
string $name, |
| 33 |
array $aliases, |
| 34 |
string $description, |
| 35 |
bool $isHidden, |
| 36 |
\Closure $commandFactory, |
| 37 |
private ?bool $isEnabled = true, |
| 38 |
) { |
| 39 |
$this->setName($name) |
| 40 |
->setAliases($aliases) |
| 41 |
->setHidden($isHidden) |
| 42 |
->setDescription($description); |
| 43 |
|
| 44 |
$this->command = $commandFactory; |
| 45 |
} |
| 46 |
|
| 47 |
public function ignoreValidationErrors(): void |
| 48 |
{ |
| 49 |
$this->getCommand()->ignoreValidationErrors(); |
| 50 |
} |
| 51 |
|
| 52 |
public function setApplication(?Application $application): void |
| 53 |
{ |
| 54 |
if ($this->command instanceof parent) { |
| 55 |
$this->command->setApplication($application); |
| 56 |
} |
| 57 |
|
| 58 |
parent::setApplication($application); |
| 59 |
} |
| 60 |
|
| 61 |
public function setHelperSet(HelperSet $helperSet): void |
| 62 |
{ |
| 63 |
if ($this->command instanceof parent) { |
| 64 |
$this->command->setHelperSet($helperSet); |
| 65 |
} |
| 66 |
|
| 67 |
parent::setHelperSet($helperSet); |
| 68 |
} |
| 69 |
|
| 70 |
public function isEnabled(): bool |
| 71 |
{ |
| 72 |
return $this->isEnabled ?? $this->getCommand()->isEnabled(); |
| 73 |
} |
| 74 |
|
| 75 |
public function run(InputInterface $input, OutputInterface $output): int |
| 76 |
{ |
| 77 |
return $this->getCommand()->run($input, $output); |
| 78 |
} |
| 79 |
|
| 80 |
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
| 81 |
{ |
| 82 |
$this->getCommand()->complete($input, $suggestions); |
| 83 |
} |
| 84 |
|
| 85 |
public function setCode(callable $code): static |
| 86 |
{ |
| 87 |
$this->getCommand()->setCode($code); |
| 88 |
|
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @internal |
| 94 |
*/ |
| 95 |
public function mergeApplicationDefinition(bool $mergeArgs = true): void |
| 96 |
{ |
| 97 |
$this->getCommand()->mergeApplicationDefinition($mergeArgs); |
| 98 |
} |
| 99 |
|
| 100 |
public function setDefinition(array|InputDefinition $definition): static |
| 101 |
{ |
| 102 |
$this->getCommand()->setDefinition($definition); |
| 103 |
|
| 104 |
return $this; |
| 105 |
} |
| 106 |
|
| 107 |
public function getDefinition(): InputDefinition |
| 108 |
{ |
| 109 |
return $this->getCommand()->getDefinition(); |
| 110 |
} |
| 111 |
|
| 112 |
public function getNativeDefinition(): InputDefinition |
| 113 |
{ |
| 114 |
return $this->getCommand()->getNativeDefinition(); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion |
| 119 |
*/ |
| 120 |
public function addArgument(string $name, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static |
| 121 |
{ |
| 122 |
$this->getCommand()->addArgument($name, $mode, $description, $default, $suggestedValues); |
| 123 |
|
| 124 |
return $this; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion |
| 129 |
*/ |
| 130 |
public function addOption(string $name, string|array|null $shortcut = null, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static |
| 131 |
{ |
| 132 |
$this->getCommand()->addOption($name, $shortcut, $mode, $description, $default, $suggestedValues); |
| 133 |
|
| 134 |
return $this; |
| 135 |
} |
| 136 |
|
| 137 |
public function setProcessTitle(string $title): static |
| 138 |
{ |
| 139 |
$this->getCommand()->setProcessTitle($title); |
| 140 |
|
| 141 |
return $this; |
| 142 |
} |
| 143 |
|
| 144 |
public function setHelp(string $help): static |
| 145 |
{ |
| 146 |
$this->getCommand()->setHelp($help); |
| 147 |
|
| 148 |
return $this; |
| 149 |
} |
| 150 |
|
| 151 |
public function getHelp(): string |
| 152 |
{ |
| 153 |
return $this->getCommand()->getHelp(); |
| 154 |
} |
| 155 |
|
| 156 |
public function getProcessedHelp(): string |
| 157 |
{ |
| 158 |
return $this->getCommand()->getProcessedHelp(); |
| 159 |
} |
| 160 |
|
| 161 |
public function getSynopsis(bool $short = false): string |
| 162 |
{ |
| 163 |
return $this->getCommand()->getSynopsis($short); |
| 164 |
} |
| 165 |
|
| 166 |
public function addUsage(string $usage): static |
| 167 |
{ |
| 168 |
$this->getCommand()->addUsage($usage); |
| 169 |
|
| 170 |
return $this; |
| 171 |
} |
| 172 |
|
| 173 |
public function getUsages(): array |
| 174 |
{ |
| 175 |
return $this->getCommand()->getUsages(); |
| 176 |
} |
| 177 |
|
| 178 |
public function getHelper(string $name): HelperInterface |
| 179 |
{ |
| 180 |
return $this->getCommand()->getHelper($name); |
| 181 |
} |
| 182 |
|
| 183 |
public function getCommand(): parent |
| 184 |
{ |
| 185 |
if (!$this->command instanceof \Closure) { |
| 186 |
return $this->command; |
| 187 |
} |
| 188 |
|
| 189 |
$command = $this->command = ($this->command)(); |
| 190 |
$command->setApplication($this->getApplication()); |
| 191 |
|
| 192 |
if (null !== $this->getHelperSet()) { |
| 193 |
$command->setHelperSet($this->getHelperSet()); |
| 194 |
} |
| 195 |
|
| 196 |
$command->setName($this->getName()) |
| 197 |
->setAliases($this->getAliases()) |
| 198 |
->setHidden($this->isHidden()) |
| 199 |
->setDescription($this->getDescription()); |
| 200 |
|
| 201 |
// Will throw if the command is not correctly initialized. |
| 202 |
$command->getDefinition(); |
| 203 |
|
| 204 |
return $command; |
| 205 |
} |
| 206 |
} |