-
-
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
Closed
Closed
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 f239b1c
[Console] added JSON descriptors
jfsimon bebf1eb
[Console] added markdown descriptors
jfsimon 43b5e5c
[Console] added forgotten headers
jfsimon acc7414
[Console] removed unused methods (tests broken)
jfsimon 774794c
[Console] fixed tests
jfsimon 2066a9b
[Console] added XML descriptors
jfsimon 47fa194
[Console] started text derciptors
jfsimon 8b56eb1
[Console] finished text descriptors
jfsimon 27aa872
[Console] repaired tests
jfsimon 30d8807
[Console] re-introduced asText & asXml methods to avoid BC break
jfsimon 9964838
[Console] added command definition merge be fore description
jfsimon ef6d8ba
[Console] simplified description commands
jfsimon 389101a
[Console] re-introduced --xml option to avoid BC break
jfsimon 84be8de
[Console] simplified mardown descriptors
jfsimon ce5c0fd
[Console] applied comments from github
jfsimon 49a4612
[Console] applied comments from github
jfsimon 20c10a5
[Console] added provider injection to descriptors
jfsimon 9c7b358
[Console] simplified descriptors
jfsimon d3ec073
[Console] applied advices from github comments
jfsimon d70e086
[Console] removed proxy
jfsimon ce60fb7
[Console] simplified descriptor interface
jfsimon 28f082e
[Console] removed changes
jfsimon cbb8105
[Console] moved commands arguments
jfsimon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Console] added markdown descriptors
- Loading branch information
commit bebf1ebb2882b330bf7c9e17bb2be7045eb66f8d
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/Symfony/Component/Console/Descriptor/ApplicationDescription.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Descriptor; | ||
|
||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Command\Command; | ||
|
||
/** | ||
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com> | ||
*/ | ||
class ApplicationDescription | ||
{ | ||
const GLOBAL_NAMESPACE = '_global'; | ||
|
||
private $application; | ||
private $namespace; | ||
private $namespaces; | ||
private $commands; | ||
|
||
public function __construct(Application $application, $namespace = null) | ||
{ | ||
$this->application = $application; | ||
$this->namespace = $namespace; | ||
} | ||
|
||
public function getNamespaces() | ||
{ | ||
if (null === $this->namespaces) { | ||
$this->inspectApplication(); | ||
} | ||
|
||
return $this->namespaces; | ||
} | ||
|
||
public function getCommands() | ||
{ | ||
if (null === $this->commands) { | ||
$this->inspectApplication(); | ||
} | ||
|
||
return $this->commands; | ||
} | ||
|
||
private function inspectApplication() | ||
{ | ||
$this->commands = array(); | ||
$this->namespaces = array(); | ||
|
||
$all = $this->application->all($this->namespace ? $this->application->findNamespace($this->namespace) : null); | ||
foreach ($this->sortCommands($all) as $namespace => $commands) { | ||
$names = array(); | ||
|
||
/** @var Command $command */ | ||
foreach ($commands as $name => $command) { | ||
if (!$command->getName()) { | ||
continue; | ||
} | ||
|
||
// aliases must be skipped in commands list | ||
if ($name === $command->getName()) { | ||
$this->commands[] = $command; | ||
} | ||
|
||
$names[] = $name; | ||
} | ||
|
||
$this->namespaces[] = array('id' => $namespace, 'commands' => $names); | ||
} | ||
} | ||
|
||
private function sortCommands(array $commands) | ||
{ | ||
$method = new \ReflectionMethod($this->application, 'sortCommands'); | ||
$method->setAccessible(true); | ||
|
||
return $method->invoke($this->application, $commands); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/Symfony/Component/Console/Descriptor/Markdown/AbstractMarkdownDescriptor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Descriptor\Markdown; | ||
|
||
use Symfony\Component\Console\Descriptor\DescriptorInterface; | ||
|
||
/** | ||
* @author Jean-François Simon <contact@jfsimon.fr> | ||
*/ | ||
abstract class AbstractMarkdownDescriptor implements DescriptorInterface | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $maxWidth; | ||
|
||
/** | ||
* @param int $maxWidth | ||
*/ | ||
public function __construct($maxWidth = 120) | ||
{ | ||
$this->maxWidth = $maxWidth; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure(array $options) | ||
{ | ||
$this->maxWidth = $options['markdown_width']; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function describe($object, $raw = false) | ||
{ | ||
return $this->getDocument($object)->format(new Document\Formatter($this->maxWidth)); | ||
} | ||
|
||
/** | ||
* Returns object document to format. | ||
* | ||
* @param object $object | ||
* | ||
* @return Document\Document | ||
*/ | ||
abstract public function getDocument($object); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFormat() | ||
{ | ||
return 'md'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function useFormatting() | ||
{ | ||
return false; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/Symfony/Component/Console/Descriptor/Markdown/ApplicationMarkdownDescriptor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Descriptor\Markdown; | ||
|
||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Descriptor\ApplicationDescription; | ||
|
||
/** | ||
* @author Jean-François Simon <contact@jfsimon.fr> | ||
*/ | ||
class ApplicationMarkdownDescriptor extends AbstractMarkdownDescriptor | ||
{ | ||
/** | ||
* @var string|null | ||
*/ | ||
private $namespace; | ||
|
||
/** | ||
* @param string|null $namespace | ||
* @param int $maxWidth | ||
*/ | ||
public function __construct($namespace = null, $maxWidth = 120) | ||
{ | ||
$this->namespace = $namespace; | ||
parent::__construct($maxWidth); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure(array $options) | ||
{ | ||
$this->namespace = $options['namespace']; | ||
|
||
return parent::configure($options); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDocument($object) | ||
{ | ||
/** @var Application $object */ | ||
$description = new ApplicationDescription($object, $this->namespace); | ||
$descriptor = new CommandMarkdownDescriptor(); | ||
$document = new Document\Document(array(new Document\Title($object->getName(), 1))); | ||
|
||
foreach ($description->getNamespaces() as $namespace) { | ||
if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) { | ||
$document->add(new Document\Paragraph('**'.$namespace['id'].':**')); | ||
} | ||
|
||
$document->add(new Document\UnorderedList($namespace['commands'])); | ||
} | ||
|
||
foreach ($description->getCommands() as $command) { | ||
$document->add($descriptor->getDocument($command)); | ||
} | ||
|
||
return $document; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($object) | ||
{ | ||
return $object instanceof Application; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Symfony/Component/Console/Descriptor/Markdown/CommandMarkdownDescriptor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Descriptor\Markdown; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
|
||
/** | ||
* @author Jean-François Simon <contact@jfsimon.fr> | ||
*/ | ||
class CommandMarkdownDescriptor extends AbstractMarkdownDescriptor | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDocument($object) | ||
{ | ||
$definitionDescriptor = new InputDefinitionMarkdownDescriptor(); | ||
|
||
$object->getProcessedHelp(); | ||
|
||
/** @var Command $object */ | ||
return new Document\Document(array( | ||
new Document\Title($object->getName(), 2), | ||
new Document\UnorderedList(array( | ||
'Description: '.($object->getDescription() ?: '<none>'), | ||
'Usage: `'.$object->getSynopsis().'`', | ||
'Aliases: '.(count($object->getAliases()) ? '`'.implode('`, `', $object->getAliases()).'`' : '<none>'), | ||
)), | ||
new Document\Paragraph($object->getProcessedHelp()), | ||
$definitionDescriptor->getDocument($object->getDefinition()), | ||
)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($object) | ||
{ | ||
return $object instanceof Command; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Console/Descriptor/Markdown/Document/BlockInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Descriptor\Markdown\Document; | ||
|
||
/** | ||
* Document block interface. | ||
* | ||
* @author Jean-François Simon <contact@jfsimon.fr> | ||
*/ | ||
interface BlockInterface | ||
{ | ||
/** | ||
* @return boolean | ||
*/ | ||
public function isEmpty(); | ||
|
||
/** | ||
* Formats block output. | ||
* | ||
* @param Formatter $formatter | ||
* | ||
* @return string | ||
*/ | ||
public function format(Formatter $formatter); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should we really rely on reflection to call this method ? It looks hackish to me
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.
I had to change the scope of the following methods:
Application::extractNamespace
from private to publicCommand::getNativeDefinition
from protected to publicI guess it's a BC break, isn't it?