Permalink
Browse files

[Console] Added the possibility to set a different default command

  • Loading branch information...
1 parent f499094 commit 418de05b354d5a8580c27a494299928fd873cc81 @dcsg dcsg committed with fabpot Dec 14, 2013
View
16 src/Symfony/Component/Console/Application.php
@@ -67,6 +67,7 @@ class Application
private $helperSet;
private $dispatcher;
private $terminalDimensions;
+ private $defaultCommand;
/**
* Constructor.
@@ -80,6 +81,7 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
{
$this->name = $name;
$this->version = $version;
+ $this->defaultCommand = 'list';
$this->helperSet = $this->getDefaultHelperSet();
$this->definition = $this->getDefaultInputDefinition();
@@ -179,8 +181,8 @@ public function doRun(InputInterface $input, OutputInterface $output)
}
if (!$name) {
- $name = 'list';
- $input = new ArrayInput(array('command' => 'list'));
+ $name = $this->defaultCommand;
+ $input = new ArrayInput(array('command' => $this->defaultCommand));
}
// the command name MUST be the first element of the input
@@ -1086,4 +1088,14 @@ private function findAlternatives($name, $collection)
return array_keys($alternatives);
}
+
+ /**
+ * Sets the default Command name.
+ *
+ * @param string $commandName The Command name
+ */
+ public function setDefaultCommand($commandName)
+ {
+ $this->defaultCommand = $commandName;
+ }
}
View
1 src/Symfony/Component/Console/CHANGELOG.md
@@ -4,6 +4,7 @@ CHANGELOG
2.5.0
-----
+* added a way to set a default command instead of `ListCommand`
* added a way to set the process name of a command
2.4.0
View
37 src/Symfony/Component/Console/Tests/ApplicationTest.php
@@ -881,6 +881,28 @@ protected function getDispatcher()
return $dispatcher;
}
+
+ public function testSetRunCustomDefaultCommand()
+ {
+ $command = new \FooCommand();
+
+ $application = new Application();
+ $application->setAutoExit(false);
+ $application->add($command);
+ $application->setDefaultCommand($command->getName());
+
+ $tester = new ApplicationTester($application);
+ $tester->run(array());
+ $this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
+
+ $application = new CustomDefaultCommandApplication();
+ $application->setAutoExit(false);
+
+ $tester = new ApplicationTester($application);
+ $tester->run(array());
+
+ $this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
+ }
}
class CustomApplication extends Application
@@ -905,3 +927,18 @@ protected function getDefaultHelperSet()
return new HelperSet(array(new FormatterHelper()));
}
}
+
+class CustomDefaultCommandApplication extends Application
+{
+ /**
+ * Overwrites the constructor in order to set a different default command.
+ */
+ public function __construct()
+ {
+ parent::__construct();
+
+ $command = new \FooCommand();
+ $this->add($command);
+ $this->setDefaultCommand($command->getName());
+ }
+}

0 comments on commit 418de05

Please sign in to comment.