8000 [Console] application/command as text/xml/whatever decoupling by jfsimon · Pull Request #7454 · symfony/symfony · GitHub
[go: up one dir, main page]

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] repaired tests
  • Loading branch information
jfsimon committed Apr 12, 2013
commit 27aa872544b8ef0161dba00c9214cc40caff4a92
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ protected function configure()
protected function createDefinition()
{
return new InputDefinition(array(
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output help in other formats'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output help in other formats.'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list.'),
));
}

Expand Down
85 changes: 85 additions & 0 deletions src/Symfony/Component/Console/Descriptor/CommandDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Descriptor;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputDefinition;

/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
class CommandDescription
{
/**
* @var Command
*/
private $command;

/**
* @param Command $command
*/
public function __construct(Command $command)
{
$this->command = $command;
}

/**
* @return string
*/
public function getName()
{
return $this->command->getName();
}

/**
* @return string
*/
public function getDescription()
{
return $this->command->getDescription();
}

/**
* @return array
*/
public function getAliases()
{
return $this->command->getAliases();
}

/**
* @return string
*/
public function getSynopsis()
{
return $this->command->getSynopsis();
}

/**
* @return string
*/
public function getHelp()
{
return $this->command->getProcessedHelp();
}

/**
* @return InputDefinition
*/
public function getDefinition()
{
$method = new \ReflectionMethod($this->command, 'getNativeDefinition');
$method->setAccessible(true);
Copy link
Member

Choose a reason for hiding this comment

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

Relying on reflection to get the definition looks like a hack


return $method->invoke($this->command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Descriptor\Json;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Descriptor\CommandDescription;

/**
* @author Jean-François Simon <contact@jfsimon.fr>
Expand All @@ -24,15 +25,16 @@ class CommandJsonDescriptor extends AbstractJsonDescriptor
public function getData($object)
{
$definitionDescriptor = new InputDefinitionJsonDescriptor();
Copy link
Member

Choose a reason for hiding this comment

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

creating a new descriptor inside the descriptor looks weird to me


/** @var Command $object */
$description = new CommandDescription($object);

return array(
'name' => $object->getName(),
'usage' => $object->getSynopsis(),
'description' => $object->getDescription(),
'help' => $object->getProcessedHelp(),
'aliases' => $object->getAliases(),
'definition' => $definitionDescriptor->getData($object->getDefinition()),
'name' => $description->getName(),
'usage' => $description->getSynopsis(),
'description' => $description->getDescription(),
'help' => $description->getHelp(),
'aliases' => $description->getAliases(),
'definition' => $definitionDescriptor->getData($description->getDefinition()),
);
}

Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Descriptor\Markdown;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Descriptor\CommandDescription;

/**
* @author Jean-François Simon <contact@jfsimon.fr>
Expand All @@ -23,20 +24,19 @@ class CommandMarkdownDescriptor extends AbstractMarkdownDescriptor
*/
public function getDocument($object)
{
/** @var Command $object */
$description = new CommandDescription($object);
$definitionDescriptor = new InputDefinitionMarkdownDescriptor();

$object->getProcessedHelp();

/** @var Command $object */
return new Document\Document(array(
new Document\Title($object->getName(), 2),
new Document\Title($description->getName(), 2),
new Document\UnorderedList(array(
'Description: '.($object->getDescription() ?: '<none>'),
'Usage: `'.$object->getSynopsis().'`',
'Aliases: '.(count($object->getAliases()) ? '`'.implode('`, `', $object->getAliases()).'`' : '<none>'),
'Description: '.($description->getDescription() ?: '<none>'),
'Usage: `'.$description->getSynopsis().'`',
'Aliases: '.(count($description->getAliases()) ? '`'.implode('`, `', $description->getAliases()).'`' : '<none>'),
)),
new Document\Paragraph($object->getProcessedHelp()),
$definitionDescriptor->getDocument($object->getDefinition()),
new Document\Paragraph($description->getHelp()),
$definitionDescriptor->getDocument($description->getDefinition()),
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function getFormat()
*/
public function useFormatting()
{
return false;
return true;
}

protected function formatDefaultValue($default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Descriptor\Text;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Descriptor\CommandDescription;
use Symfony\Component\Console\Input\InputDefinition;

/**
Expand All @@ -33,16 +34,17 @@ public function getRawText($object)
public function getFormattedText($object)
{
/** @var Command $object */
$messages = array('<comment>Usage:</comment>', ' '.$object->getSynopsis(), '');
$description = new CommandDescription($object);
$messages = array('<comment>Usage:</comment>', ' '.$description->getSynopsis(), '');

if ($object->getAliases()) {
$messages[] = '<comment>Aliases:</comment> <info>'.implode(', ', $object->getAliases()).'</info>';
$messages[] = '<comment>Aliases:</comment> <info>'.implode(', ', $description->getAliases()).'</info>';
}

$descriptor = new InputDefinitionTextDescriptor();
$messages[] = $descriptor->getFormattedText($object->getDefinition());
$messages[] = $descriptor->getFormattedText($description->getDefinition());

if ($help = $object->getProcessedHelp()) {
if ($help = $description->getHelp()) {
$messages[] = '<comment>Help:</comment>';
$messages[] = ' '.str_replace("\n", "\n ", $help)."\n";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Descriptor\Xml;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Descriptor\CommandDescription;

/**
* @author Jean-François Simon <contact@jfsimon.fr>
Expand All @@ -27,26 +28,28 @@ public function buildDocument(\DOMNode $parent, $object)
$parent->appendChild($commandXML = $dom->createElement('command'));

/** @var Command $object */
$commandXML->setAttribute('id', $object->getName());
$commandXML->setAttribute('name', $object->getName());
$description = new CommandDescription($object);

$commandXML->setAttribute('id', $description->getName());
$commandXML->setAttribute('name', $description->getName());

$commandXML->appendChild($usageXML = $dom->createElement('usage'));
$usageXML->appendChild($dom->createTextNode(sprintf($object->getSynopsis(), '')));
$usageXML->appendChild($dom->createTextNode(sprintf($description->getSynopsis(), '')));

$commandXML->appendChild($descriptionXML = $dom->createElement('description'));
$descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $object->getDescription())));
$descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $description->getDescription())));

$commandXML->appendChild($helpXML = $dom->createElement('help'));
$helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $object->getProcessedHelp())));
$helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $description->getHelp())));

