8000 [Console] Added the possibility to set a different default command · symfony/symfony@418de05 · GitHub
[go: up one dir, main page]

Skip to content

Commit 418de05

Browse files
dcsgfabpot
authored andcommitted
[Console] Added the possibility to set a different default command
1 parent f499094 commit 418de05

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Application
6767
private $helperSet;
6868
private $dispatcher;
6969
private $terminalDimensions;
70+
private $defaultCommand;
7071

7172
/**
7273
* Constructor.
@@ -80,6 +81,7 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
8081
{
8182
$this->name = $name;
8283
$this->version = $version;
84+
$this->defaultCommand = 'list';
8385
$this->helperSet = $this->getDefaultHelperSet();
8486
$this->definition = $this->getDefaultInputDefinition();
8587

@@ -179,8 +181,8 @@ public function doRun(InputInterface $input, OutputInterface $output)
179181
}
180182

181183
if (!$name) {
182-
$name = 'list';
183-
$input = new ArrayInput(array('command' => 'list'));
184+
$name = $this->defaultCommand;
185+
$input = new ArrayInput(array('command' => $this->defaultCommand));
184186
}
185187

186188
// the command name MUST be the first element of the input
@@ -1086,4 +1088,14 @@ private function findAlternatives($name, $collection)
10861088

10871089
return array_keys($alternatives);
10881090
}
1091+
1092+
/**
1093+
* Sets the default Command name.
1094+
*
1095+
* @param string $commandName The Command name
1096+
*/
1097+
public function setDefaultCommand($commandName)
1098+
{
1099+
$this->defaultCommand = $commandName;
1100+
}
10891101
}

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
2.5.0
55
-----
66

7+
* added a way to set a default command instead of `ListCommand`
78
* added a way to set the process name of a command
89

910
2.4.0

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,28 @@ protected function getDispatcher()
881881

882882
return $dispatcher;
883883
}
884+
885+
public function testSetRunCustomDefaultCommand()
886+
{
887+
$command = new \FooCommand();
888+
889+
$application = new Application();
890+
$application->setAutoExit(false);
891+
$application->add($command);
892+
$application->setDefaultCommand($command->getName());
893+
894+
$tester = new ApplicationTester($application);
895+
$tester->run(array());
896+
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
897+
898+
$application = new CustomDefaultCommandApplication();
899+
$application->setAutoExit(false);
900+
901+
$tester = new ApplicationTester($application);
902+
$tester->run(array());
903+
904+
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
905+
}
884906
}
885907

886908
class CustomApplication extends Application
@@ -905,3 +927,18 @@ protected function getDefaultHelperSet()
905927
return new HelperSet(array(new FormatterHelper()));
906928
}
907929
}
930+
931+
class CustomDefaultCommandApplication extends Application
932+
{
933+
/**
934+
* Overwrites the constructor in order to set a different default command.
935+
*/
936+
public function __construct()
937+
{
938+
parent::__construct();
939+
940+
$command = new \FooCommand();
941+
$this->add($command);
942+
$this->setDefaultCommand($command->getName());
943+
}
944+
}

0 commit comments

Comments
 (0)
0