Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] application/command as text/xml/whatever decoupling #7454

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 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
da67c12
[Console] added descriptor & refactored description commands
jfsimon Mar 22, 2013
f239b1c
[Console] added JSON descriptors
jfsimon Apr 1, 2013
bebf1eb
[Console] added markdown descriptors
jfsimon Apr 3, 2013
43b5e5c
[Console] added forgotten headers
jfsimon Apr 3, 2013
acc7414
[Console] removed unused methods (tests broken)
jfsimon Apr 3, 2013
774794c
[Console] fixed tests
jfsimon Apr 3, 2013
2066a9b
[Console] added XML descriptors
jfsimon Apr 6, 2013
47fa194
[Console] started text derciptors
jfsimon Apr 6, 2013
8b56eb1
[Console] finished text descriptors
jfsimon Apr 8, 2013
27aa872
[Console] repaired tests
jfsimon Apr 10, 2013
30d8807
[Console] re-introduced asText & asXml methods to avoid BC break
jfsimon Apr 14, 2013
9964838
[Console] added command definition merge be fore description
jfsimon Apr 14, 2013
ef6d8ba
[Console] simplified description commands
jfsimon Apr 14, 2013
389101a
[Console] re-introduced --xml option to avoid BC break
jfsimon Apr 14, 2013
84be8de
[Console] simplified mardown descriptors
jfsimon Apr 14, 2013
ce5c0fd
[Console] applied comments from github
jfsimon Apr 14, 2013
49a4612
[Console] applied comments from github
jfsimon Apr 14, 2013
20c10a5
[Console] added provider injection to descriptors
jfsimon Apr 14, 2013
9c7b358
[Console] simplified descriptors
jfsimon Apr 23, 2013
d3ec073
[Console] applied advices from github comments
jfsimon Apr 23, 2013
d70e086
[Console] removed proxy
jfsimon Apr 23, 2013
ce60fb7
[Console] simplified descriptor interface
jfsimon Apr 23, 2013
28f082e
[Console] removed changes
jfsimon Apr 23, 2013
cbb8105
[Console] moved commands arguments
jfsimon Apr 23, 2013
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
Prev Previous commit
Next Next commit
[Console] fixed tests
  • Loading branch information
jfsimon committed Apr 12, 2013
commit 774794c6ae08a86ef6af19bb35220d5245517349
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
abstract class AbstractDescriptorCommand extends Command
{
/**
* @var array
* @var DescriptorProvider
*/
private $supportedFormats;
private $provider;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->provider = new DescriptorProvider();
$this->setDefinition($this->createDefinition());
}

Expand All @@ -37,7 +38,7 @@ protected function configure()
protected function createDefinition()
{
return new InputDefinition(array(
new InputOption('format', null, InputOption::VALUE_OPTIONAL, 'Output format ('.implode(', ', $this->supportedFormats).')'),
new InputOption('format', null, InputOption::VALUE_OPTIONAL, 'Output format ('.implode(', ', $this->provider->getSupportedFormats()).')'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
));
}
Expand All @@ -47,9 +48,6 @@ protected function createDefinition()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$descriptorProvider = new DescriptorProvider();
$this->supportedFormats = $descriptorProvider->getSupportedFormats();
$this->setDefinition($this->createDefinition());
$this->getHelperSet()->set(new DescriptorHelper($descriptorProvider));
$this->getHelperSet()->set(new DescriptorHelper($this->provider));
}
}
4 changes: 1 addition & 3 deletions src/Symfony/Component/Console/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class HelpCommand extends AbstractDescriptorCommand
protected function configure()
{
parent::configure();
Copy link
Member

Choose a reason for hiding this comment

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

this does not seem needed


$this->ignoreValidationErrors();

$this
Expand Down Expand Up @@ -68,7 +67,6 @@ public function setCommand(Command $command)
protected function createDefinition()
{
$definition = parent::createDefinition();

$definition->addArgument(new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'));
Copy link
Member

Choose a reason for hiding this comment

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

Why not simply calling $this->addArgument in the configure method ? It would be much simpler than introducing this createDefinition method


return $definition;
Expand All @@ -85,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->command = $this->getApplication()->find($input->getArgument('command_name'));
}

Copy link
Member

Choose a reason for hiding this comment

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

--xml is still supported, so we must convert it to the right format too.

$this->getHelper('descriptor')->describe($output, $this->command, $input->getArgument('format'), $input->getOption('raw'));
$this->getHelper('descriptor')->describe($output, $this->command, $input->getOption('format'), $input->getOption('raw'));

$this->command = null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$this->getHelper('descriptor')->describe($output, $this->getApplication(), $input->getArgument('format'), $input->getOption('raw'));
$this->getHelper('descriptor')->describe($output, $this->getApplication(), $input->getOption('format'), $input->getOption('raw'));
}
}
13 changes: 11 additions & 2 deletions src/Symfony/Component/Console/Descriptor/DescriptorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class DescriptorProvider
*/
private $options = array(
'default_format' => 'txt',
'json_encoding' => 0,
'namespace' => null,
'json_encoding' => 0,
'markdown_width' => 120,
);

