8000 [Console] application/command as text/xml/whatever decoupling by jfsimon · Pull Request #7454 · symfony/symfony · GitHub
[go: up one dir, main page]

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 JSON descriptors
  • Loading branch information
jfsimon committed Apr 12, 2013
commit f239b1c5c442b788b6c779ca110e34f97669f521
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
*/
interface DescriptorInterface
{
/**
* Configures descriptor with description options.
*
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 array $options Description options
*
* @return DescriptorInterface
*/
public function configure(array $options);

/**
* Returns given object's representation.
*
Expand Down
44 changes: 38 additions & 6 deletions src/Symfony/Component/Console/Descriptor/DescriptorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Symfony\Component\Console\Descriptor;

use Symfony\Component\Console\Descriptor\Json;

/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the add() method already configures the added descriptor.

Copy link
Contributor

Choose a reason for hiding this comment

The 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())
;
}

/**
Expand All @@ -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.
*
Expand Down Expand Up @@ -69,7 +101,7 @@ public function get($object, $format)
*/
public function getDefaultFormat()
{
return $this->defaultFormat;
return $this->options['default_format'];
}

/**
Expand Down
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;
}
}
Loading
0