-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Changes from 1 commit
da67c12
f239b1c
bebf1eb
43b5e5c
acc7414
774794c
2066a9b
47fa194
8b56eb1
27aa872
30d8807
9964838
ef6d8ba
389101a
84be8de
ce5c0fd
49a4612
20c10a5
9c7b358
d3ec073
d70e086
ce60fb7
28f082e
cbb8105
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
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'), | ||
)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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: | ||
|
@@ -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')); | ||
|
||
return $definition; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -74,11 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
$this->command = $this->getApplication()->find($input->getArgument('command_name')); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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} | ||
|
@@ -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: | ||
|
@@ -58,29 +55,27 @@ protected function configure() | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getNativeDefinition() | ||
protected function createDefinition() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')); | ||
} | ||
} |
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. | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
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'; | ||
} | ||
} |
There was a problem hiding this comment.
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 thiscreateDefinition
method