Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\CommandDescription;
use Symfony\Component\Console\Command\DescribableCommandInterface;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -33,10 +35,8 @@
*
* @final
*/
class CacheClearCommand extends Command
class CacheClearCommand extends Command implements DescribableCommandInterface
{
protected static $defaultName = 'cache:clear';

private $cacheClearer;
private $filesystem;

Expand All @@ -48,6 +48,14 @@ public function __construct(CacheClearerInterface $cacheClearer, Filesystem $fil
$this->filesystem = $filesystem ?: new Filesystem();
}

public static function describe(): CommandDescription
{
return new CommandDescription(
'cache:clear',
'Clears the cache'
);
}

/**
* {@inheritdoc}
*/
Expand All @@ -58,7 +66,6 @@ protected function configure()
new InputOption('no-warmup', '', InputOption::VALUE_NONE, 'Do not warm up the cache'),
new InputOption('no-optional-warmers', '', InputOption::VALUE_NONE, 'Skip optional cache warmers (faster)'),
])
->setDescription('Clears the cache')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command clears the application cache for a given environment
and debug mode:
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\Command\LazyCommand;
use Symfony\Component\Console\Command\ListCommand;
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
Expand Down Expand Up @@ -489,8 +490,10 @@ public function add(Command $command)
return null;
}

// Will throw if the command is not correctly initialized.
$command->getDefinition();
Copy link
Member Author

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.

Copy link
Member

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.

if (!$command instanceof LazyCommand) {
// Will throw if the command is not correctly initialized.
$command->getDefinition();
}

if (!$command->getName()) {
throw new LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_debug_type($command)));
Expand Down
14 changes: 11 additions & 3 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Command
private $aliases = [];
private $definition;
private $hidden = false;
private $enabled = true;
private $help = '';
private $description = '';
private $fullDefinition;
Expand Down Expand Up @@ -73,8 +74,15 @@ public static function getDefaultName()
public function __construct(string $name = null)
{
$this->definition = new InputDefinition();

if (null !== $name || null !== $name = static::getDefaultName()) {
if ($this instanceof DescribableCommandInterface) {
$description = static::describe();
$this->setName($description->getName())
->setAliases($description->getAliases())
->setHidden($description->isHidden())
->setDescription($description->getDescription());

$this->enabled = $description->isEnabled();
} elseif (null !== $name || null !== $name = static::getDefaultName()) {
$this->setName($name);
}

Expand Down Expand Up @@ -138,7 +146,7 @@ public function getApplication()
*/
public function isEnabled()
{
return true;
return $this->enabled;
}

/**
Expand Down
60 changes: 60 additions & 0 deletions src/Symfony/Component/Console/Command/CommandDescription.php
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;
}
}
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;
}
211 changes: 211 additions & 0 deletions src/Symfony/Component/Console/Command/LazyCommand.php
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;
}
}
Loading