Skip to content

Commit

Permalink
Merge pull request #83 from alexpott/symfony-console-6-and-psr-log-3
Browse files Browse the repository at this point in the history
Allow symfony console 6 and psr log 3
  • Loading branch information
Chi-teck committed Mar 30, 2022
2 parents cd3912e + 51bf259 commit 90f6a87
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 24 deletions.
10 changes: 5 additions & 5 deletions composer.json
Expand Up @@ -29,8 +29,8 @@
"require": {
"php": ">=7.4",
"ext-json": "*",
"psr/log": "^1.1 || ^2.0",
"symfony/console": "^4.4.15 || ^5.1",
"psr/log": "^1.1 || ^2.0 || ^3.0",
"symfony/console": "^4.4.15 || ^5.1 || ^6.0",
"symfony/filesystem": "^4.4 || ^5.1 || ^6",
"symfony/polyfill-php80": "^1.23",
"symfony/string": "^5.1 || ^6",
Expand All @@ -39,12 +39,12 @@
"require-dev": {
"chi-teck/drupal-coder-extension": "^1.2",
"drupal/coder": "^8.3.14",
"friendsoftwig/twigcs": "^5.0",
"friendsoftwig/twigcs": "dev-master",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.4",
"squizlabs/php_codesniffer": "^3.5",
"symfony/var-dumper": "^5.2",
"symfony/yaml": "^5.2"
"symfony/var-dumper": "^5.2 || ^6.0",
"symfony/yaml": "^5.2 || ^6.0"
},
"conflict": {
"squizlabs/php_codesniffer": "<3.6"
Expand Down
2 changes: 2 additions & 0 deletions phpcs.xml
Expand Up @@ -10,6 +10,8 @@
<!-- Conflicts with PHP attributes. -->
<exclude name="Drupal.Commenting.InlineComment.DocBlock"/>
<exclude name="Drupal.Commenting.FunctionComment.Missing"/>
<!-- Conflicts with BC layer. -->
<exclude name="Drupal.Commenting.FileComment.Missing"/>
</rule>
<rule ref="vendor/drupal/coder/coder_sniffer/DrupalPractice">
<exclude name="Drupal.Commenting.VariableComment.Missing"/>
Expand Down
16 changes: 16 additions & 0 deletions src/Compatibility/AskQuestionTrait.php
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace DrupalCodeGenerator\Compatibility;

if (\PHP_VERSION_ID >= 80000) {
\class_alias(
'\DrupalCodeGenerator\Compatibility\Php8\AskQuestionTrait',
'\DrupalCodeGenerator\Compatibility\AskQuestionTrait'
);
}
else {
\class_alias(
'\DrupalCodeGenerator\Compatibility\Php7\AskQuestionTrait',
'\DrupalCodeGenerator\Compatibility\AskQuestionTrait'
);
}
21 changes: 21 additions & 0 deletions src/Compatibility/AskTrait.php
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

/**
* @file
* Compatibility shim.
*/

namespace DrupalCodeGenerator\Compatibility;

if (\PHP_VERSION_ID >= 80000) {
\class_alias(
'\DrupalCodeGenerator\Compatibility\Php8\AskTrait',
'\DrupalCodeGenerator\Compatibility\AskTrait'
);
}
else {
\class_alias(
'\DrupalCodeGenerator\Compatibility\Php7\AskTrait',
'\DrupalCodeGenerator\Compatibility\AskTrait'
);
}
21 changes: 21 additions & 0 deletions src/Compatibility/GeneratorStyleCompatibilityInterface.php
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

/**
* @file
* Compatibility shim.
*/

namespace DrupalCodeGenerator\Compatibility;

if (\PHP_VERSION_ID >= 80000) {
\class_alias(
'\DrupalCodeGenerator\Compatibility\Php8\GeneratorStyleInterface',
'\DrupalCodeGenerator\Compatibility\GeneratorStyleCompatibilityInterface'
);
}
else {
\class_alias(
'\DrupalCodeGenerator\Compatibility\Php7\GeneratorStyleInterface',
'\DrupalCodeGenerator\Compatibility\GeneratorStyleCompatibilityInterface'
);
}
30 changes: 30 additions & 0 deletions src/Compatibility/Php7/AskQuestionTrait.php
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace DrupalCodeGenerator\Compatibility\Php7;

use Symfony\Component\Console\Question\Question;

/**
* PHP 7 compatibility.
*/
trait AskQuestionTrait {

/**
* Asks a question.
*
* @return mixed
* The answer.
*/
public function askQuestion(Question $question) {
return $this->compatAskQuestion($question);
}

/**
* Asks a question.
*
* @return mixed
* The answer.
*/
abstract protected function compatAskQuestion(Question $question);

}
32 changes: 32 additions & 0 deletions src/Compatibility/Php7/AskTrait.php
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace DrupalCodeGenerator\Compatibility\Php7;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

/**
* PHP 7 compatibility.
*/
trait AskTrait {

/**
* Asks a question.
*
* @return mixed
* The answer.
*/
public function ask(InputInterface $input, OutputInterface $output, Question $question) {
return $this->compatAsk($input, $output, $question);
}

/**
* Asks a question.
*
* @return mixed
* The answer.
*/
abstract protected function compatAsk(InputInterface $input, OutputInterface $output, Question $question);

}
20 changes: 20 additions & 0 deletions src/Compatibility/Php7/GeneratorStyleInterface.php
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace DrupalCodeGenerator\Compatibility\Php7;

use Symfony\Component\Console\Question\Question;

/**
* PHP 7 compatibility.
*/
interface GeneratorStyleInterface {

/**
* Asks a question to the user.
*
* @return mixed
* The answer.
*/
public function askQuestion(Question $question);

}
24 changes: 24 additions & 0 deletions src/Compatibility/Php8/AskQuestionTrait.php
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace DrupalCodeGenerator\Compatibility\Php8;

use Symfony\Component\Console\Question\Question;

/**
* PHP 8 compatibility.
*/
trait AskQuestionTrait {

public function askQuestion(Question $question): mixed {
return $this->compatAskQuestion($question);
}

/**
* Asks a question.
*
* @return mixed
* The answer.
*/
abstract protected function compatAskQuestion(Question $question);

}
26 changes: 26 additions & 0 deletions src/Compatibility/Php8/AskTrait.php
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace DrupalCodeGenerator\Compatibility\Php8;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

/**
* PHP 8 compatibility.
*/
trait AskTrait {

public function ask(InputInterface $input, OutputInterface $output, Question $question): mixed {
return $this->compatAsk($input, $output, $question);
}

/**
* Asks a question.
*
* @return mixed
* The answer.
*/
abstract protected function compatAsk(InputInterface $input, OutputInterface $output, Question $question);

}
20 changes: 20 additions & 0 deletions src/Compatibility/Php8/GeneratorStyleInterface.php
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace DrupalCodeGenerator\Compatibility\Php8;

use Symfony\Component\Console\Question\Question;

/**
* PHP 8 compatibility.
*/
interface GeneratorStyleInterface {

/**
* Asks a question to the user.
*
* @return mixed
* The answer.
*/
public function askQuestion(Question $question): mixed;

}
9 changes: 6 additions & 3 deletions src/Helper/QuestionHelper.php
Expand Up @@ -2,6 +2,7 @@

namespace DrupalCodeGenerator\Helper;

use DrupalCodeGenerator\Compatibility\AskTrait;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Helper\QuestionHelper as BaseQuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -15,6 +16,7 @@
* The QuestionHelper class provides helpers to interact with the user.
*/
class QuestionHelper extends BaseQuestionHelper {
use AskTrait;

/**
* Counter to match questions and answers.
Expand All @@ -24,7 +26,7 @@ class QuestionHelper extends BaseQuestionHelper {
/**
* {@inheritdoc}
*/
public function ask(InputInterface $input, OutputInterface $output, Question $question) {
protected function compatAsk(InputInterface $input, OutputInterface $output, Question $question) {

// Input is not supplied with 'answer' option when the generator was started
// from the Navigation command.
Expand Down Expand Up @@ -123,13 +125,14 @@ protected function writePrompt(OutputInterface $output, Question $question): voi
$output->write($question_text);

if ($question instanceof ChoiceQuestion) {
$max_width = \max(\array_map([$this, 'strlen'], \array_keys($question->getChoices())));
$func_name = \method_exists($this, 'width') ? 'width' : 'strlen';
$max_width = \max(\array_map([$this, $func_name], \array_keys($question->getChoices())));

$output->writeln('');
$messages = [];
$choices = $question->getChoices();
foreach ($choices as $key => $value) {
$width = $max_width - static::strlen((string) $key);
$width = $max_width - static::$func_name((string) $key);
$messages[] = ' [<info>' . \str_repeat(' ', $width) . $key . '</info>] ' . $value;
}
$output->writeln($messages);
Expand Down
13 changes: 10 additions & 3 deletions src/Style/GeneratorStyle.php
Expand Up @@ -2,6 +2,7 @@

namespace DrupalCodeGenerator\Style;

use DrupalCodeGenerator\Compatibility\AskQuestionTrait;
use DrupalCodeGenerator\Helper\QuestionHelper;
use DrupalCodeGenerator\Utils;
use Symfony\Component\Console\Helper\Helper;
Expand All @@ -15,6 +16,7 @@
* Output decorator for the DCG style guide.
*/
final class GeneratorStyle extends SymfonyStyle implements GeneratorStyleInterface {
use AskQuestionTrait;

/**
* Console input.
Expand Down Expand Up @@ -47,14 +49,19 @@ public function __construct(InputInterface $input, OutputInterface $output, Ques
public function title($message): void {
$this->writeln('');
$this->writeln(' ' . $message);
$length = Helper::strlenWithoutDecoration($this->getFormatter(), $message);
if (\method_exists('\Symfony\Component\Console\Helper\Helper', 'width')) {
$length = Helper::width(Helper::removeDecoration($this->getFormatter(), $message));
}
else {
$length = Helper::strlenWithoutDecoration($this->getFormatter(), $message);
}
$this->writeln(\sprintf('<fg=cyan;options=bold>%s</>', \str_repeat('–', $length + 2)));
}

/**
* {@inheritdoc}
*/
public function askQuestion(Question $question) {
protected function compatAskQuestion(Question $question) {
$answer = $this->questionHelper->ask($this->input, $this, $question);
if (\is_string($answer)) {
$answer = Utils::addSlashes($answer);
Expand Down Expand Up @@ -114,7 +121,7 @@ public function getOutput(): OutputInterface {
/**
* {@inheritdoc}
*/
public function getErrorStyle(): GeneratorStyleInterface {
public function getErrorStyle(): self {
return new self($this->input, $this->getErrorOutput(), $this->questionHelper);
}

Expand Down
12 changes: 2 additions & 10 deletions src/Style/GeneratorStyleInterface.php
Expand Up @@ -2,24 +2,16 @@

namespace DrupalCodeGenerator\Style;

use DrupalCodeGenerator\Compatibility\GeneratorStyleCompatibilityInterface;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\StyleInterface as SymfonyStyleInterface;

/**
* Output style helpers.
*/
interface GeneratorStyleInterface extends SymfonyStyleInterface, OutputInterface {

/**
* Asks a question to the user.
*
* @return mixed
* The answer.
*/
public function askQuestion(Question $question);
interface GeneratorStyleInterface extends SymfonyStyleInterface, OutputInterface, GeneratorStyleCompatibilityInterface {

/**
* Builds console table.
Expand Down
4 changes: 2 additions & 2 deletions tests/dcg/QuestionHelper.php
Expand Up @@ -15,9 +15,9 @@ final class QuestionHelper extends BaseHelper {
/**
* {@inheritdoc}
*/
public function ask(InputInterface $input, OutputInterface $output, Question $question) {
protected function compatAsk(InputInterface $input, OutputInterface $output, Question $question) {
$question->setAutocompleterValues(NULL);
$answer = parent::ask($input, $output, $question);
$answer = parent::compatAsk($input, $output, $question);
// Write line after each input to make test output more readable.
$output->writeln('');
return $answer;
Expand Down
2 changes: 1 addition & 1 deletion tests/dcg/TestLogger.php
Expand Up @@ -14,7 +14,7 @@ final class TestLogger extends AbstractLogger {
/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = []) {
public function log($level, $message, array $context = []): void {
$this->records[] = [
'level' => $level,
'message' => $message,
Expand Down

0 comments on commit 90f6a87

Please sign in to comment.