Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Console] made descriptors use output instead of returning a string
[console] made descriptors use output instead of returning a string
[console] updated descriptors usage
[console] fixed descriptors usage & tests
[console] applied advices from github
[Console] applied advices from github

updated changelog
  • Loading branch information
jfsimon committed Jul 31, 2013
1 parent 70bbf96 commit b9fa52c
Show file tree
Hide file tree
Showing 26 changed files with 512 additions and 258 deletions.
14 changes: 12 additions & 2 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputAwareInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
Expand Down Expand Up @@ -685,8 +686,10 @@ public static function getAbbreviations($names)
public function asText($namespace = null, $raw = false)
{
$descriptor = new TextDescriptor();
$output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, !$raw);
$descriptor->describe($output, $this, array('namespace' => $namespace, 'raw_output' => true));

return $descriptor->describe($this, array('namespace' => $namespace, 'raw_text' => $raw));
return $output->fetch();
}

/**
Expand All @@ -703,7 +706,14 @@ public function asXml($namespace = null, $asDom = false)
{
$descriptor = new XmlDescriptor();

return $descriptor->describe($this, array('namespace' => $namespace, 'as_dom' => $asDom));
if ($asDom) {
return $descriptor->getApplicationDocument($this, $namespace);
}

$output = new BufferedOutput();
$descriptor->describe($output, $this, array('namespace' => $namespace));

return $output->fetch();
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.4.0
-----

* [BC BREAK] made descriptors use output instead of returning a string

2.3.0
-----

Expand Down
18 changes: 14 additions & 4 deletions src/Symfony/Component/Console/Command/Command.php
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
Expand Down Expand Up @@ -576,8 +577,10 @@ public function getHelper($name)
public function asText()
{
$descriptor = new TextDescriptor();

return $descriptor->describe($this);
$output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
$descriptor->describe($output, $this, array('raw_output' => true));

return $output->fetch();
}

/**
Expand All @@ -592,8 +595,15 @@ public function asText()
public function asXml($asDom = false)
{
$descriptor = new XmlDescriptor();

return $descriptor->describe($this, array('as_dom' => $asDom));

if ($asDom) {
return $descriptor->getCommandDocument($this);
}

$output = new BufferedOutput();
$descriptor->describe($output, $this);

return $output->fetch();
}

private function validateName($name)
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Console/Command/HelpCommand.php
Expand Up @@ -38,7 +38,7 @@ 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'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output help in other formats'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output help in other formats', 'txt'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command help'),
))
->setDescription('Displays help for a command')
Expand Down Expand Up @@ -81,7 +81,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

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

$this->command = null;
}
}
8 changes: 6 additions & 2 deletions src/Symfony/Component/Console/Command/ListCommand.php
Expand Up @@ -73,7 +73,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

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

/**
Expand All @@ -85,7 +89,7 @@ private function createDefinition()
new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
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'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output list in other formats', 'txt'),
));
}
}
41 changes: 34 additions & 7 deletions src/Symfony/Component/Console/Descriptor/Descriptor.php
Expand Up @@ -16,28 +16,55 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
abstract class Descriptor implements DescriptorInterface
{
public function describe($object, array $options = array())
/**
* @var OutputInterface
*/
private $output;

/**
* {@inheritdoc}
*/
public function describe(OutputInterface $output, $object, array $options = array())
{
$this->output = $output;

switch (true) {
case $object instanceof InputArgument:
return $this->describeInputArgument($object, $options);
$this->describeInputArgument($object, $options);
break;
case $object instanceof InputOption:
return $this->describeInputOption($object, $options);
$this->describeInputOption($object, $options);
break;
case $object instanceof InputDefinition:
return $this->describeInputDefinition($object, $options);
$this->describeInputDefinition($object, $options);
break;
case $object instanceof Command:
return $this->describeCommand($object, $options);
$this->describeCommand($object, $options);
break;
case $object instanceof Application:
return $this->describeApplication($object, $options);
$this->describeApplication($object, $options);
break;
default:
throw new \InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_class($object)));
}
}

