Skip to content

[Console] Added support for lazy-loaded commands #13946

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
41 changes: 37 additions & 4 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Console;

use Symfony\Component\Console\Command\CommandConfiguration;
use Symfony\Component\Console\Descriptor\TextDescriptor;
use Symfony\Component\Console\Descriptor\XmlDescriptor;
use Symfony\Component\Console\Helper\DebugFormatterHelper;
Expand Down Expand Up @@ -59,6 +60,9 @@
*/
class Application
{
/**
* @var CommandConfiguration[]
*/
private $commands = array();
private $wantHelps = false;
private $runningCommand;
Expand Down Expand Up @@ -404,6 +408,32 @@ public function add(Command $command)
return $command;
}

/**
* Adds a command object.
*
* If a command with the same name already exists, it will be overridden.
*
* @param CommandConfiguration $commandConfiguration A command configuration
*
* @return CommandConfiguration The command configuration
*
* @api
*/
public function addCommandConfiguration(CommandConfiguration $commandConfiguration)
{
if (!$commandConfiguration->isEnabled()) {
return $commandConfiguration;
}

$this->commands[$commandConfiguration->getName()] = $commandConfiguration;

foreach ($commandConfiguration->getAliases() as $alias) {
$this->commands[$alias] = $commandConfiguration;
}

return $commandConfiguration;
}

/**
* Returns a registered command by name or alias.
*
Expand All @@ -422,12 +452,13 @@ public function get($name)
}

$command = $this->commands[$name];
$command = $command->getCommand();

if ($this->wantHelps) {
$this->wantHelps = false;

$helpCommand = $this->get('help');
$helpCommand->setCommand($command);
$helpCommand->setTargetCommand($command);

return $helpCommand;
}
Expand Down Expand Up @@ -583,13 +614,15 @@ public function find($name)
public function all($namespace = null)
{
if (null === $namespace) {
return $this->commands;
return array_map(function (CommandConfiguration $commandConfiguration) {
return $commandConfiguration->getCommand();
}, $this->commands);
}

$commands = array();
foreach ($this->commands as $name => $command) {
foreach ($this->commands as $name => $commandConfiguration) {
if ($namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1)) {
$commands[$name] = $command;
$commands[$name] = $commandConfiguration->getCommand();
}
}

Expand Down
Loading