-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
namespace Symfony\Component\Console\Descriptor; | ||
|
||
use Symfony\Component\Console\Descriptor\Json; | ||
|
||
/** | ||
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com> | ||
*/ | ||
|
@@ -13,18 +15,29 @@ class DescriptorProvider | |
private $descriptors = array(); | ||
|
||
/** | ||
* @var string | ||
* @var array | ||
*/ | ||
private $defaultFormat; | ||
private $options = array( | ||
'default_format' => 'txt', | ||
'json_encoding' => 0, | ||
'namespace' => null, | ||
); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $defaultFormat | ||
* @param array $options | ||
*/ | ||
public function __construct($defaultFormat = 'txt') | ||
public function __construct(array $options = array()) | ||
{ | ||
$this->defaultFormat = $defaultFormat; | ||
$this | ||
->configure($options) | ||
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. You are configuring, then adding the descriptors. This means the configuration will not be applied to them at all. Is that intentional? 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. Yes, the 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. Ah, I see. Missed that part. :) |
||
->add(new Json\ApplicationJsonDescriptor()) | ||
->add(new Json\CommandJsonDescriptor()) | ||
->add(new Json\InputDefinitionJsonDescriptor()) | ||
->add(new Json\InputArgumentJsonDescriptor()) | ||
->add(new Json\InputOptionJsonDescriptor()) | ||
; | ||
} | ||
|
||
/** | ||
|
@@ -36,11 +49,30 @@ public function __construct($defaultFormat = 'txt') | |
*/ | ||
public function add(DescriptorInterface $descriptor) | ||
{ | ||
$descriptor->configure($this->options); | ||
$this->descriptors[] = $descriptor; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Configures provider with options. | ||
* | ||
* @param array $options | ||
* | ||
* @return DescriptorProvider | ||
*/ | ||
public function configure(array $options) | ||
{ | ||
$this->options = array_replace($this->options, $options); | ||
|
||
foreach ($this->descriptors as $descriptor) { | ||
$descriptor->configure($this->options); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Provides a descriptor for given object and format. | ||
* | ||
|
@@ -69,7 +101,7 @@ public function get($object, $format) | |
*/ | ||
public function getDefaultFormat() | ||
{ | ||
return $this->defaultFormat; | ||
return $this->options['default_format']; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Descriptor\Json; | ||
|
||
use Symfony\Component\Console\Descriptor\DescriptorInterface; | ||
|
||
/** | ||
* @author Jean-François Simon <contact@jfsimon.fr> | ||
*/ | ||
abstract class AbstractJsonDescriptor implements DescriptorInterface | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $encodingOptions = 0; | ||
|
||
/** | ||
* @param int $encodingOptions | ||
*/ | ||
public function __construct($encodingOptions = 0) | ||
{ | ||
$this->encodingOptions = $encodingOptions; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure(array $options) | ||
{ | ||
$this->encodingOptions = $options['json_encoding']; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function describe($object, $raw = false) | ||
{ | ||
return json_encode($this->getData($object), $this->encodingOptions); | ||
} | ||
|
||
/** | ||
* Returns object data to encode. | ||
* | ||
* @param object $object | ||
* | ||
* @return array | ||
*/ | ||
abstract public function getData($object); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFormat() | ||
{ | ||
return 'json'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function useFormatting() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Builds a JSON descriptor. | ||
* | ||
* @param AbstractJsonDescriptor $descriptor | ||
* | ||
* @return AbstractJsonDescriptor | ||
*/ | ||
protected function build(AbstractJsonDescriptor $descriptor) | ||
{ | ||
$descriptor->configure(array('json_encoding' => $this->encodingOptions)); | ||
|
||
return $descriptor; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Descriptor\Json; | ||
|
||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Command\Command; | ||
|
||
/** | ||
* @author Jean-François Simon <contact@jfsimon.fr> | ||
*/ | ||
class ApplicationJsonDescriptor extends AbstractJsonDescriptor | ||
{ | ||
/** | ||
* @var string|null | ||
*/ | ||
private $namespace; | ||
|
||
public function __construct($namespace = null, $encodingOptions = 0) | ||
{ | ||
$this->namespace = $namespace; | ||
parent::__construct($encodingOptions); | ||
} | ||
|
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure(array $options) | ||
{ | ||
$this->namespace = $options['namespace']; | ||
|
||
return parent::configure($options); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getData($object) | ||
{ | ||
$commandDescriptor = $this->build(new CommandJsonDescriptor()); | ||
|
||
/** @var Application $object */ | ||
$commands = $object->all($this->namespace ? $object->findNamespace($this->namespace) : null); | ||
$data = array('commands' => array(), 'namespaces' => array()); | ||
|
||
foreach ($this->sortCommands($object, $commands) as $space => $commands) { | ||
$namespaceData = array('id' => $space, 'commands' => array()); | ||
|
||
/** @var Command $command */ | ||
foreach ($commands as $command) { | ||
if (!$command->getName()) { | ||
continue; | ||
} | ||
|
||
$data['commands'][] = $commandDescriptor->getData($command); | ||
$namespaceData['commands'][] = $command->getName(); | ||
} | ||
|
||
$data['namespaces'][] = $namespaceData; | ||
} | ||
|
||
if ($this->namespace) { | ||
$data['namespace'] = $this->namespace; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($object) | ||
{ | ||
return $object instanceof Application; | ||
} | ||
|
||
/** | ||
* @param Application $application | ||
* @param Command[] $commands | ||
* | ||
* @return array | ||
*/ | ||
private function sortCommands(Application $application, array $commands) | ||
{ | ||
$method = new \ReflectionMethod($application, 'sortCommands'); | ||
$method->setAccessible(true); | ||
|
||
return $method->invoke($application, $commands); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Descriptor\Json; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
|
||
/** | ||
* @author Jean-François Simon <contact@jfsimon.fr> | ||
*/ | ||
class CommandJsonDescriptor extends AbstractJsonDescriptor | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getData($object) | ||
{ | ||
$definitionDescriptor = $this->build(new InputDefinitionJsonDescriptor()); | ||
|
||
/** @var Command $object */ | ||
return array( | ||
'name' => $object->getName(), | ||
'usage' => $object->getSynopsis(), | ||
'description' => $object->getDescription(), | ||
'help' => $object->getProcessedHelp(), | ||
'aliases' => $object->getAliases(), | ||
'definition' => $definitionDescriptor->getData($object->getDefinition()), | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($object) | ||
{ | ||
return $object instanceof Command; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Descriptor\Json; | ||
|
||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
/** | ||
* @author Jean-François Simon <contact@jfsimon.fr> | ||
*/ | ||
class InputArgumentJsonDescriptor extends AbstractJsonDescriptor | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getData($object) | ||
{ | ||
/** @var InputArgument $object */ | ||
return array( | ||
'name' => $object->getName(), | ||
'is_required' => $object->isRequired(), | ||
'is_array' => $object->isArray(), | ||
'description' => $object->getDescription(), | ||
'default' => $object->getDefault(), | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($object) | ||
{ | ||
return $object instanceof InputArgument; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Descriptor\Json; | ||
|
||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputDefinition; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
/** | ||
* @author Jean-François Simon <contact@jfsimon.fr> | ||
*/ | ||
class InputDefinitionJsonDescriptor extends AbstractJsonDescriptor | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getData($object) | ||
{ | ||
$argumentDescriptor = $this->build(new InputArgumentJsonDescriptor()); | ||
$optionDescriptor = $this->build(new InputOptionJsonDescriptor()); | ||
|
||
/** @var InputDefinition $object */ | ||
return array( | ||
'arguments' => array_map(function (InputArgument $argument) use ($argumentDescriptor) { | ||
return $argumentDescriptor->getData($argument); | ||
}, $object->getArguments()), | ||
'options' => array_map(function (InputOption $option) use ($optionDescriptor) { | ||
return $optionDescriptor->getData($option); | ||
}, $object->getOptions()), | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($object) | ||
{ | ||
return $object instanceof InputDefinition; | ||
} | ||
} |
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.
Can you remove the blank lines between @param lines?