$commandXML->appendChild($aliasesXML = $dom->createElement('aliases'));
foreach ($object->getAliases() as $alias) {
foreach ($description->getAliases() as $alias) {
$aliasesXML->appendChild($aliasXML = $dom->createElement('alias'));
$aliasXML->appendChild($dom->createTextNode($alias));
}

$descriptor = new InputDefinitionXmlDescriptor();
$descriptor->buildDocument($commandXML, $object->getDefinition());
$descriptor->buildDocument($commandXML, $description->getDefinition());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/DescriptorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function describe(OutputInterface $output, $object, $format = null, $raw
{
$format = $format ?: $this->provider->getDefaultFormat();
$descriptor = $this->provider->configure(array('raw_text' => $raw))->get($object, $format);
$type = $raw && $descriptor->useFormatting() ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW;
$type = !$raw && $descriptor->useFormatting() ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW;

$output->writeln($descriptor->describe($object), $type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"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 app\/console help list<\/info>\n\nYou can also output the help in other formats by using the <comment>--format<\/comment> option:\n\n <info>php app\/console help --format=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":true,"is_multiple":false,"description":"To output help in other formats","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 app\/console list<\/info>\n\nYou can also display the commands for a specific namespace:\n\n <info>php app\/console list test<\/info>\n\nYou can also output the information in other formats by using the <comment>--format<\/comment> option:\n\n <info>php app\/console list --format=xml<\/info>\n\nIt's also possible to get raw list of commands (useful for embedding command runner):\n\n <info>php app\/console 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":true,"is_multiple":false,"description":"To output help in other formats","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"]}]}
{"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 app\/console help list<\/info>\n\nYou can also output the help in other formats by using the <comment>--format<\/comment> option:\n\n <info>php app\/console help --format=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":true,"is_multiple":false,"description":"To output help in other formats.","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 app\/console list<\/info>\n\nYou can also display the commands for a specific namespace:\n\n <info>php app\/console list test<\/info>\n\nYou can also output the information in other formats by using the <comment>--format<\/comment> option:\n\n <info>php app\/console list --format=xml<\/info>\n\nIt's also possible to get raw list of commands (useful for embedding command runner):\n\n <info>php app\/console 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":true,"is_multiple":false,"description":"To output help in other formats.","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"]}]}
8 changes: 4 additions & 4 deletions src/Symfony/Component/Console/Tests/Fixtures/application_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ To display the list of available commands, please use the <info>list</info> comm
* Accept value: yes
* Is value required: yes
* Is multiple: no
* Description: To output help in other formats
* Description: To output help in other formats.
* Default: `NULL`

**raw:**
Expand All @@ -50,7 +50,7 @@ To display the list of available commands, please use the <info>list</info> comm
* Accept value: no
* Is value required: no
* Is multiple: no
* Description: To output raw command list
* Description: To output raw command list.
* Default: `false`

list
Expand Down Expand Up @@ -95,7 +95,7 @@ It's also possible to get raw list of commands (useful for embedding command run
* Accept value: yes
* Is value required: yes
* Is multiple: no
* Description: To output help in other formats
* Description: To output help in other formats.
* Default: `NULL`

**raw:**
Expand All @@ -105,5 +105,5 @@ It's also possible to get raw list of commands (useful for embedding command run
* Accept value: no
* Is value required: no
* Is multiple: no
* Description: To output raw command list
* Description: To output raw command list.
* Default: `false`
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
</arguments>
<options>
<option name="--format" shortcut="" accept_value="1" is_value_required="1" is_multiple="0">
<description>To output help in other formats</description>
<description>To output help in other formats.</description>
</option>
<option name="--raw" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
<description>To output raw command list</description>
<description>To output raw command list.</description>
</option>
</options>
</command>
Expand Down Expand Up @@ -58,10 +58,10 @@
</arguments>
<options>
<option name="--format" shortcut="" accept_value="1" is_value_required="1" is_multiple="0">
<description>To output help in other formats</description>
<description>To output help in other formats.</description>
</option>
<option name="--raw" shortcut="" accept_value="0" is_value_required="0" is_multiple="0">
<description>To output raw command list</description>
<description>To output raw command list.</description>
</option>
</options>
</command>
Expand Down
Loading
0