Skip to content
Merged
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
44 changes: 35 additions & 9 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Application
private $dispatcher;
private $terminalDimensions;
private $defaultCommand;
private $initialized;

/**
* Constructor.
Expand All @@ -84,12 +85,6 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
$this->name = $name;
$this->version = $version;
$this->defaultCommand = 'list';
$this->helperSet = $this->getDefaultHelperSet();
$this->definition = $this->getDefaultInputDefinition();

foreach ($this->getDefaultCommands() as $command) {
$this->add($command);
}
}

public function setDispatcher(EventDispatcherInterface $dispatcher)
Expand Down Expand Up @@ -189,10 +184,11 @@ public function doRun(InputInterface $input, OutputInterface $output)

if (!$name) {
$name = $this->defaultCommand;
$this->definition->setArguments(array_merge(
$this->definition->getArguments(),
$definition = $this->getDefinition();
$definition->setArguments(array_merge(
$definition->getArguments(),
array(
'command' => new InputArgument('command', InputArgument::OPTIONAL, $this->definition->getArgument('command')->getDescription(), $name),
'command' => new InputArgument('command', InputArgument::OPTIONAL, $definition->getArgument('command')->getDescription(), $name),
)
));
}
Expand Down Expand Up @@ -225,6 +221,10 @@ public function setHelperSet(HelperSet $helperSet)
*/
public function getHelperSet()
{
if (!$this->helperSet) {
$this->helperSet = $this->getDefaultHelperSet();
}

return $this->helperSet;
}

Expand All @@ -245,6 +245,10 @@ public function setDefinition(InputDefinition $definition)
*/
public function getDefinition()
{
if (!$this->definition) {
$this->definition = $this->getDefaultInputDefinition();
}

return $this->definition;
}

Expand Down Expand Up @@ -374,6 +378,8 @@ public function addCommands(array $commands)
*/
public function add(Command $command)
{
$this->init();

$command->setApplication($this);

if (!$command->isEnabled()) {
Expand Down Expand Up @@ -406,6 +412,8 @@ public function add(Command $command)
*/
public function get($name)
{
$this->init();

if (!isset($this->commands[$name])) {
throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
}
Expand Down Expand Up @@ -433,6 +441,8 @@ public function get($name)
*/
public function has($name)
{
$this->init();

return isset($this->commands[$name]);
}

Expand Down Expand Up @@ -510,6 +520,8 @@ public function findNamespace($namespace)
*/
public function find($name)
{
$this->init();

$allCommands = array_keys($this->commands);
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
$commands = preg_grep('{^'.$expr.'}', $allCommands);
Expand Down Expand Up @@ -565,6 +577,8 @@ public function find($name)
*/
public function all($namespace = null)
{
$this->init();

if (null === $namespace) {
return $this->commands;
}
Expand Down Expand Up @@ -1151,4 +1165,16 @@ private function extractAllNamespaces($name)

return $namespaces;
}

private function init()
{
if ($this->initialized) {
return;
}
$this->initialized = true;

foreach ($this->getDefaultCommands() as $command) {
$this->add($command);
}
}
}