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] added XML descriptors
  • Loading branch information
jfsimon committed Apr 12, 2013
commit 2066a9be21d70788100c8b2d4cf05250953bcfab
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Base class for descriptor commands.
* Base class for description commands.
*
* @author Jean-François Simon <[email protected]>
*/
abstract class AbstractDescriptorCommand extends Command
abstract class AbstractDescriptionCommand extends Command
{
/**
* @var DescriptorProvider
Expand All @@ -38,7 +38,7 @@ protected function configure()
protected function createDefinition()
{
return new InputDefinition(array(
new InputOption('format', null, InputOption::VALUE_OPTIONAL, 'Output format ('.implode(', ', $this->provider->getSupportedFormats()).')'),
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
10 changes: 4 additions & 6 deletions src/Symfony/Component/Console/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author Fabien Potencier <[email protected]>
*/
class HelpCommand extends AbstractDescriptorCommand
class HelpCommand extends AbstractDescriptionCommand
{
private $command;

Expand All @@ -41,9 +41,9 @@ protected function configure()

<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 @@ -77,14 +77,12 @@ 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'));
}

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.

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

$this->command = null;
}
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/Console/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author Fabien Potencier <[email protected]>
*/
class ListCommand extends AbstractDescriptorCommand
class ListCommand extends AbstractDescriptionCommand
{
/**
* {@inheritdoc}
Expand All @@ -42,9 +42,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public function __construct($encodingOptions = 0)
*/
public function configure(array $options)
{
$this->encodingOptions = $options['json_encoding'];
if (isset($options['json_encoding'])) {
$this->encodingOptions = $options['json_encoding'];
}

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public function __construct($namespace = null, $encodingOptions = 0)
*/
public function configure(array $options)
{
$this->namespace = $options['namespace'];
if (isset($options['namespace'])) {
$this->namespace = $options['namespace'];
}

return parent::configure($options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public function __construct($maxWidth = 120)
*/
public function configure(array $options)
{
$this->maxWidth = $options['markdown_width'];
if (isset($options['markdown_width'])) {
$this->maxWidth = $options['markdown_width'];
}

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public function __construct($namespace = null, $maxWidth = 120)
*/
public function configure(array $options)
{
$this->namespace = $options['namespace'];
if (isset($options['namespace'])) {
$this->namespace = $options['namespace'];
}

return parent::configure($options);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

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

namespace Symfony\Component\Console\Descriptor\Xml;

use Symfony\Component\Console\Descriptor\DescriptorInterface;

/**
* @author Jean-François Simon <[email protected]>
*/
abstract class AbstractXmlDescriptor implements DescriptorInterface
{
/**
* {@inheritdoc}
*/
public function configure(array $options)
{
return $this;
}

/**
* {@inheritdoc}
*/
public function describe($object, $raw = false)
{
$document = new \DOMDocument('1.0', 'UTF-8');
$document->formatOutput = true;
$this->buildDocument($document, $object);

return $document->saveXML();
}

/**
* Builds DOM document with object.
*
* @param \DOMNode $parent
* @param object $object
*/
abstract public function buildDocument(\DOMNode $parent, $object);

/**
* {@inheritdoc}
*/
public function getFormat()
{
return 'xml';
}

/**
* {@inheritdoc}
*/
public function useFormatting()
{
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

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

namespace Symfony\Component\Console\Descriptor\Xml;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Descriptor\ApplicationDescription;

/**
* @author Jean-François Simon <[email protected]>
*/
class ApplicationXmlDescriptor extends AbstractXmlDescriptor
{
/**
* @var string|null
*/
private $namespace;

/**
* @param string|null $namespace
*/
public function __construct($namespace = null)
{
$this->namespace = $namespace;
}

/**
* {@inheritdoc}
*/
public function configure(array $options)
{
if (isset($options['namespace'])) {
$this->namespace = $options['namespace'];
}

return parent::configure($options);
}

/**
* {@inheritdoc}
*/
public function buildDocument(\DOMNode $parent, $object)
{
$dom = $parent instanceof \DOMDocument ? $parent : $parent->ownerDocument;
$dom->appendChild($rootXml = $dom->createElement('symfony'));
$rootXml->appendChild($commandsXML = $dom->createElement('commands'));

/** @var Application $object */
$description = new ApplicationDescription($object, $this->namespace);
$descriptor = new CommandXmlDescriptor();

if ($this->namespace) {
$commandsXML->setAttribute('namespace', $this->namespace);
}

foreach ($description->getCommands() as $command) {
$descriptor->buildDocument($commandsXML, $command);
}

if (!$this->namespace) {
$rootXml->appendChild($namespacesXML = $dom->createElement('namespaces'));

foreach ($description->getNamespaces() as $namespace) {
$namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace'));
$namespaceArrayXML->setAttribute('id', $namespace['id']);

foreach ($namespace['commands'] as $name) {
$namespaceArrayXML->appendChild($commandXML = $dom->createElement('command'));
$commandXML->appendChild($dom->createTextNode($name));
}
}
}
}

/**
* {@inheritdoc}
*/
public function supports($object)
{
return $object instanceof Application;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

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

namespace Symfony\Component\Console\Descriptor\Xml;

use Symfony\Component\Console\Command\Command;

/**
* @author Jean-François Simon <[email protected]>
*/
class CommandXmlDescriptor extends AbstractXmlDescriptor
{
/**
* {@inheritdoc}
*/
public function buildDocument(\DOMNode $parent, $object)
{
$dom = $parent instanceof \DOMDocument ? $parent : $parent->ownerDocument;
$parent->appendChild($commandXML = $dom->createElement('command'));

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

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

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

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

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

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

/**
* {@inheritdoc}
*/
public function supports($object)
{
return $object instanceof Command;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

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

namespace Symfony\Component\Console\Descriptor\Xml;

use Symfony\Component\Console\Input\InputArgument;

/**
* @author Jean-François Simon <[email protected]>
*/
class InputArgumentXmlDescriptor extends AbstractXmlDescriptor
{
/**
* {@inheritdoc}
*/
public function buildDocument(\DOMNode $parent, $object)
{
$dom = $parent instanceof \DOMDocument ? $parent : $parent->ownerDocument;

/** @var InputArgument $object */
$parent->appendChild($objectXML = $dom->createElement('argument'));
$objectXML->setAttribute('name', $object->getName());
$objectXML->setAttribute('is_required', $object->isRequired() ? 1 : 0);
$objectXML->setAttribute('is_array', $object->isArray() ? 1 : 0);
$objectXML->appendChild($descriptionXML = $dom->createElement('description'));
$descriptionXML->appendChild($dom->createTextNode($object->getDescription()));

$objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
$defaults = is_array($object->getDefault()) ? $object->getDefault() : (is_bool($object->getDefault()) ? array(var_export($object->getDefault(), true)) : ($object->getDefault() ? array($object->getDefault()) : array()));
foreach ($defaults as $default) {
$defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
$defaultXML->appendChild($dom->createTextNode($default));
}
}

/**
* {@inheritdoc}
*/
public function supports($object)
{
return $object instanceof InputArgument;
}
}
Loading