Skip to content

Commit

Permalink
build: Update the RequirementChecker (#1386)
Browse files Browse the repository at this point in the history
Update the RequirementChecker

Co-authored-by: theofidry <[email protected]>
  • Loading branch information
github-actions[bot] and theofidry authored Jun 17, 2024
1 parent 4e616f7 commit 1d74277
Show file tree
Hide file tree
Showing 30 changed files with 284 additions and 313 deletions.
12 changes: 6 additions & 6 deletions res/requirement-checker/bin/check-requirements.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php

namespace HumbugBox451\KevinGH\RequirementChecker;
namespace HumbugBox462\KevinGH\RequirementChecker;

if (isset($_SERVER['BOX_REQUIREMENT_CHECKER'])) {
$enableRequirementChecker = $_SERVER['BOX_REQUIREMENT_CHECKER'];
if (\is_bool($enableRequirementChecker) && !$enableRequirementChecker) {
if (is_bool($enableRequirementChecker) && !$enableRequirementChecker) {
return;
}
if (\is_string($enableRequirementChecker) && \in_array(\strtolower($enableRequirementChecker), ['false', '0'], \true)) {
if (is_string($enableRequirementChecker) && in_array(strtolower($enableRequirementChecker), ['false', '0'], \true)) {
return;
}
if (!\is_bool($enableRequirementChecker) && !\is_string($enableRequirementChecker)) {
echo \PHP_EOL . 'Unhandled value type for "BOX_REQUIREMENT_CHECKER". Got "' . \gettype($enableRequirementChecker) . '". Proceeding with the requirement checks.' . \PHP_EOL;
if (!is_bool($enableRequirementChecker) && !is_string($enableRequirementChecker)) {
echo \PHP_EOL . 'Unhandled value type for "BOX_REQUIREMENT_CHECKER". Got "' . gettype($enableRequirementChecker) . '". Proceeding with the requirement checks.' . \PHP_EOL;
}
}
if (\false === \in_array(\PHP_SAPI, array('cli', 'phpdbg', 'embed', 'micro'), \true)) {
if (\false === in_array(\PHP_SAPI, array('cli', 'phpdbg', 'embed', 'micro'), \true)) {
echo \PHP_EOL . 'The application may only be invoked from a command line, got "' . \PHP_SAPI . '"' . \PHP_EOL;
exit(1);
}
Expand Down
13 changes: 6 additions & 7 deletions res/requirement-checker/src/Checker.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
<?php

declare (strict_types=1);
namespace HumbugBox451\KevinGH\RequirementChecker;
namespace HumbugBox462\KevinGH\RequirementChecker;

use InvalidArgumentException;
use function count;
use function sprintf;
/** @internal */
final class Checker
{
private static $requirementsConfig;
public static function checkRequirements() : bool
public static function checkRequirements(): bool
{
$requirements = self::retrieveRequirements();
$checkPassed = $requirements->evaluateRequirements();
$io = new IO();
self::printCheck($checkPassed, new Printer($io->getVerbosity(), $io->hasColorSupport()), $requirements);
return $checkPassed;
}
public static function printCheck($checkPassed, Printer $printer, RequirementCollection $requirements) : void
public static function printCheck($checkPassed, Printer $printer, RequirementCollection $requirements): void
{
if (\false === $checkPassed && IO::VERBOSITY_VERY_VERBOSE > $printer->getVerbosity()) {
$printer->setVerbosity(IO::VERBOSITY_VERY_VERBOSE);
Expand Down Expand Up @@ -75,19 +74,19 @@ public static function printCheck($checkPassed, Printer $printer, RequirementCol
}
$printer->printvln('', $verbosity);
}
private static function retrieveRequirements() : RequirementCollection
private static function retrieveRequirements(): RequirementCollection
{
if (null === self::$requirementsConfig) {
self::$requirementsConfig = __DIR__ . '/../.requirements.php';
}
$config = (require self::$requirementsConfig);
$config = require self::$requirementsConfig;
$requirements = new RequirementCollection();
foreach ($config as $constraint) {
$requirements->addRequirement(self::createCondition($constraint['type'], $constraint['condition']), $constraint['message'], $constraint['helpMessage']);
}
return $requirements;
}
private static function createCondition($type, $condition) : IsFulfilled
private static function createCondition($type, $condition): IsFulfilled
{
switch ($type) {
case 'php':
Expand Down
19 changes: 9 additions & 10 deletions res/requirement-checker/src/IO.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

declare (strict_types=1);
namespace HumbugBox451\KevinGH\RequirementChecker;
namespace HumbugBox462\KevinGH\RequirementChecker;

use function fstat;
use function function_exists;
Expand All @@ -16,7 +16,6 @@
use function stream_isatty;
use const DIRECTORY_SEPARATOR;
use const STDOUT;
/** @internal */
final class IO
{
public const VERBOSITY_QUIET = 16;
Expand All @@ -35,30 +34,30 @@ public function __construct()
$this->interactive = $this->checkInteractivity($shellVerbosity);
$this->colorSupport = $this->checkColorSupport();
}
public function isInteractive() : bool
public function isInteractive(): bool
{
return $this->interactive;
}
public function getVerbosity() : int
public function getVerbosity(): int
{
return $this->verbosity;
}
public function hasColorSupport() : bool
public function hasColorSupport(): bool
{
return $this->colorSupport;
}
public function hasParameter($values) : bool
public function hasParameter($values): bool
{
$values = (array) $values;
foreach ($values as $value) {
$regexp = sprintf('/\\s%s\\b/', str_replace(' ', '\\s+', preg_quote($value, '/')));
$regexp = sprintf('/\s%s\b/', str_replace(' ', '\s+', preg_quote($value, '/')));
if (1 === preg_match($regexp, $this->options)) {
return \true;
}
}
return \false;
}
private function checkInteractivity(int $shellVerbosity) : bool
private function checkInteractivity(int $shellVerbosity): bool
{
if (-1 === $shellVerbosity) {
return \false;
Expand All @@ -71,7 +70,7 @@ private function checkInteractivity(int $shellVerbosity) : bool
}
return \true;
}
private function configureVerbosity() : int
private function configureVerbosity(): int
{
switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) {
case -1:
Expand Down Expand Up @@ -105,7 +104,7 @@ private function configureVerbosity() : int
}
return $shellVerbosity;
}
private function checkColorSupport() : bool
private function checkColorSupport(): bool
{
if ($this->hasParameter(['--ansi'])) {
return \true;
Expand Down
5 changes: 2 additions & 3 deletions res/requirement-checker/src/IsExtensionConflictFulfilled.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

declare (strict_types=1);
namespace HumbugBox451\KevinGH\RequirementChecker;
namespace HumbugBox462\KevinGH\RequirementChecker;

use function extension_loaded;
/** @internal */
final class IsExtensionConflictFulfilled implements IsFulfilled
{
private $conflictingExtension;
public function __construct(string $requiredExtension)
{
$this->conflictingExtension = $requiredExtension;
}
public function __invoke() : bool
public function __invoke(): bool
{
return !extension_loaded($this->conflictingExtension);
}
Expand Down
5 changes: 2 additions & 3 deletions res/requirement-checker/src/IsExtensionFulfilled.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

declare (strict_types=1);
namespace HumbugBox451\KevinGH\RequirementChecker;
namespace HumbugBox462\KevinGH\RequirementChecker;

use function extension_loaded;
/** @internal */
final class IsExtensionFulfilled implements IsFulfilled
{
private $requiredExtension;
public function __construct(string $requiredExtension)
{
$this->requiredExtension = $requiredExtension;
}
public function __invoke() : bool
public function __invoke(): bool
{
return extension_loaded($this->requiredExtension);
}
Expand Down
5 changes: 2 additions & 3 deletions res/requirement-checker/src/IsFulfilled.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

declare (strict_types=1);
namespace HumbugBox451\KevinGH\RequirementChecker;
namespace HumbugBox462\KevinGH\RequirementChecker;

/** @internal */
interface IsFulfilled
{
public function __invoke() : bool;
public function __invoke(): bool;
}
7 changes: 3 additions & 4 deletions res/requirement-checker/src/IsPhpVersionFulfilled.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<?php

declare (strict_types=1);
namespace HumbugBox451\KevinGH\RequirementChecker;
namespace HumbugBox462\KevinGH\RequirementChecker;

use HumbugBox451\Composer\Semver\Semver;
use HumbugBox462\Composer\Semver\Semver;
use function sprintf;
use const PHP_MAJOR_VERSION;
use const PHP_MINOR_VERSION;
use const PHP_RELEASE_VERSION;
/** @internal */
final class IsPhpVersionFulfilled implements IsFulfilled
{
private $requiredPhpVersion;
public function __construct(string $requiredPhpVersion)
{
$this->requiredPhpVersion = $requiredPhpVersion;
}
public function __invoke() : bool
public function __invoke(): bool
{
return Semver::satisfies(sprintf('%d.%d.%d', PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION), $this->requiredPhpVersion);
}
Expand Down
23 changes: 11 additions & 12 deletions res/requirement-checker/src/Printer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

declare (strict_types=1);
namespace HumbugBox451\KevinGH\RequirementChecker;
namespace HumbugBox462\KevinGH\RequirementChecker;

use function array_shift;
use function count;
Expand All @@ -15,7 +15,6 @@
use function trim;
use function wordwrap;
use const PHP_EOL;
/** @internal */
final class Printer
{
private $styles = ['reset' => "\x1b[0m", 'red' => "\x1b[31m", 'green' => "\x1b[32m", 'yellow' => "\x1b[33m", 'title' => "\x1b[33m", 'error' => "\x1b[37;41m", 'success' => "\x1b[30;42m"];
Expand All @@ -32,15 +31,15 @@ public function __construct(int $verbosity, bool $supportColors, ?int $width = n
$this->supportColors = $supportColors;
$this->width = $width ?: 80;
}
public function getVerbosity() : int
public function getVerbosity(): int
{
return $this->verbosity;
}
public function setVerbosity($verbosity) : void
public function setVerbosity($verbosity): void
{
$this->verbosity = $verbosity;
}
public function title(string $title, int $verbosity, ?string $style = null) : void
public function title(string $title, int $verbosity, ?string $style = null): void
{
if (null === $style) {
$style = 'title';
Expand All @@ -50,14 +49,14 @@ public function title(string $title, int $verbosity, ?string $style = null) : vo
$this->printvln(str_repeat('=', min(strlen($title), $this->width)), $verbosity, $style);
$this->printvln('', $verbosity, $style);
}
public function getRequirementErrorMessage(Requirement $requirement) : ?string
public function getRequirementErrorMessage(Requirement $requirement): ?string
{
if ($requirement->isFulfilled()) {
return null;
}
return wordwrap($requirement->getHelpText(), $this->width - 3, PHP_EOL . ' ') . PHP_EOL;
}
public function block(string $title, string $message, int $verbosity, ?string $style = null) : void
public function block(string $title, string $message, int $verbosity, ?string $style = null): void
{
$prefix = ' [' . $title . '] ';
$lineLength = $this->width - strlen($prefix) - 1;
Expand All @@ -84,22 +83,22 @@ public function block(string $title, string $message, int $verbosity, ?string $s
$this->printv(str_repeat(' ', $this->width), $verbosity, $style);
$this->printvln('', $verbosity);
}
public function printvln(string $message, int $verbosity, ?string $style = null) : void
public function printvln(string $message, int $verbosity, ?string $style = null): void
{
$this->printv($message, $verbosity, $style);
$this->printv(PHP_EOL, $verbosity, null);
}
public function printv(string $message, int $verbosity, ?string $style = null) : void
public function printv(string $message, int $verbosity, ?string $style = null): void
{
if ($verbosity > $this->verbosity) {
return;
}
$message = wordwrap($message, $this->width);
$message = sprintf('%s%s%s', $this->supportColors && isset($this->styles[$style]) ? $this->styles[$style] : '', $message, $this->supportColors ? $this->styles['reset'] : '');
if ('1' === \getenv('BOX_REQUIREMENTS_CHECKER_LOG_TO_STDOUT')) {
$message = sprintf('%s%s%s', ($this->supportColors && isset($this->styles[$style])) ? $this->styles[$style] : '', $message, $this->supportColors ? $this->styles['reset'] : '');
if ('1' === getenv('BOX_REQUIREMENTS_CHECKER_LOG_TO_STDOUT')) {
echo $message;
} else {
\fwrite(\STDERR, $message);
fwrite(\STDERR, $message);
}
}
}
11 changes: 5 additions & 6 deletions res/requirement-checker/src/Requirement.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

declare (strict_types=1);
namespace HumbugBox451\KevinGH\RequirementChecker;
namespace HumbugBox462\KevinGH\RequirementChecker;

/** @internal */
final class Requirement
{
private $checkIsFulfilled;
Expand All @@ -16,22 +15,22 @@ public function __construct(IsFulfilled $checkIsFulfilled, string $testMessage,
$this->testMessage = $testMessage;
$this->helpText = $helpText;
}
public function isFulfilled() : bool
public function isFulfilled(): bool
{
if (!isset($this->fulfilled)) {
$this->fulfilled = $this->checkIsFulfilled->__invoke();
}
return $this->fulfilled;
}
public function getIsFullfilledChecker() : IsFulfilled
public function getIsFullfilledChecker(): IsFulfilled
{
return $this->checkIsFulfilled;
}
public function getTestMessage() : string
public function getTestMessage(): string
{
return $this->testMessage;
}
public function getHelpText() : string
public function getHelpText(): string
{
return $this->helpText;
}
Expand Down
Loading

0 comments on commit 1d74277

Please sign in to comment.