Refactoring
This commit is contained in:
85
lib/composer/vendor/consolidation/annotated-command/src/Options/AlterOptionsCommandEvent.php
vendored
Normal file
85
lib/composer/vendor/consolidation/annotated-command/src/Options/AlterOptionsCommandEvent.php
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
namespace Consolidation\AnnotatedCommand\Options;
|
||||
|
||||
use Consolidation\AnnotatedCommand\AnnotatedCommand;
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\ConsoleEvents;
|
||||
use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
* AlterOptionsCommandEvent is a subscriber to the Command Event
|
||||
* that looks up any additional options (e.g. from an OPTION_HOOK)
|
||||
* that should be added to the command. Options need to be added
|
||||
* in two circumstances:
|
||||
*
|
||||
* 1. When 'help' for the command is called, so that the additional
|
||||
* command options may be listed in the command description.
|
||||
*
|
||||
* 2. When the command itself is called, so that option validation
|
||||
* may be done.
|
||||
*
|
||||
* We defer the addition of options until these times so that we
|
||||
* do not invoke the option hooks for every command on every run
|
||||
* of the program, and so that we do not need to defer the addition
|
||||
* of all of the application hooks until after all of the application
|
||||
* commands have been added. (Hooks may appear in the same command files
|
||||
* as command implementations; applications may support command file
|
||||
* plug-ins, and hooks may add options to commands defined in other
|
||||
* commandfiles.)
|
||||
*/
|
||||
class AlterOptionsCommandEvent implements EventSubscriberInterface
|
||||
{
|
||||
/** var Application */
|
||||
protected $application;
|
||||
|
||||
public function __construct(Application $application)
|
||||
{
|
||||
$this->application = $application;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ConsoleCommandEvent $event
|
||||
*/
|
||||
public function alterCommandOptions(ConsoleCommandEvent $event)
|
||||
{
|
||||
/* @var Command $command */
|
||||
$command = $event->getCommand();
|
||||
$input = $event->getInput();
|
||||
if ($command->getName() == 'help') {
|
||||
// Symfony 3.x prepares $input for us; Symfony 2.x, on the other
|
||||
// hand, passes it in prior to binding with the command definition,
|
||||
// so we have to go to a little extra work. It may be inadvisable
|
||||
// to do these steps for commands other than 'help'.
|
||||
if (!$input->hasArgument('command_name')) {
|
||||
$command->ignoreValidationErrors();
|
||||
$command->mergeApplicationDefinition();
|
||||
$input->bind($command->getDefinition());
|
||||
}
|
||||
|
||||
$nameOfCommandToDescribe = $event->getInput()->getArgument('command_name');
|
||||
$commandToDescribe = $this->application->find($nameOfCommandToDescribe);
|
||||
$this->findAndAddHookOptions($commandToDescribe);
|
||||
} else {
|
||||
$this->findAndAddHookOptions($command);
|
||||
}
|
||||
}
|
||||
|
||||
public function findAndAddHookOptions($command)
|
||||
{
|
||||
if (!$command instanceof AnnotatedCommand) {
|
||||
return;
|
||||
}
|
||||
$command->optionsHook();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @{@inheritdoc}
|
||||
*/
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [ConsoleEvents::COMMAND => 'alterCommandOptions'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace Consolidation\AnnotatedCommand\Options;
|
||||
|
||||
use Consolidation\AnnotatedCommand\Parser\CommandInfo;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
/**
|
||||
* Option providers can add options to commands based on the annotations
|
||||
* present in a command. For example, a command that specifies @fields
|
||||
* will automatically be given --format and --fields options.
|
||||
*
|
||||
* @see AnnotatedCommandFactory::addListener()
|
||||
* @see HookManager::addOptionHook()
|
||||
*/
|
||||
interface AutomaticOptionsProviderInterface
|
||||
{
|
||||
/**
|
||||
* @return InputOption[]
|
||||
*/
|
||||
public function automaticOptions(CommandInfo $commandInfo);
|
||||
}
|
||||
10
lib/composer/vendor/consolidation/annotated-command/src/Options/PrepareFormatter.php
vendored
Normal file
10
lib/composer/vendor/consolidation/annotated-command/src/Options/PrepareFormatter.php
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace Consolidation\AnnotatedCommand\Options;
|
||||
|
||||
use Consolidation\AnnotatedCommand\CommandData;
|
||||
use Consolidation\OutputFormatters\Options\FormatterOptions;
|
||||
|
||||
interface PrepareFormatter
|
||||
{
|
||||
public function prepare(CommandData $commandData, FormatterOptions $options);
|
||||
}
|
||||
69
lib/composer/vendor/consolidation/annotated-command/src/Options/PrepareTerminalWidthOption.php
vendored
Normal file
69
lib/composer/vendor/consolidation/annotated-command/src/Options/PrepareTerminalWidthOption.php
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace Consolidation\AnnotatedCommand\Options;
|
||||
|
||||
use Symfony\Component\Console\Application;
|
||||
use Consolidation\AnnotatedCommand\CommandData;
|
||||
use Consolidation\OutputFormatters\Options\FormatterOptions;
|
||||
|
||||
class PrepareTerminalWidthOption implements PrepareFormatter
|
||||
{
|
||||
/** var Application */
|
||||
protected $application;
|
||||
|
||||
/** var int */
|
||||
protected $defaultWidth;
|
||||
|
||||
/** var int */
|
||||
protected $maxWidth = PHP_INT_MAX;
|
||||
|
||||
/** var int */
|
||||
protected $minWidth = 0;
|
||||
|
||||
public function __construct($defaultWidth = 0)
|
||||
{
|
||||
$this->defaultWidth = $defaultWidth;
|
||||
}
|
||||
|
||||
public function setApplication(Application $application)
|
||||
{
|
||||
$this->application = $application;
|
||||
}
|
||||
|
||||
public function prepare(CommandData $commandData, FormatterOptions $options)
|
||||
{
|
||||
$width = $this->getTerminalWidth();
|
||||
if (!$width) {
|
||||
$width = $this->defaultWidth;
|
||||
}
|
||||
|
||||
// Enforce minimum and maximum widths
|
||||
$width = min($width, $this->getMaxWidth($commandData));
|
||||
$width = max($width, $this->getMinWidth($commandData));
|
||||
|
||||
$options->setWidth($width);
|
||||
}
|
||||
|
||||
protected function getTerminalWidth()
|
||||
{
|
||||
if (!$this->application) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$dimensions = $this->application->getTerminalDimensions();
|
||||
if ($dimensions[0] == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $dimensions[0];
|
||||
}
|
||||
|
||||
protected function getMaxWidth(CommandData $commandData)
|
||||
{
|
||||
return $this->maxWidth;
|
||||
}
|
||||
|
||||
protected function getMinWidth(CommandData $commandData)
|
||||
{
|
||||
return $this->minWidth;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user