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 all commits
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
121 changes: 14 additions & 107 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Console;

use Symfony\Component\Console\Descriptor\TextDescriptor;
use Symfony\Component\Console\Descriptor\XmlDescriptor;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\ArrayInput;
Expand Down Expand Up @@ -687,47 +689,14 @@ public static function getAbbreviations($names)
* @param boolean $raw Whether to return raw command list
*
* @return string A string representing the Application
Copy link
Member

Choose a reason for hiding this comment

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

you need to provide a BC layer to avoid the BC break

*
* @deprecated Deprecated since version 2.3, to be removed in 3.0.
*/
public function asText($namespace = null, $raw = false)
{
$commands = $namespace ? $this->all($this->findNamespace($namespace)) : $this->commands;

$width = 0;
foreach ($commands as $command) {
$width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
}
$width += 2;

if ($raw) {
$messages = array();
foreach ($this->sortCommands($commands) as $space => $commands) {
foreach ($commands as $name => $command) {
$messages[] = sprintf("%-${width}s %s", $name, $command->getDescription());
}
}

return implode(PHP_EOL, $messages);
}
$descriptor = new TextDescriptor();

$messages = array($this->getHelp(), '');
if ($namespace) {
$messages[] = sprintf("<comment>Available commands for the \"%s\" namespace:</comment>", $namespace);
} else {
$messages[] = '<comment>Available commands:</comment>';
}

// add commands by namespace
foreach ($this->sortCommands($commands) as $space => $commands) {
if (!$namespace && '_global' !== $space) {
$messages[] = '<comment>'.$space.'</comment>';
}

foreach ($commands as $name => $command) {
$messages[] = sprintf(" <info>%-${width}s</info> %s", $name, $command->getDescription());
}
}

return implode(PHP_EOL, $messages);
return $descriptor->describe($this, array('namespace' => $namespace, 'raw_text' => $raw));
}

/**
Expand All @@ -736,52 +705,15 @@ public function asText($namespace = null, $raw = false)
* @param string $namespace An optional namespace name
* @param Boolean $asDom Whether to return a DOM or an XML string
*
* @return string|DOMDocument An XML string representing the Application
* @return string|\DOMDocument An XML string representing the Application
*
* @deprecated Deprecated since version 2.3, to be removed in 3.0.
*/
public function asXml($namespace = null, $asDom = false)
{
$commands = $namespace ? $this->all($this->findNamespace($namespace)) : $this->commands;

$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$dom->appendChild($xml = $dom->createElement('symfony'));

$xml->appendChild($commandsXML = $dom->createElement('commands'));

if ($namespace) {
$commandsXML->setAttribute('namespace', $namespace);
} else {
$namespacesXML = $dom->createElement('namespaces');
$xml->appendChild($namespacesXML);
}

// add commands by namespace
foreach ($this->sortCommands($commands) as $space => $commands) {
if (!$namespace) {
$namespaceArrayXML = $dom->createElement('namespace');
$namespacesXML->appendChild($namespaceArrayXML);
$namespaceArrayXML->setAttribute('id', $space);
}

foreach ($commands as $name => $command) {
if ($name !== $command->getName()) {
continue;
}

if (!$namespace) {
$commandXML = $dom->createElement('command');
$namespaceArrayXML->appendChild($commandXML);
$commandXML->appendChild($dom->createTextNode($name));
}

$node = $command->asXml(true)->getElementsByTagName('command')->item(0);
$node = $dom->importNode($node, true);
$descriptor = new XmlDescriptor();

$commandsXML->appendChild($node);
}
}

return $asDom ? $dom : $dom->saveXml();
return $descriptor->describe($this, array('namespace' => $namespace, 'as_dom' => $asDom));
}

/**
Expand Down Expand Up @@ -1066,33 +998,6 @@ private function getConsoleMode()
}
}

/**
* Sorts commands in alphabetical order.
*
* @param array $commands An associative array of commands to sort
*
* @return array A sorted array of commands
*/
private function sortCommands($commands)
{
$namespacedCommands = array();
foreach ($commands as $name => $command) {
$key = $this->extractNamespace($name, 1);
if (!$key) {
$key = '_global';
}

$namespacedCommands[$key][$name] = $command;
}
ksort($namespacedCommands);

foreach ($namespacedCommands as &$commands) {
ksort($commands);
}

return $namespacedCommands;
}