throw new \InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_class($object)));
/**
* Writes content to output.
*
* @param string $content
* @param boolean $decorated
*/
protected function write($content, $decorated = false)
{
$this->output->write($content, false, $decorated ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/Symfony/Component/Console/Descriptor/DescriptorInterface.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Console\Descriptor;

use Symfony\Component\Console\Output\OutputInterface;

/**
* Descriptor interface.
*
Expand All @@ -21,10 +23,9 @@ interface DescriptorInterface
/**
* Describes an InputArgument instance.
*
* @param object $object
* @param array $options
*
* @return string|mixed
* @param OutputInterface $output
* @param object $object
* @param array $options
*/
public function describe($object, array $options = array());
public function describe(OutputInterface $output, $object, array $options = array());
}
126 changes: 81 additions & 45 deletions src/Symfony/Component/Console/Descriptor/JsonDescriptor.php
Expand Up @@ -29,65 +29,31 @@ class JsonDescriptor extends Descriptor
*/
protected function describeInputArgument(InputArgument $argument, array $options = array())
{
return $this->output(array(
'name' => $argument->getName(),
'is_required' => $argument->isRequired(),
'is_array' => $argument->isArray(),
'description' => $argument->getDescription(),
'default' => $argument->getDefault(),
), $options);
$this->writeData($this->getInputArgumentData($argument), $options);
}

/**
* {@inheritdoc}
*/
protected function describeInputOption(InputOption $option, array $options = array())
{
return $this->output(array(
'name' => '--'.$option->getName(),
'shortcut' => $option->getShortcut() ? '-'.implode('|-', explode('|', $option->getShortcut())) : '',
'accept_value' => $option->acceptValue(),
'is_value_required' => $option->isValueRequired(),
'is_multiple' => $option->isArray(),
'description' => $option->getDescription(),
'default' => $option->getDefault(),
), $options);
$this->writeData($this->getInputOptionData($option), $options);
}

/**
* {@inheritdoc}
*/
protected function describeInputDefinition(InputDefinition $definition, array $options = array())
{
$inputArguments = array();
foreach ($definition->getArguments() as $name => $argument) {
$inputArguments[$name] = $this->describeInputArgument($argument, array('as_array' => true));
}

$inputOptions = array();
foreach ($definition->getOptions() as $name => $option) {
$inputOptions[$name] = $this->describeInputOption($option, array('as_array' => true));
}

return $this->output(array('arguments' => $inputArguments, 'options' => $inputOptions), $options);
$this->writeData($this->getInputDefinitionData($definition), $options);
}

/**
* {@inheritdoc}
*/
protected function describeCommand(Command $command, array $options = array())
{
$command->getSynopsis();
$command->mergeApplicationDefinition(false);

return $this->output(array(
'name' => $command->getName(),
'usage' => $command->getSynopsis(),
'description' => $command->getDescription(),
'help' => $command->getProcessedHelp(),
'aliases' => $command->getAliases(),
'definition' => $this->describeInputDefinition($command->getNativeDefinition(), array('as_array' => true)),
), $options);
$this->writeData($this->getCommandData($command), $options);
}

/**
Expand All @@ -100,30 +66,100 @@ protected function describeApplication(Application $application, array $options
$commands = array();

foreach ($description->getCommands() as $command) {
$commands[] = $this->describeCommand($command, array('as_array' => true));
$commands[] = $this->getCommandData($command);
}

$data = $describedNamespace
? array('commands' => $commands, 'namespace' => $describedNamespace)
: array('commands' => $commands, 'namespaces' => array_values($description->getNamespaces()));

return $this->output($data, $options);
$this->writeData($data, $options);
}

/**
* Outputs data as array or string according to options.
* Writes data as json.
*
* @param array $data
* @param array $options
*
* @return array|string
*/
private function output(array $data, array $options)
private function writeData(array $data, array $options)
{
$this->write(json_encode($data, isset($options['json_encoding']) ? $options['json_encoding'] : 0));
}

/**
* @param InputArgument $argument
*
* @return array
*/
private function getInputArgumentData(InputArgument $argument)
{
return array(
'name' => $argument->getName(),
'is_required' => $argument->isRequired(),
'is_array' => $argument->isArray(),
'description' => $argument->getDescription(),
'default' => $argument->getDefault(),
);
}

/**
* @param InputOption $option
*
* @return array
*/
private function getInputOptionData(InputOption $option)
{
return array(
'name' => '--'.$option->getName(),
'shortcut' => $option->getShortcut() ? '-'.implode('|-', explode('|', $option->getShortcut())) : '',
'accept_value' => $option->acceptValue(),
'is_value_required' => $option->isValueRequired(),
'is_multiple' => $option->isArray(),
'description' => $option->getDescription(),
'default' => $option->getDefault(),
);
}

/**
* @param InputDefinition $definition
*
* @return array
*/
private function getInputDefinitionData(InputDefinition $definition)
{
if (isset($options['as_array']) && $options['as_array']) {
return $data;
$inputArguments = array();
foreach ($definition->getArguments() as $name => $argument) {
$inputArguments[$name] = $this->getInputArgumentData($argument);
}

$inputOptions = array();
foreach ($definition->getOptions() as $name => $option) {
$inputOptions[$name] = $this->getInputOptionData($option);
}

return json_encode($data, isset($options['json_encoding']) ? $options['json_encoding'] : 0);
return array('arguments' => $inputArguments, 'options' => $inputOptions);
}

/**
* @param Command $command
*
* @return array
*/
private function getCommandData(Command $command)
{
$command->getSynopsis();
$command->mergeApplicationDefinition(false);

return array(
'name' => $command->getName(),
'usage' => $command->getSynopsis(),
'description' => $command->getDescription(),
'help' => $command->getProcessedHelp(),
'aliases' => $command->getAliases(),
'definition' => $this->getInputDefinitionData($command->getNativeDefinition()),
);
}
}

0 comments on commit b9fa52c

Please sign in to comment.