Skip to content

Commit bdc93d2

Browse files
committed
[Console] Support for lazy-loaded commands
Refactored the commands to extract the configuration into a separate class. That allows to define the configuration of a command without instantiating the command. The command is then loaded only when being used.
1 parent 17ad6fd commit bdc93d2

File tree

8 files changed

+723
-370
lines changed

8 files changed

+723
-370
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console;
1313

14+
use Symfony\Component\Console\Command\CommandConfiguration;
1415
use Symfony\Component\Console\Descriptor\TextDescriptor;
1516
use Symfony\Component\Console\Descriptor\XmlDescriptor;
1617
use Symfony\Component\Console\Helper\DebugFormatterHelper;
@@ -59,6 +60,9 @@
5960
*/
6061
class Application
6162
{
63+
/**
64+
* @var CommandConfiguration[]
65+
*/
6266
private $commands = array();
6367
private $wantHelps = false;
6468
private $runningCommand;
@@ -404,6 +408,32 @@ public function add(Command $command)
404408
return $command;
405409
}
406410

411+
/**
412+
* Adds a command object.
413+
*
414+
* If a command with the same name already exists, it will be overridden.
415+
*
416+
* @param CommandConfiguration $commandConfiguration A command configuration
417+
*
418+
* @return CommandConfiguration The command configuration
419+
*
420+
* @api
421+
*/
422+
public function addCommandConfiguration(CommandConfiguration $commandConfiguration)
423+
{
424+
if (!$commandConfiguration->isEnabled()) {
425+
return $commandConfiguration;
426+
}
427+
428+
$this->commands[$commandConfiguration->getName()] = $commandConfiguration;
429+
430+
foreach ($commandConfiguration->getAliases() as $alias) {
431+
$this->commands[$alias] = $commandConfiguration;
432+
}
433+
434+
return $commandConfiguration;
435+
}
436+
407437
/**
408438
* Returns a registered command by name or alias.
409439
*
@@ -422,12 +452,13 @@ public function get($name)
422452
}
423453

424454
$command = $this->commands[$name];
455+
$command = $command->getCommand();
425456

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

429460
$helpCommand = $this->get('help');
430-
$helpCommand->setCommand($command);
461+
$helpCommand->setTargetCommand($command);
431462

432463
return $helpCommand;
433464
}
@@ -583,13 +614,15 @@ public function find($name)
583614
public function all($namespace = null)
584615
{
585616
if (null === $namespace) {
586-
return $this->commands;
617+
return array_map(function (CommandConfiguration $commandConfiguration) {
618+
return $commandConfiguration->getCommand();
619+
}, $this->commands);
587620
}
588621

589622
$commands = array();
590-
foreach ($this->commands as $name => $command) {
623+
foreach ($this->commands as $name => $commandConfiguration) {
591624
if ($namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1)) {
592-
$commands[$name] = $command;
625+
$commands[$name] = $commandConfiguration->getCommand();
593626
}
594627
}
595628

0 commit comments

Comments
 (0)