/**
* Returns abbreviated suggestions in string format.
*
Expand All @@ -1108,12 +1013,14 @@ private function getAbbreviationSuggestions($abbrevs)
/**
* Returns the namespace part of the command name.
*
* This method is not part of public API and should not be used directly.
*
* @param string $name The full name of the command
* @param string $limit The maximum number of parts of the namespace
*
* @return string The namespace of the command
*/
private function extractNamespace($name, $limit = null)
public function extractNamespace($name, $limit = null)
{
$parts = explode(':', $name);
array_pop($parts);
Expand Down
72 changes: 17 additions & 55 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Console\Command;

use Symfony\Component\Console\Descriptor\TextDescriptor;
use Symfony\Component\Console\Descriptor\XmlDescriptor;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -273,9 +275,11 @@ public function setCode($code)
/**
* Merges the application definition with the command definition.
*
* This method is not part of public API and should not be used directly.
*
* @param Boolean $mergeArgs Whether to merge or not the Application definition arguments to Command definition arguments
*/
private function mergeApplicationDefinition($mergeArgs = true)
public function mergeApplicationDefinition($mergeArgs = true)
{
if (null === $this->application || true === $this->applicationDefinitionMerged) {
return;
Expand Down Expand Up @@ -332,9 +336,11 @@ public function getDefinition()
* Can be overridden to provide the original command representation when it would otherwise
* be changed by merging with the application InputDefinition.
*
* This method is not part of public API and should not be used directly.
*
* @return InputDefinition An InputDefinition instance
*/
protected function getNativeDefinition()
public function getNativeDefinition()
{
return $this->getDefinition();
}
Expand Down Expand Up @@ -559,74 +565,30 @@ public function getHelper($name)
* Returns a text representation of the command.
*
* @return string A string representing the command
*
* @deprecated Deprecated since version 2.3, to be removed in 3.0.
*/
public function asText()
{
if ($this->application && !$this->applicationDefinitionMerged) {
$this->getSynopsis();
$this->mergeApplicationDefinition(false);
}

$messages = array(
'<comment>Usage:</comment>',
' '.$this->getSynopsis(),
'',
);

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

$messages[] = $this->getNativeDefinition()->asText();

if ($help = $this->getProcessedHelp()) {
$messages[] = '<comment>Help:</comment>';
$messages[] = ' '.str_replace("\n", "\n ", $help)."\n";
}
$descriptor = new TextDescriptor();

return implode("\n", $messages);
return $descriptor->describe($this);
}

/**
* Returns an XML representation of the command.
*
* @param Boolean $asDom Whether to return a DOM or an XML string
*
* @return string|DOMDocument An XML string representing the command
* @return string|\DOMDocument An XML string representing the command
*
* @deprecated Deprecated since version 2.3, to be removed in 3.0.
*/
public function asXml($asDom = false)
{
if ($this->application && !$this->applicationDefinitionMerged) {
$this->getSynopsis();
$this->mergeApplicationDefinition(false);
}

$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$dom->appendChild($commandXML = $dom->createElement('command'));
$commandXML->setAttribute('id', $this->name);
$commandXML->setAttribute('name', $this->name);

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

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

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

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

$definition = $this->getNativeDefinition()->asXml(true);
$commandXML->appendChild($dom->importNode($definition->getElementsByTagName('arguments')->item(0), true));
$commandXML->appendChild($dom->importNode($definition->getElementsByTagName('options')->item(0), true));
$descriptor = new XmlDescriptor();

return $asDom ? $dom : $dom->saveXml();
return $descriptor->describe($this, array('as_dom' => $asDom));
}

private function validateName($name)
Expand Down
14 changes: 8 additions & 6 deletions src/Symfony/Component/Console/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace Symfony\Component\Console\Command;

use Symfony\Component\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;

/**
* HelpCommand displays the help for a given command.
Expand All @@ -38,16 +38,18 @@ protected function configure()
->setDefinition(array(
new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'),
new InputOption('xml', null, InputOption::VALUE_NONE, 'To output help as XML'),
Copy link
Member

Choose a reason for hiding this comment

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

you sould keep a BC layer for --xml

new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output help in other formats'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command help'),
))
->setDescription('Displays help for a command')
->setHelp(<<<EOF
The <info>%command.name%</info> command displays help for a given command:

<info>php %command.full_name% list</info>

You can also output the help as XML by using the <comment>--xml</comment> option:
You can also output the help in other formats by using the <comment>--format</comment> option:

<info>php %command.full_name% --xml list</info>
<info>php %command.full_name% --format=xml list</info>

To display the list of available commands, please use the <info>list</info> command.
EOF
Expand Down Expand Up @@ -75,11 +77,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

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.

if ($input->getOption('xml')) {
$output->writeln($this->command->asXml(), OutputInterface::OUTPUT_RAW);
} else {
$output->writeln($this->command->asText());
$input->setOption('format', 'xml');
}

$helper = new DescriptorHelper();
$helper->describe($output, $this->command, $input->getOption('format'), $input->getOption('raw'));
$this->command = null;
}
}
21 changes: 13 additions & 8 deletions src/Symfony/Component/Console/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace Symfony\Component\Console\Command;

use Symfony\Component\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputDefinition;

/**
Expand Down Expand Up @@ -43,9 +43,9 @@ protected function configure()

<info>php %command.full_name% test</info>

You can also output the information as XML by using the <comment>--xml</comment> option:
You can also output the information in other formats by using the <comment>--format</comment> option:

<info>php %command.full_name% --xml</info>
<info>php %command.full_name% --format=xml</info>

It's also possible to get raw list of commands (useful for embedding command runner):

Expand All @@ -58,7 +58,7 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function getNativeDefinition()
public function getNativeDefinition()
{
return $this->createDefinition();
}
Expand All @@ -69,18 +69,23 @@ protected function getNativeDefinition()
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('xml')) {
$output->writeln($this->getApplication()->asXml($input->getArgument('namespace')), OutputInterface::OUTPUT_RAW);
} else {
$output->writeln($this->getApplication()->asText($input->getArgument('namespace'), $input->getOption('raw')));
$input->setOption('format', 'xml');
}

$helper = new DescriptorHelper();
$helper->describe($output, $this->getApplication(), $input->getOption('format'), $input->getOption('raw'));
}

/**
* {@inheritdoc}
*/
private function createDefinition()
{
return new InputDefinition(array(
new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
new InputOption('xml', null, InputOption::VALUE_NONE, 'To output help as XML'),
new InputOption('xml', null, InputOption::VALUE_NONE, 'To output list as XML'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output list in other formats'),
));
}
}
Loading
0