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
Prev Previous commit
Next Next commit
[Console] started text derciptors
  • Loading branch information
jfsimon committed Apr 12, 2013
commit 47fa194a7c927023ee0317c6c9583cbf1ddb21a9
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,41 @@ class ApplicationDescription
{
const GLOBAL_NAMESPACE = '_global';

/**
* @var Application
*/
private $application;

/**
* @var null|string
*/
private $namespace;

/**
* @var array
*/
private $namespaces;

/**
* @var Command[]
*/
private $commands;

/**
* Constructor.
*
* @param Application $application
* @param string|null $namespace
*/
public function __construct(Application $application, $namespace = null)
{
$this->application = $application;
$this->namespace = $namespace;
}

/**
* @return array
*/
public function getNamespaces()
{
if (null === $this->namespaces) {
Expand All @@ -41,6 +65,9 @@ public function getNamespaces()
return $this->namespaces;
}

/**
* @return Command[]
*/
public function getCommands()
{
if (null === $this->commands) {
Expand All @@ -50,6 +77,22 @@ public function getCommands()
return $this->commands;
}

/**
* @param string $name
*
* @return Command
*
* @throws \InvalidArgumentException
*/
public function getCommand($name)
{
if (!isset($this->commands[$name])) {
throw new \InvalidArgumentException('Command "'.$name.'" does not exist.');
Copy link
Member

Choose a reason for hiding this comment

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

we have standardize to use sprintf for exception messages.

}

return $this->commands[$name];
}

private function inspectApplication()
{
$this->commands = array();
Expand All @@ -67,16 +110,21 @@ private function inspectApplication()

// aliases must be skipped in commands list
if ($name === $command->getName()) {
$this->commands[] = $command;
$this->commands[$name] = $command;
}

$names[] = $name;
}

$this->namespaces[] = array('id' => $namespace, 'commands' => $names);
$this->namespaces[$namespace] = array('id' => $namespace, 'commands' => $names);
}
}

/**
* @param array $commands
*
* @return array
*/
private function sortCommands(array $commands)
{
$method = new \ReflectionMethod($this->application, 'sortCommands');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ public function configure(array $options);
* Returns given object's representation.
*
* @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);
public function describe($object);

/**
* Tests if this descriptor supports given object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class DescriptorProvider
private $options = array(
'default_format' => 'txt',
'namespace' => null,
'raw_text' => false,
'json_encoding' => 0,
'markdown_width' => 120,
);
Expand All @@ -42,6 +43,11 @@ public function __construct(array $options = array())
{
$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 Xml\ApplicationXmlDescriptor())
->add(new Xml\CommandXmlDescriptor())
->add(new Xml\InputDefinitionXmlDescriptor())
->add(new Xml\InputArgumentXmlDescriptor())
->add(new Xml\InputOptionXmlDescriptor())
->add(new Json\ApplicationJsonDescriptor())
->add(new Json\CommandJsonDescriptor())
->add(new Json\InputDefinitionJsonDescriptor())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function configure(array $options)
/**
* {@inheritdoc}
*/
public function describe($object, $raw = false)
public function describe($object)
{
return json_encode($this->getData($object), $this->encodingOptions);
}
Expand Down Expand Up @@ -75,18 +75,4 @@ 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
Expand Up @@ -53,7 +53,7 @@ public function getData($object)
{
/** @var Application $object */
$description = new ApplicationDescription($object, $this->namespace);
$descriptor = $this->build(new CommandJsonDescriptor());
$descriptor = new CommandJsonDescriptor();
$commands = array_map(array($descriptor, 'getData'), $description->getCommands());

return null === $this->namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CommandJsonDescriptor extends AbstractJsonDescriptor
*/
public function getData($object)
{
$definitionDescriptor = $this->build(new InputDefinitionJsonDescriptor());
$definitionDescriptor = new InputDefinitionJsonDescriptor();
Copy link
Member

Choose a reason for hiding this comment

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

creating a new descriptor inside the descriptor looks weird to me


/** @var Command $object */
return array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class InputDefinitionJsonDescriptor extends AbstractJsonDescriptor
*/
public function getData($object)
{
$argumentDescriptor = $this->build(new InputArgumentJsonDescriptor());
$optionDescriptor = $this->build(new InputOptionJsonDescriptor());
$argumentDescriptor = new InputArgumentJsonDescriptor();
$optionDescriptor = new InputOptionJsonDescriptor();

/** @var InputDefinition $object */
return array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function configure(array $options)
/**
* {@inheritdoc}
*/
public function describe($object, $raw = false)
public function describe($object)
{
return $this->getDocument($object)->format(new Document\Formatter($this->maxWidth));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Text;

use Symfony\Component\Console\Descriptor\DescriptorInterface;

/**
* @author Jean-François Simon <[email protected]>
*/
abstract class AbstractTextDescriptor implements DescriptorInterface
{
/**
* @var boolean
*/
private $raw;

/**
* @param boolean $raw
*/
public function __construct($raw = false)
{
$this->raw = $raw;
}

/**
* {@inheritdoc}
*/
public function configure(array $options)
{
if (isset($options['raw_text'])) {
$this->raw = $options['raw_text'];
}

return $this;
}

/**
* {@inheritdoc}
*/
public function describe($object)
{
return $this->raw ? $this->getRawText($object) : $this->getFormattedText($object);
}

/**
* Returns object's raw text description.
*
* @param object $object
*
* @return string
*/
abstract public function getRawText($object);

/**
* Returns object's formatted text description.
*
* @param object $object
*
* @return string
*/
abstract public function getFormattedText($object);

/**
* {@inheritdoc}
*/
public function getFormat()
{
return 'txt';
}

/**
* {@inheritdoc}
*/
public function useFormatting()
{
return false;
}
}
Loading