-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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 |
---|---|---|
|
@@ -43,6 +43,11 @@ public function __construct(array $options = array()) | |
{ | ||
$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 Text\ApplicationTextDescriptor()) | ||
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. Wouldn't it be a lot simpler if the application text descriptor itself were responsible for rendering its elements? I think it would simplify the design and lead to less implicit dependency on the order of the descriptors. 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. I'm not sure to understand, this class just acts as a "descriptor selector". |
||
->add(new Text\CommandTextDescriptor()) | ||
->add(new Text\InputDefinitionTextDescriptor()) | ||
->add(new Text\InputArgumentTextDescriptor()) | ||
->add(new Text\InputOptionTextDescriptor()) | ||
->add(new Xml\ApplicationXmlDescriptor()) | ||
->add(new Xml\CommandXmlDescriptor()) | ||
->add(new Xml\InputDefinitionXmlDescriptor()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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\Command\Command; | ||
use Symfony\Component\Console\Input\InputDefinition; | ||
|
||
/** | ||
* @author Jean-François Simon <[email protected]> | ||
*/ | ||
class CommandTextDescriptor extends AbstractTextDescriptor | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRawText($object) | ||
{ | ||
return strip_tags($this->getFormattedText($object)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFormattedText($object) | ||
{ | ||
/** @var Command $object */ | ||
$messages = array('<comment>Usage:</comment>', ' '.$object->getSynopsis(), ''); | ||
|
||
if ($object->getAliases()) { | ||
$messages[] = '<comment>Aliases:</comment> <info>'.implode(', ', $object->getAliases()).'</info>'; | ||
} | ||
|
||
$descriptor = new InputDefinitionTextDescriptor(); | ||
$messages[] = $descriptor->getFormattedText($object->getDefinition()); | ||
|
||
if ($help = $object->getProcessedHelp()) { | ||
$messages[] = '<comment>Help:</comment>'; | ||
$messages[] = ' '.str_replace("\n", "\n ", $help)."\n"; | ||
} | ||
|
||
return implode("\n", $messages); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($object) | ||
{ | ||
return $object instanceof Command; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?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\Input\InputArgument; | ||
|
||
/** | ||
* @author Jean-François Simon <[email protected]> | ||
*/ | ||
class InputArgumentTextDescriptor extends AbstractTextDescriptor | ||
{ | ||
/** | ||
* @var int|null | ||
*/ | ||
private $nameWidth; | ||
|
||
/** | ||
* @param string|null $nameWidth | ||
* @param bool $raw | ||
*/ | ||
public function __construct($nameWidth = null, $raw = false) | ||
{ | ||
$this->nameWidth = $nameWidth; | ||
parent::__construct($raw); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure(array $options) | ||
{ | ||
if (isset($options['name_width'])) { | ||
$this->nameWidth = $options['name_width']; | ||
} | ||
|
||
return parent::configure($options); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRawText($object) | ||
{ | ||
return strip_tags($this->getFormattedText($object)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFormattedText($object) | ||
{ | ||
/** @var InputArgument $object */ | ||
if (null !== $object->getDefault() && (!is_array($object->getDefault()) || count($object->getDefault()))) { | ||
$default = sprintf('<comment> (default: %s)</comment>', $this->formatDefaultValue($object->getDefault())); | ||
} else { | ||
$default = ''; | ||
} | ||
|
||
$nameWidth = $this->nameWidth ?: strlen($object->getName()); | ||
$description = str_replace("\n", "\n".str_repeat(' ', $nameWidth + 2), $object->getDescription()); | ||
|
||
return sprintf(" <info>%-${nameWidth}s</info> %s%s", $object->getName(), $description, $default); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($object) | ||
{ | ||
return $object instanceof InputArgument; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?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\Input\InputDefinition; | ||
|
||
/** | ||
* @author Jean-François Simon <[email protected]> | ||
*/ | ||
class InputDefinitionTextDescriptor extends AbstractTextDescriptor | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRawText($object) | ||
{ | ||
return strip_tags($this->getFormattedText($object)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFormattedText($object) | ||
{ | ||
// find the largest option or argument name | ||
$nameWidth = 0; | ||
|
||
/** @var InputDefinition $object */ | ||
foreach ($object->getOptions() as $option) { | ||
$nameLength = strlen($option->getName()) + 2; | ||
if ($option->getShortcut()) { | ||
$nameLength += strlen($option->getShortcut()) + 3; | ||
} | ||
|
||
$nameWidth = max($nameWidth, $nameLength); | ||
} | ||
|
||
foreach ($object->getArguments() as $argument) { | ||
$nameWidth = max($nameWidth, strlen($argument->getName())); | ||
} | ||
|
||
++$nameWidth; | ||
|
||
$text = array(); | ||
|
||
if ($object->getArguments()) { | ||
$text[] = '<comment>Arguments:</comment>'; | ||
$descriptor = new InputArgumentTextDescriptor($nameWidth); | ||
foreach ($object->getArguments() as $argument) { | ||
$text[] = $descriptor->getFormattedText($argument); | ||
} | ||
$text[] = ''; | ||
} | ||
|
||
if ($object->getOptions()) { | ||
$text[] = '<comment>Options:</comment>'; | ||
$descriptor = new InputOptionTextDescriptor($nameWidth); | ||
foreach ($object->getOptions() as $option) { | ||
$text[] = $descriptor->getFormattedText($option); | ||
} | ||
$text[] = ''; | ||
} | ||
|
||
return implode("\n", $text); | ||
} | ||
|
||
/** | ||
* {@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.
we have standardize to use
sprintf
for exception messages.