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
Next Next commit
[Console] added descriptor & refactored description commands
  • Loading branch information
jfsimon committed Apr 12, 2013
commit da67c121d314186f05d0c1e5c58b4f0a33393f65
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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\InputOption;

/**
* Base class for descriptor commands.
*
* @author Jean-François Simon <[email protected]>
*/
abstract class AbstractDescriptorCommand extends Command
{
/**
* @var array
*/
private $supportedFormats;

/**
* {@inheritdoc}
*/
protected function configure()
{
$descriptorProvider = new DescriptorProvider();
$this->supportedFormats = $descriptorProvider->getSupportedFormats();
$this->setDefinition($this->createDefinition());
$this->getHelperSet()->set(new DescriptorHelper($descriptorProvider));
}

/**
* Creates command definition.
*
* @return InputDefinition
*/
protected function createDefinition()
{
return new InputDefinition(array(
new InputOption('format', null, InputOption::VALUE_OPTIONAL, 'Output format ('.implode(', ', $this->supportedFormats).')'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
));
}
}
24 changes: 13 additions & 11 deletions src/Symfony/Component/Console/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
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.
*
* @author Fabien Potencier <[email protected]>
*/
class HelpCommand extends Command
class HelpCommand extends AbstractDescriptorCommand
{
private $command;

Expand All @@ -35,10 +34,6 @@ protected function configure()

$this
->setName('help')
->setDefinition(array(
new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'),
new InputOption('xml', null, InputOption::VALUE_NONE, 'To output help as XML'),
))
->setDescription('Displays help for a command')
->setHelp(<<<EOF
The <info>%command.name%</info> command displays help for a given command:
Expand All @@ -65,6 +60,17 @@ public function setCommand(Command $command)
$this->command = $command;
}

/**
* {@inheritdoc}
*/
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;
}

/**
* {@inheritdoc}
*/
Expand All @@ -74,11 +80,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.

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

$this->command = null;
}
Expand Down
31 changes: 13 additions & 18 deletions src/Symfony/Component/Console/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
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;

/**
* ListCommand displays the list of all available commands for the application.
*
* @author Fabien Potencier <[email protected]>
*/
class ListCommand extends Command
class ListCommand extends AbstractDescriptorCommand
{
/**
* {@inheritdoc}
Expand All @@ -32,7 +30,6 @@ protected function configure()
{
$this
->setName('list')
->setDefinition($this->createDefinition())
->setDescription('Lists commands')
->setHelp(<<<EOF
The <info>%command.name%</info> command lists all commands:
Expand All @@ -58,29 +55,27 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function getNativeDefinition()
protected function createDefinition()
Copy link
Member

Choose a reason for hiding this comment

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

why changing it from private to protected ?

{
return $this->createDefinition();
$definition = parent::createDefinition();
$definition->addArgument(new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'));

return $definition;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function getNativeDefinition()
{
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')));
}
return $this->createDefinition();
}

private function createDefinition()
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
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('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
));
$this->getHelper('descriptor')->describe($output, $this->getApplication(), $input->getArgument('format'), $input->getOption('raw'));
}
}
51 changes: 51 additions & 0 deletions src/Symfony/Component/Console/Descriptor/DescriptorInterface.php
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;

/**
* @author Jean-François Simon <[email protected]>
*/
interface DescriptorInterface
{
/**
* Returns given object's representation.
*
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove the blank lines between @param lines?

* @param object $object The object to describe
* @param boolean $raw No additional markers if true
*
* @return string The object formatted description
*/
public function describe($object, $raw = false);

/**
* Tests if this descriptor supports given object.
*
* @param object $object The object to describe
*
* @return boolean
*/
public function supports($object);

/**
* Returns descriptor's format name.
*
* @return string The format name
*/
public function getFormat();

/**
* Returns true if output formatting is used.
*
* @return boolean
*/
public function useFormatting();
}
89 changes: 89 additions & 0 deletions src/Symfony/Component/Console/Descriptor/DescriptorProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

Copy link

Choose a reason for hiding this comment

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

Might need the copyright at the top of some of these files...

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

/**
* @author Jean-François Simon <[email protected]>
*/
class DescriptorProvider
{
/**
* @var DescriptorInterface[]
*/
private $descriptors = array();

/**
* @var string
*/
private $defaultFormat;

/**
* Constructor.
*
* @param string $defaultFormat
*/
public function __construct($defaultFormat = 'txt')
{
$this->defaultFormat = $defaultFormat;
}

/**
* Adds a descriptor to the stack.
*
* @param DescriptorInterface $descriptor
*
* @return DescriptorProvider
*/
public function add(DescriptorInterface $descriptor)
{
$this->descriptors[] = $descriptor;

return $this;
}

/**
* Provides a descriptor for given object and format.
*
* @param mixed $object The object to describe
* @param string $format The description format
*
* @return DescriptorInterface The object descriptor
*
* @throws \InvalidArgumentException If no descriptors was found
*/
public function get($object, $format)
{
foreach ($this->descriptors as $descriptor) {
if ($format === $descriptor->getFormat() && $descriptor->supports($object)) {
return $descriptor;
}
}

throw new \InvalidArgumentException(sprintf('Unsupported format "%s".', $format));
}

/**
* Returns default format.
*
* @return string
*/
public function getDefaultFormat()
{
return $this->defaultFormat;
}

/**
* Returns supported formats list.
*
* @return array
*/
public function getSupportedFormats()
{
$supportedFormats = array();
foreach ($this->descriptors as $descriptor) {
$supportedFormats[] = $descriptor->getFormat();
}

return array_unique($supportedFormats);
}
}
54 changes: 54 additions & 0 deletions src/Symfony/Component/Console/Helper/DescriptorHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Symfony\Component\Console\Helper;

use Symfony\Component\Console\Descriptor\DescriptorProvider;
use Symfony\Component\Console\Output\OutputInterface;

/**
* This class adds helper method to describe objects in various formats.
*
* @author Jean-François Simon <[email protected]>
*/
class DescriptorHelper extends Helper
{
/**
* @var DescriptorProvider
*/
private $provider;

/**
* Constructor.
*
* @param DescriptorProvider $provider
*/
public function __construct(DescriptorProvider $provider)
{
$this->provider = $provider;
}

/**
* Describes an object if supported.
*
* @param OutputInterface $output
* @param object $object
* @param string $format
* @param boolean $raw
*/
public function describe(OutputInterface $output, $object, $format = null, $raw = false)
{
$format = $format ?: $this->provider->getDefaultFormat();
$descriptor = $this->provider->get($object, $format);
$type = $raw && $descriptor->useFormatting() ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW;

$output->writeln($descriptor->describe($object, $format, $raw), $type);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'descriptor';
}
}