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
This commit is contained in:
commit
c833518795
|
@ -67,6 +67,7 @@ class Application
|
|||
private $helperSet;
|
||||
private $dispatcher;
|
||||
private $terminalDimensions;
|
||||
private $defaultCommand;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
|
@ -80,6 +81,7 @@ class Application
|
|||
{
|
||||
$this->name = $name;
|
||||
$this->version = $version;
|
||||
$this->defaultCommand = 'list';
|
||||
$this->helperSet = $this->getDefaultHelperSet();
|
||||
$this->definition = $this->getDefaultInputDefinition();
|
||||
|
||||
|
@ -179,8 +181,8 @@ class Application
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -1096,4 +1098,14 @@ class Application
|
|||
|
||||
return array_keys($alternatives);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default Command name.
|
||||
*
|
||||
* @param string $commandName The Command name
|
||||
*/
|
||||
public function setDefaultCommand($commandName)
|
||||
{
|
||||
$this->defaultCommand = $commandName;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -894,6 +894,28 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
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
|
||||
|
@ -918,3 +940,18 @@ class CustomApplication extends Application
|
|||
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());
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue