diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 620a43216a9e4..0641a10dff6ab 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -56,7 +56,7 @@ class Application private $autoExit; private $definition; private $helperSet; - + private $defaultCommandName = null; /** * Constructor. * @@ -80,6 +80,30 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') } } + /** + * Sets the name of the default command, this command will be executed if + * no command is provided in the CLI arguments + * @param String $command + */ + public function setDefaultCommandName($command) + { + if (!is_string($command)) { + throw new \UnexpectedValueException('Default Command Name must be a String'); + } + $this->defaultCommandName = $command; + } + + /** + * gets the name of the default command, this command will be executed if + * no command is provided in the CLI arguments + + * @return String + */ + public function getDefaultCommandName() + { + return $this->defaultCommandName ; + } + /** * Runs the current application. * @@ -96,6 +120,15 @@ public function run(InputInterface $input = null, OutputInterface $output = null { if (null === $input) { $input = new ArgvInput(); + // if we have a default command and we can not decide which command + // to execute we execute the default one + if (! is_null($this->getDefaultcommandName())) { + if (! $this->has($this->getCommandName($input))) { + $argv = $_SERVER['argv']; + array_splice($argv, 1, 0, $this->getDefaultcommandName()); + $input = new ArgvInput($argv); + } + } } if (null === $output) { diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index 5a7cc27945f3f..5389d2fba649c 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -426,6 +426,27 @@ public function testRun() $this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed'); } + /** + * Tests the run() with the default command set + */ + public function testRun_defaultCommand() + { + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $application->add($command = new \Foo1Command()); + $application->setDefaultCommandName('foo:bar1'); + $_SERVER['argv'] = array('cli.php'); + + ob_start(); + $application->run(); + ob_end_clean(); + + $this->assertSame('Symfony\Component\Console\Input\ArgvInput', get_class($command->input), '->run() creates an ArgvInput by default if none is given'); + $this->assertSame('Symfony\Component\Console\Output\ConsoleOutput', get_class($command->output), '->run() creates a ConsoleOutput by default if none is given'); + + } + /** * @expectedException \LogicException * @dataProvider getAddingAlreadySetDefinitionElementData @@ -454,4 +475,20 @@ public function getAddingAlreadySetDefinitionElementData() array(new InputOption('query', 'q', InputOption::VALUE_NONE)), ); } + + public function testSetGetDefaultCommandName() + { + $application = new Application(); + $application->setDefaultCommandName('foo'); + $this->assertEquals('foo', $application->getDefaultCommandName()); + } + + /** + * @expectedException \UnexpectedValueException + */ + public function testSetGetDefaultCommandName_InvalidParameter() + { + $application = new Application(); + $application->setDefaultCommandName(new \stdClass); + } }