8000 feature #9776 [Console] Added the possibility to set a different defa… · symfony/symfony@c833518 · GitHub
[go: up one dir, main page]

Skip to content

Commit c833518

Browse files
committed
feature #9776 [Console] Added the possibility to set a different default command (danielcsgomes)
This PR was squashed before being merged into the 2.5-dev branch (closes #9776). Discussion ---------- [Console] Added the possibility to set a different default command I am not quite sure if this is the best approach to solve the issue but the solution I provide works. Let me know your suggestions to improve it. | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8058 | License | MIT | Doc PR | symfony/symfony-docs#3426 Commits ------- 418de05 [Console] Added the possibility to set a different default command
2 parents 79bea0a + 418de05 commit c833518

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
@@ -1096,4 +1098,14 @@ private function findAlternatives($name, $collection)
10961098

10971099
return array_keys($alternatives);
10981100
}
1101+
1102+
/**
1103+
* Sets the default Command name.
1104+
*
1105+
* @param string $commandName The Command name
1106+
*/
1107+
public function setDefaultCommand($commandName)
1108+
{
1109+
$this->defaultCommand = $commandName;
1110+
}
10991111
}

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
@@ -894,6 +894,28 @@ protected function getDispatcher()
894894

895895
return $dispatcher;
896896
}
897+
898+
public function testSetRunCustomDefaultCommand()
899+
{
900+
$command = new \FooCommand();
901+
902+
$application = new Application();
903+
$application->setAutoExit(false);
904+
$application->add($command);
905+
$application->setDefaultCommand($command->getName());
906+
907+
$tester = new ApplicationTester($application);
908+
$tester->run(array());
909+
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
910+
911+
$application = new CustomDefaultCommandApplication();
912+
$application->setAutoExit(false);
913+
914+
$tester = new ApplicationTester($application);
915+
$tester->run(array());
916+
917+
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
918+
}
897919
}
898920

899921
class CustomApplication extends Application
@@ -918,3 +940,18 @@ protected function getDefaultHelperSet()
918940
return new HelperSet(array(new FormatterHelper()));
919941
}
920942
}
943+
944+
class CustomDefaultCommandApplication extends Application
945+
{
946+
/**
947+
* Overwrites the constructor in order to set a different default command.
948+
*/
949+
public function __construct()
950+
{
951+
parent::__construct();
952+
953+
$command = new \FooCommand();
954+
$this->add($command);
955+
$this->setDefaultCommand($command->getName());
956+
}
957+
}

0 commit comments

Comments
 (0)
0