/**
Expand All @@ -46,6 +47,11 @@ public function __construct(array $options = array())
->add(new Json\InputDefinitionJsonDescriptor())
->add(new Json\InputArgumentJsonDescriptor())
->add(new Json\InputOptionJsonDescriptor())
->add(new Markdown\ApplicationMarkdownDescriptor())
->add(new Markdown\CommandMarkdownDescriptor())
->add(new Markdown\InputDefinitionMarkdownDescriptor())
->add(new Markdown\InputArgumentMarkdownDescriptor())
->add(new Markdown\InputOptionMarkdownDescriptor())
;
}

Expand Down Expand Up @@ -125,6 +131,9 @@ public function getSupportedFormats()
$supportedFormats[] = $descriptor->getFormat();
}

return array_unique($supportedFormats);
$supportedFormats = array_unique($supportedFormats);
sort($supportedFormats);

return $supportedFormats;
}
}
16 changes: 6 additions & 10 deletions src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public function testExecuteForCommandAlias()
$command->setApplication(new Application());
$commandTester = new CommandTester($command);
$commandTester->execute(array('command_name' => 'li'));

$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias');
$this->assertRegExp('/list \[--format="\.\.\."\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias');
}

public function testExecuteForCommand()
Expand All @@ -34,17 +33,15 @@ public function testExecuteForCommand()
$commandTester = new CommandTester($command);
$command->setCommand(new ListCommand());
$commandTester->execute(array());

$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
$this->assertRegExp('/list \[--format="\.\.\."\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
}

public function testExecuteForCommandWithXmlOption()
{
$command = new HelpCommand();
$commandTester = new CommandTester($command);
$command->setCommand(new ListCommand());
$commandTester->execute(array('--xml' => true));

$commandTester->execute(array('--format' => 'xml'));
$this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
}

Expand All @@ -53,16 +50,15 @@ public function testExecuteForApplicationCommand()
$application = new Application();
$commandTester = new CommandTester($application->get('help'));
$commandTester->execute(array('command_name' => 'list'));

$this->assertRegExp('/list \[--xml\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
$this->assertRegExp('/list \[--format="\.\.\."\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
}

public function testExecuteForApplicationCommandWithXmlOption()
{
$application = new Application();
$commandTester = new CommandTester($application->get('help'));
$commandTester->execute(array('command_name' => 'list', '--xml' => true));

$commandTester->execute(array('command_name' => 'list', 'format' => 'xml'));
$this->assertRegExp('/list \[--format="\.\.\."\] \[--raw\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
$this->assertRegExp('/<command/', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function testExecuteListsCommandsWithXmlOption()
{
$application = new Application();
$commandTester = new CommandTester($command = $application->get('list'));
$commandTester->execute(array('command' => $command->getName(), '--xml' => true));

$commandTester->execute(array('command' => $command->getName(), '--format' => 'xml'));
$this->assertRegExp('/<command id="list" name="list">/', $commandTester->getDisplay(), '->execute() returns a list of available commands in XML if --xml is passed');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"commands":[{"name":"help","usage":"help","description":"Displays help for a command","help":"The <info>help<\/info> command displays help for a given command:\n\n <info>php \/usr\/bin\/phpunit help list<\/info>\n\nYou can also output the help as XML by using the <comment>--xml<\/comment> option:\n\n <info>php \/usr\/bin\/phpunit help --xml list<\/info>\n\nTo display the list of available commands, please use the <info>list<\/info> command.","aliases":[],"definition":{"arguments":[],"options":[]}},{"name":"list","usage":"list","description":"Lists commands","help":"The <info>list<\/info> command lists all commands:\n\n <info>php \/usr\/bin\/phpunit list<\/info>\n\nYou can also display the commands for a specific namespace:\n\n <info>php \/usr\/bin\/phpunit list test<\/info>\n\nYou can also output the information as XML by using the <comment>--xml<\/comment> option:\n\n <info>php \/usr\/bin\/phpunit list --xml<\/info>\n\nIt's also possible to get raw list of commands (useful for embedding command runner):\n\n <info>php \/usr\/bin\/phpunit list --raw<\/info>","aliases":[],"definition":{"arguments":[],"options":[]}}],"namespaces":[{"id":"_global","commands":["help","list"]}]}
{"commands":[{"name":"help","usage":"help [--format[=\"...\"]] [--raw] [command_name]","description":"Displays help for a command","help":"The <info>help<\/info> command displays help for a given command:\n\n <info>php \/usr\/bin\/phpunit help list<\/info>\n\nYou can also output the help as XML by using the <comment>--xml<\/comment> option:\n\n <info>php \/usr\/bin\/phpunit help --xml list<\/info>\n\nTo display the list of available commands, please use the <info>list<\/info> command.","aliases":[],"definition":{"arguments":{"command_name":{"name":"command_name","is_required":false,"is_array":false,"description":"The command name","default":"help"}},"options":{"format":{"name":"--format","shortcut":"","accept_value":true,"is_value_required":false,"is_multiple":false,"description":"Output format (json, md)","default":null},"raw":{"name":"--raw","shortcut":"","accept_value":false,"is_value_required":false,"is_multiple":false,"description":"To output raw command list","default":false}}}},{"name":"list","usage":"list [--format[=\"...\"]] [--raw] [namespace]","description":"Lists commands","help":"The <info>list<\/info> command lists all commands:\n\n <info>php \/usr\/bin\/phpunit list<\/info>\n\nYou can also display the commands for a specific namespace:\n\n <info>php \/usr\/bin\/phpunit list test<\/info>\n\nYou can also output the information as XML by using the <comment>--xml<\/comment> option:\n\n <info>php \/usr\/bin\/phpunit list --xml<\/info>\n\nIt's also possible to get raw list of commands (useful for embedding command runner):\n\n <info>php \/usr\/bin\/phpunit list --raw<\/info>","aliases":[],"definition":{"arguments":{"namespace":{"name":"namespace","is_required":false,"is_array":false,"description":"The namespace name","default":null}},"options":{"format":{"name":"--format","shortcut":"","accept_value":true,"is_value_required":false,"is_multiple":false,"description":"Output format (json, md)","default":null},"raw":{"name":"--raw","shortcut":"","accept_value":false,"is_value_required":false,"is_multiple":false,"description":"To output raw command list","default":false}}}}],"namespaces":[{"id":"_global","commands":["help","list"]}]}
68 changes: 66 additions & 2 deletions src/Symfony/Component/Console/Tests/Fixtures/application_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ help
----

* Description: Displays help for a command
* Usage: `help`
* Usage: `help [--format[="..."]] [--raw] [command_name]`
* Aliases: <none>

The <info>help</info> command displays help for a given command:
Expand All @@ -21,11 +21,43 @@ You can also output the help as XML by using the <comment>--xml</comment> option

To display the list of available commands, please use the <info>list</info> command.

### Arguments:

**command_name:**

* Name: command_name
* Is required: no
* Is array: no
* Description: The command name
* Default: `'help'`

### Options:

**format:**

* Name: `--format`
* Shortcut: <none>
* Accept value: yes
* Is value required: no
* Is multiple: no
* Description: Output format (json, md)
* Default: `NULL`

**raw:**

* Name: `--raw`
* Shortcut: <none>
* Accept value: no
* Is value required: no
* Is multiple: no
* Description: To output raw command list
* Default: `false`

list
----

* Description: Lists commands
* Usage: `list`
* Usage: `list [--format[="..."]] [--raw] [namespace]`
* Aliases: <none>

The <info>list</info> command lists all commands:
Expand All @@ -43,3 +75,35 @@ You can also output the information as XML by using the <comment>--xml</comment>
It's also possible to get raw list of commands (useful for embedding command runner):

<info>php /usr/bin/phpunit list --raw</info>

### Arguments:

**namespace:**

* Name: namespace
* Is required: no
* Is array: no
* Description: The namespace name
* Default: `NULL`

### Options:

**format:**

* Name: `--format`
* Shortcut: <none>
* Accept value: yes
* Is value required: no
* Is multiple: no
* Description: Output format (json, md)
* Default: `NULL`

**raw:**

* Name: `--raw`
* Shortcut: <none>
* Accept value: no
* Is value required: no
* Is multiple: no
* Description: To output raw command list
* Default: `false`
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"commands":[{"name":"help","usage":"help","description":"Displays help for a command","help":"The <info>help<\/info> command displays help for a given command:\n\n <info>php \/usr\/bin\/phpunit help list<\/info>\n\nYou can also output the help as XML by using the <comment>--xml<\/comment> option:\n\n <info>php \/usr\/bin\/phpunit help --xml list<\/info>\n\nTo display the list of available commands, please use the <info>list<\/info> command.","aliases":[],"definition":{"arguments":[],"options":[]}},{"name":"list","usage":"list","description":"Lists commands","help":"The <info>list<\/info> command lists all commands:\n\n <info>php \/usr\/bin\/phpunit list<\/info>\n\nYou can also display the commands for a specific namespace:\n\n <info>php \/usr\/bin\/phpunit list test<\/info>\n\nYou can also output the information as XML by using the <comment>--xml<\/comment> option:\n\n <info>php \/usr\/bin\/phpunit list --xml<\/info>\n\nIt's also possible to get raw list of commands (useful for embedding command runner):\n\n <info>php \/usr\/bin\/phpunit list --raw<\/info>","aliases":[],"definition":{"arguments":[],"options":[]}},{"name":"descriptor:command1","usage":"descriptor:command1","description":"command 1 description","help":"command 1 help","aliases":["alias1","alias2"],"definition":{"arguments":[],"options":[]}},{"name":"descriptor:command2","usage":"descriptor:command2 [-o|--option_name] argument_name","description":"command 2 description","help":"command 2 help","aliases":[],"definition":{"arguments":{"argument_name":{"name":"argument_name","is_required":true,"is_array":false,"description":"","default":null}},"options":{"option_name":{"name":"--option_name","shortcut":"-o","accept_value":false,"is_value_required":false,"is_multiple":false,"description":"","default":false}}}}],"namespaces":[{"id":"_global","commands":["alias1","alias2","help","list"]},{"id":"descriptor","commands":["descriptor:command1","descriptor:command2"]}]}
{"commands":[{"name":"help","usage":"help [--format[=\"...\"]] [--raw] [command_name]","description":"Displays help for a command","help":"The <info>help<\/info> command displays help for a given command:\n\n <info>php \/usr\/bin\/phpunit help list<\/info>\n\nYou can also output the help as XML by using the <comment>--xml<\/comment> option:\n\n <info>php \/usr\/bin\/phpunit help --xml list<\/info>\n\nTo display the list of available commands, please use the <info>list<\/info> command.","aliases":[],"definition":{"arguments":{"command_name":{"name":"command_name","is_required":false,"is_array":false,"description":"The command name","default":"help"}},"options":{"format":{"name":"--format","shortcut":"","accept_value":true,"is_value_required":false,"is_multiple":false,"description":"Output format (json, md)","default":null},"raw":{"name":"--raw","shortcut":"","accept_value":false,"is_value_required":false,"is_multiple":false,"description":"To output raw command list","default":false}}}},{"name":"list","usage":"list [--format[=\"...\"]] [--raw] [namespace]","description":"Lists commands","help":"The <info>list<\/info> command lists all commands:\n\n <info>php \/usr\/bin\/phpunit list<\/info>\n\nYou can also display the commands for a specific namespace:\n\n <info>php \/usr\/bin\/phpunit list test<\/info>\n\nYou can also output the information as XML by using the <comment>--xml<\/comment> option:\n\n <info>php \/usr\/bin\/phpunit list --xml<\/info>\n\nIt's also possible to get raw list of commands (useful for embedding command runner):\n\n <info>php \/usr\/bin\/phpunit list --raw<\/info>","aliases":[],"definition":{"arguments":{"namespace":{"name":"namespace","is_required":false,"is_array":false,"description":"The namespace name","default":null}},"options":{"format":{"name":"--format","shortcut":"","accept_value":true,"is_value_required":false,"is_multiple":false,"description":"Output format (json, md)","default":null},"raw":{"name":"--raw","shortcut":"","accept_value":false,"is_value_required":false,"is_multiple":false,"description":"To output raw command list","default":false}}}},{"name":"descriptor:command1","usage":"descriptor:command1","description":"command 1 description","help":"command 1 help","aliases":["alias1","alias2"],"definition":{"arguments":[],"options":[]}},{"name":"descriptor:command2","usage":"descriptor:command2 [-o|--option_name] argument_name","description":"command 2 description","help":"command 2 help","aliases":[],"definition":{"arguments":{"argument_name":{"name":"argument_name","is_required":true,"is_array":false,"description":"","default":null}},"options":{"option_name":{"name":"--option_name","shortcut":"-o","accept_value":false,"is_value_required":false,"is_multiple":false,"description":"","default":false}}}}],"namespaces":[{"id":"_global","commands":["alias1","alias2","help","list"]},{"id":"descriptor","commands":["descriptor:command1","descriptor:command2"]}]}
68 changes: 66 additions & 2 deletions src/Symfony/Component/Console/Tests/Fixtures/application_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ help
----

* Description: Displays help for a command
* Usage: `help`
* Usage: `help [--format[="..."]] [--raw] [command_name]`
* Aliases: <none>

The <info>help</info> command displays help for a given command:
Expand All @@ -28,11 +28,43 @@ You can also output the help as XML by using the <comment>--xml</comment> option

To display the list of available commands, please use the <info>list</info> command.

### Arguments:

**command_name:**

* Name: command_name
* Is required: no
* Is array: no
* Description: The command name
* Default: `'help'`

### Options:

**format:**

* Name: `--format`
* Shortcut: <none>
* Accept value: yes
* Is value required: no
* Is multiple: no
* Description: Output format (json, md)
* Default: `NULL`

**raw:**

* Name: `--raw`
* Shortcut: <none>
* Accept value: no
* Is value required: no
* Is multiple: no
* Description: To output raw command list
* Default: `false`

list
----

* Description: Lists commands
* Usage: `list`
* Usage: `list [--format[="..."]] [--raw] [namespace]`
* Aliases: <none>

The <info>list</info> command lists all commands:
Expand All @@ -51,6 +83,38 @@ It's also possible to get raw list of commands (useful for embedding command run

<info>php /usr/bin/phpunit list --raw</info>

### Arguments:

**namespace:**

* Name: namespace
* Is required: no
* Is array: no
* Description: The namespace name
* Default: `NULL`

### Options:

**format:**

* Name: `--format`
* Shortcut: <none>
* Accept value: yes
* Is value required: no
* Is multiple: no
* Description: Output format (json, md)
* Default: `NULL`

**raw:**

* Name: `--raw`
* Shortcut: <none>
* Accept value: no
* Is value required: no
* Is multiple: no
* Description: To output raw command list
* Default: `false`

descriptor:command1
-------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@



list [--xml] [--raw] [namespace]
list [--format[="..."]] [--raw] [namespace]