-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Add CommandDefinition to make list
lazy
#39860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Symfony/Component/Console/Command/CommandDescription.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Command; | ||
|
||
/** | ||
* Interface for command reacting to signal. | ||
* | ||
* @author Jérémy Derussé <[email protected]> | ||
*/ | ||
final class CommandDescription | ||
{ | ||
private $name; | ||
private $aliases; | ||
private $description; | ||
private $hidden; | ||
private $enabled; | ||
|
||
public function __construct(string $name, string $description = '', array $aliases = [], bool $hidden = false, bool $enabled = true) | ||
{ | ||
$this->name = $name; | ||
$this->aliases = $aliases; | ||
$this->description = $description; | ||
$this->hidden = $hidden; | ||
$this->enabled = $enabled; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getAliases(): array | ||
{ | ||
return $this->aliases; | ||
} | ||
|
||
public function getDescription(): string | ||
{ | ||
return $this->description; | ||
} | ||
|
||
public function isEnabled(): bool | ||
{ | ||
return $this->enabled; | ||
} | ||
|
||
public function isHidden(): bool | ||
{ | ||
return $this->hidden; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Symfony/Component/Console/Command/DescribableCommandInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Command; | ||
|
||
/** | ||
* Interface for command reacting to signal. | ||
* | ||
* @author Jérémy Derussé <[email protected]> | ||
*/ | ||
interface DescribableCommandInterface | ||
{ | ||
public static function describe(): CommandDescription; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Command; | ||
|
||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Helper\HelperSet; | ||
use Symfony\Component\Console\Input\InputDefinition; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @author Nicolas Grekas <[email protected]> | ||
*/ | ||
final class LazyCommand extends Command | ||
{ | ||
private $command; | ||
private $enabled; | ||
|
||
public function __construct(CommandDescription $description, \Closure $commandFactory) | ||
{ | ||
$this->setName($description->getName()) | ||
->setAliases($description->getAliases()) | ||
->setHidden($description->isHidden()) | ||
->setDescription($description->getDescription()); | ||
$this->enabled = $description->isEnabled(); | ||
|
||
$this->command = $commandFactory; | ||
} | ||
|
||
public function ignoreValidationErrors(): void | ||
{ | ||
$this->getCommand()->ignoreValidationErrors(); | ||
} | ||
|
||
public function setApplication(Application $application = null): void | ||
{ | ||
if ($this->command instanceof parent) { | ||
$this->command->setApplication($application); | ||
} | ||
|
||
parent::setApplication($application); | ||
} | ||
|
||
public function setHelperSet(HelperSet $helperSet): void | ||
{ | ||
if ($this->command instanceof parent) { | ||
$this->command->setHelperSet($helperSet); | ||
} | ||
|
||
parent::setHelperSet($helperSet); | ||
} | ||
|
||
public function isEnabled(): bool | ||
{ | ||
return $this->enabled; | ||
} | ||
|
||
public function run(InputInterface $input, OutputInterface $output): int | ||
{ | ||
return $this->getCommand()->run($input, $output); | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function setCode(callable $code): self | ||
{ | ||
$this->getCommand()->setCode($code); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function mergeApplicationDefinition(bool $mergeArgs = true): void | ||
{ | ||
$this->getCommand()->mergeApplicationDefinition($mergeArgs); | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function setDefinition($definition): self | ||
{ | ||
$this->getCommand()->setDefinition($definition); | ||
|
||
return $this; | ||
} | ||
|
||
public function getDefinition(): InputDefinition | ||
{ | ||
return $this->getCommand()->getDefinition(); | ||
} | ||
|
||
public function getNativeDefinition(): InputDefinition | ||
{ | ||
return $this->getCommand()->getNativeDefinition(); | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function addArgument(string $name, int $mode = null, string $description = '', $default = null): self | ||
{ | ||
$this->getCommand()->addArgument($name, $mode, $description, $default); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null): self | ||
{ | ||
$this->getCommand()->addOption($name, $shortcut, $mode, $description, $default); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function setProcessTitle(string $title): self | ||
{ | ||
$this->getCommand()->setProcessTitle($title); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function setHelp(string $help): self | ||
{ | ||
$this->getCommand()->setHelp($help); | ||
|
||
return $this; | ||
} | ||
|
||
public function getHelp(): string | ||
{ | ||
return $this->getCommand()->getHelp(); | ||
} | ||
|
||
public function getProcessedHelp(): string | ||
{ | ||
return $this->getCommand()->getProcessedHelp(); | ||
} | ||
|
||
public function getSynopsis(bool $short = false): string | ||
{ | ||
return $this->getCommand()->getSynopsis($short); | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function addUsage(string $usage): self | ||
{ | ||
$this->getCommand()->addUsage($usage); | ||
|
||
return $this; | ||
} | ||
|
||
public function getUsages(): array | ||
{ | ||
return $this->getCommand()->getUsages(); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getHelper(string $name) | ||
{ | ||
return $this->getCommand()->getHelper($name); | ||
} | ||
|
||
public function getCommand(): parent | ||
{ | ||
if (!$this->command instanceof \Closure) { | ||
return $this->command; | ||
} | ||
|
||
$command = $this->command = ($this->command)(); | ||
$command->setApplication($this->getApplication()); | ||
|
||
if (null !== $this->getHelperSet()) { | ||
$command->setHelperSet($this->getHelperSet()); | ||
} | ||
|
||
$command->setName($this->getName()) | ||
->setAliases($this->getAliases()) | ||
->setHidden($this->isHidden()) | ||
->setDescription($this->getDescription()); | ||
|
||
// Will throw if the command is not correctly initialized. | ||
$command->getDefinition(); | ||
|
||
return $command; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This break laziness (#39851 should also fix this issue).
I'm not sure why this check is performed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to #9244 apparently
Apparently, we need to think twice before removing as that might hurt DX.