Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Application
private $helperSet;
private $dispatcher;
private $terminalDimensions;
private $defaultCommand;

/**
* Constructor.
Expand All @@ -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();

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method does two things:

  • sets a default command
  • adds a command

I'd make it only does what the method name says (set a default command) and make it accept a command name, not a whole command.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakzal I hadn't thought that way, but I do think it will work fine the way you say and in a more simple way. Will test it and if works well will do the changes

}
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}