Skip to content

Commit

Permalink
fix: Allow to use SymfonyStyle ask method if exists. (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiCO2k authored Oct 28, 2022
1 parent 5273a6d commit 8dc3953
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Helpers/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Termwind\Helpers;

use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\QuestionHelper as SymfonyQuestionHelper;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

Expand Down
25 changes: 24 additions & 1 deletion src/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Termwind;

use Symfony\Component\Console\Helper\QuestionHelper as SymfonyQuestionHelper;
use ReflectionClass;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\StreamableInputInterface;
use Symfony\Component\Console\Question\Question as SymfonyQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use Termwind\Helpers\QuestionHelper;

/**
Expand Down Expand Up @@ -53,6 +55,27 @@ public function ask(string $question): mixed
{
$html = (new HtmlRenderer)->parse($question)->toString();

$output = Termwind::getRenderer();

if ($output instanceof SymfonyStyle) {
$property = (new ReflectionClass(SymfonyStyle::class))
->getProperty('questionHelper');

$property->setAccessible(true);

$currentHelper = $property->isInitialized($output)
? $property->getValue($output)
: new SymfonyQuestionHelper();

$property->setValue($output, new QuestionHelper);

try {
return $output->ask($html);
} finally {
$property->setValue($output, $currentHelper);
}
}

return $this->helper->ask(
self::getStreamableInput(),
Termwind::getRenderer(),
Expand Down

0 comments on commit 8dc3953

Please sign in to comment.