Skip to content

Commit

Permalink
Make a Symfony 6 version of DrushStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Apr 1, 2022
1 parent 55c7acf commit 38c5883
Show file tree
Hide file tree
Showing 8 changed files with 570 additions and 1 deletion.
68 changes: 68 additions & 0 deletions src-symfony-compatibility/v6/Style/DrushStyle.php
@@ -0,0 +1,68 @@
<?php

namespace Drush\Style;

use Drush\Drush;
use Drush\Exceptions\UserAbortException;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;

class DrushStyle extends SymfonyStyle
{
public function confirm(string $question, bool $default = true): bool
{
// Automatically accept confirmations if the --yes argument was supplied.
if (Drush::affirmative()) {
$this->comment($question . ': yes.');
return true;
} elseif (Drush::negative()) {
// Automatically cancel confirmations if the --no argument was supplied.
$this->warning($question . ': no.');
return false;
}
return parent::confirm($question, $default);
}

public function choice(string $question, array $choices, mixed $default = null): mixed
{
// Display the choices without their keys.
$choices_values = array_values($choices);
$return = parent::choice($question, $choices_values, $default);

return array_search($return, $choices);
}

public function warning(string|array $message)
{
$this->block($message, 'WARNING', 'fg=black;bg=yellow', ' ! ', true);
}

public function note(string|array $message)
{
$this->block($message, 'NOTE', 'fg=black;bg=yellow', ' ! ');
}

public function caution(string|array $message)
{
$this->block($message, 'CAUTION', 'fg=black;bg=yellow', ' ! ', true);
}

/**
* @return mixed
*/
public function askRequired($question)
{
$question = new Question($question);
$question->setValidator(function (?string $value) {
// FALSE is not considered as empty value because question helper use
// it as negative answer on confirmation questions.
if ($value === null || $value === '') {
throw new \UnexpectedValueException('This value is required.');
}

return $value;
});

return $this->askQuestion($question);
}
}
31 changes: 31 additions & 0 deletions src-symfony-compatibility/v6/Symfony/BootstrapCompilerPass.php
@@ -0,0 +1,31 @@
<?php

namespace Drush\Symfony;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;

class BootstrapCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->has('bootstrap.manager')) {
return;
}

$definition = $container->findDefinition(
'bootstrap.manager'
);

$taggedServices = $container->findTaggedServiceIds(
'bootstrap.boot'
);
foreach ($taggedServices as $id => $tags) {
$definition->addMethodCall(
'add',
[new Reference($id)]
);
}
}
}
51 changes: 51 additions & 0 deletions src-symfony-compatibility/v6/Symfony/BufferedConsoleOutput.php
@@ -0,0 +1,51 @@
<?php

namespace Drush\Symfony;

use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;

/**
* BufferedConsoleOutput supports separation of the stdout and stderr streams.
*/
class BufferedConsoleOutput extends BufferedOutput implements ConsoleOutputInterface
{
protected $stderr;
private array $consoleSectionOutputs = [];

/**
* @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
* @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
* @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
*/
public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = false, OutputFormatterInterface $formatter = null)
{
parent::__construct($verbosity, $decorated, $formatter);

$this->stderr = new BufferedOutput($this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
}

/**
* {@inheritdoc}
*/
public function getErrorOutput(): BufferedOutput
{
return $this->stderr;
}

/**
* {@inheritdoc}
*/
public function setErrorOutput(OutputInterface $error): void
{
$this->stderr = $error;
}

public function section(): ConsoleSectionOutput
{
// @todo
}
}
16 changes: 16 additions & 0 deletions src-symfony-compatibility/v6/Symfony/DrushStyleInjector.php
@@ -0,0 +1,16 @@
<?php

namespace Drush\Symfony;

use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\CommandProcessor;
use Consolidation\AnnotatedCommand\ParameterInjector;
use Drush\Style\DrushStyle;

class DrushStyleInjector implements ParameterInjector
{
public function get(CommandData $commandData, $interfaceName): DrushStyle
{
return new DrushStyle($commandData->input(), $commandData->output());
}
}
@@ -0,0 +1,47 @@
<?php

namespace Drush\Symfony;

use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;

/**
* This is an InputDefinition that allows any option to be considered valid.
* Used when passing a command through to another dispatcher that will do
* the option validation.
*
* We use this instead of a LessStrictArgvInput in cases where we do not
* know in advance whether the input should be handled indiscriminately.
* In other words, an IndiscriminateInputDefinition is attached to individual
* Commands that should accept any option, whereas a LessStrictArgvInput
* should be used to make all command skip option validation.
*/
class IndiscriminateInputDefinition extends InputDefinition
{
/**
* @inheritdoc
*/
public function hasShortcut($name): bool
{
return true;
}

/**
* @inheritdoc
*/
public function hasOption($name): bool
{
return true;
}

/**
* @inheritdoc
*/
public function getOption($name): InputOption
{
if (parent::hasOption($name)) {
return parent::getOption($name);
}
return new InputOption($name, null, InputOption::VALUE_OPTIONAL, '', []);
}
}

0 comments on commit 38c5883

Please sign in to comment.