10000 [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] removed unused methods (tests broken)
  • Loading branch information
jfsimon committed Apr 12, 2013
commit acc74140aa0366abbce18d0ebf231c6337a6357e
104 changes: 0 additions & 104 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,110 +680,6 @@ public static function getAbbreviations($names)
return $abbrevs;
}

/**
* Returns a text representation of the Application.
*
* @param string $namespace An optional namespace name
* @param boolean $raw Whether to return raw command list
*
* @return string A string representing the Application
*/
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);
}

$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);
}

/**
* Returns an XML representation of the Application.
*
* @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
*/
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);

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

return $asDom ? $dom : $dom->saveXml();
}

/**
* Renders a caught exception.
*
Expand Down
25 changes: 13 additions & 12 deletions src/Symfony/Component/Console/Command/AbstractDescriptorCommand.php
Original fi D7AE le line number Diff line number Diff line change
@@ -1,20 +1,13 @@
<?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\Command;

use Symfony\Component\Console\Descriptor\DescriptorProvider;
use Symfony\Component\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Base class for descriptor commands.
Expand All @@ -33,10 +26,7 @@ abstract class AbstractDescriptorCommand extends Command
*/
protected function configure()
{
$descriptorProvider = new DescriptorProvider();
$this->supportedFormats = $descriptorProvider->getSupportedFormats();
$this->setDefinition($this->createDefinition());
$this->getHelperSet()->set(new DescriptorHelper($descriptorProvider));
}

/**
Expand All @@ -51,4 +41,15 @@ protected function createDefinition()
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
));
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$descriptorProvider = new DescriptorProvider();
$this->supportedFormats = $descriptorProvider->getSupportedFormats();
$this->setDefinition($this->createDefinition());
$this->getHelperSet()->set(new DescriptorHelper($descriptorProvider));
}
}
74 changes: 0 additions & 74 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,80 +555,6 @@ public function getHelper($name)
return $this->helperSet->get($name);
}

/**
* Returns a text representation of the command.
*
* @return string A string representing the command
*/
public function asText()
{
341A 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";
}

return implode("\n", $messages);
}

/**
* 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
*/
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));

return $asDom ? $dom : $dom->saveXml();
}

private function validateName($name)
{
if (!preg_match('/^[^\:]+(\:[^\:]+)*$/', $name)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ 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 @@ -66,6 +68,7 @@ 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 @@ -76,6 +79,8 @@ protected function createDefinition()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

if (null === $this->command) {
$this->command = $this->getApplication()->find($input->getArgument('command_name'));
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Console/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class ListCommand 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 as you removed the custom parent class


$this
->setName('list')
->setDescription('Lists commands')
Expand Down Expand Up @@ -76,6 +78,8 @@ protected function getNativeDefinition()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$this->getHelper('descriptor')->describe($output, $this->getApplication(), $input->getArgument('format'), $input->getOption('raw'));
}
}
Loading
0