Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Nov 24, 2023
1 parent f5c7c59 commit 441d22b
Show file tree
Hide file tree
Showing 23 changed files with 190 additions and 607 deletions.
5 changes: 4 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
'bin/box',
'bin/generate_default_stub',
])
->exclude('build/dir018/var')
->exclude([
'bench',
'build/dir018/var',
])
->notName('*-phar-stub.php');

$overriddenRules = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,24 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace HumbugBox451\KevinGH\RequirementChecker;

use function gettype;
use function in_array;
use function is_bool;
use function is_string;
use const false;
use const PHP_EOL;
use const PHP_SAPI;
use const true;

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(mb_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, ['cli', 'phpdbg', 'embed', 'micro'], true)) {
echo PHP_EOL.'The application may only be invoked from a command line, got "'.PHP_SAPI.'"'.PHP_EOL;
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);
}
require __DIR__.'/../vendor/autoload.php';
require __DIR__ . '/../vendor/autoload.php';
if (!Checker::checkRequirements()) {
exit(1);
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,36 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

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

use InvalidArgumentException;
use function count;
use function sprintf;
use const false;
use const PHP_VERSION;

/** @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()) {
if (\false === $checkPassed && IO::VERBOSITY_VERY_VERBOSE > $printer->getVerbosity()) {
$printer->setVerbosity(IO::VERBOSITY_VERY_VERBOSE);
}
$verbosity = IO::VERBOSITY_VERY_VERBOSE;
$iniPath = $requirements->getPhpIniPath();
$printer->title('Box Requirements Checker', $verbosity);
$printer->printv('> Using PHP ', $verbosity);
$printer->printvln(PHP_VERSION, $verbosity, 'green');
$printer->printvln(\PHP_VERSION, $verbosity, 'green');
if ($iniPath) {
$printer->printvln('> PHP is using the following php.ini file:', $verbosity);
$printer->printvln(' '.$iniPath, $verbosity, 'green');
$printer->printvln(' ' . $iniPath, $verbosity, 'green');
} else {
$printer->printvln('> PHP is not using any php.ini file.', $verbosity, 'yellow');
}
Expand All @@ -62,18 +45,17 @@ public static function printCheck($checkPassed, Printer $printer, RequirementCol
foreach ($requirements->getRequirements() as $requirement) {
if ($errorMessage = $printer->getRequirementErrorMessage($requirement)) {
if (IO::VERBOSITY_DEBUG === $printer->getVerbosity()) {
$printer->printvln(''.$requirement->getTestMessage(), IO::VERBOSITY_DEBUG, 'red');
$printer->printvln('' . $requirement->getTestMessage(), IO::VERBOSITY_DEBUG, 'red');
$printer->printv(' ', IO::VERBOSITY_DEBUG);
$errorMessages[] = $errorMessage;
} else {
$printer->printv('E', $verbosity, 'red');
$errorMessages[] = $errorMessage;
}

continue;
}
if (IO::VERBOSITY_DEBUG === $printer->getVerbosity()) {
$printer->printvln(''.$requirement->getTestMessage(), IO::VERBOSITY_DEBUG, 'green');
$printer->printvln('' . $requirement->getTestMessage(), IO::VERBOSITY_DEBUG, 'green');
$printer->printv(' ', IO::VERBOSITY_DEBUG);
} else {
$printer->printv('.', $verbosity, 'green');
Expand All @@ -88,27 +70,24 @@ public static function printCheck($checkPassed, Printer $printer, RequirementCol
$printer->block('ERROR', 'Your system is not ready to run the application.', $verbosity, 'error');
$printer->title('Fix the following mandatory requirements:', $verbosity, 'red');
foreach ($errorMessages as $errorMessage) {
$printer->printv(' * '.$errorMessage, $verbosity);
$printer->printv(' * ' . $errorMessage, $verbosity);
}
}
$printer->printvln('', $verbosity);
}

private static function retrieveRequirements(): RequirementCollection
private static function retrieveRequirements() : RequirementCollection
{
if (null === self::$requirementsConfig) {
self::$requirementsConfig = __DIR__.'/../.requirements.php';
self::$requirementsConfig = __DIR__ . '/../.requirements.php';
}
$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
66 changes: 20 additions & 46 deletions fixtures/bench/with-compactors/res/requirement-checker/src/IO.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

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

use function fstat;
Expand All @@ -26,10 +15,7 @@
use function str_replace;
use function stream_isatty;
use const DIRECTORY_SEPARATOR;
use const false;
use const STDOUT;
use const true;

/** @internal */
final class IO
{
Expand All @@ -42,59 +28,50 @@ final class IO
private $verbosity = self::VERBOSITY_NORMAL;
private $colorSupport;
private $options;

public function __construct()
{
$this->options = implode(' ', $_SERVER['argv']);
$shellVerbosity = $this->configureVerbosity();
$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, '/')));
if (1 === preg_match($regexp, $this->options)) {
return true;
return \true;
}
}

return false;
return \false;
}

private function checkInteractivity(int $shellVerbosity): bool
private function checkInteractivity(int $shellVerbosity) : bool
{
if (-1 === $shellVerbosity) {
return false;
return \false;
}
if (true === $this->hasParameter(['--no-interaction', '-n'])) {
return false;
if (\true === $this->hasParameter(['--no-interaction', '-n'])) {
return \false;
}
if (function_exists('posix_isatty') && !@posix_isatty(STDOUT) && false === getenv('SHELL_INTERACTIVE')) {
return false;
if (function_exists('posix_isatty') && !@posix_isatty(STDOUT) && \false === getenv('SHELL_INTERACTIVE')) {
return \false;
}

return true;
return \true;
}

private function configureVerbosity(): int
private function configureVerbosity() : int
{
switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) {
case -1:
Expand Down Expand Up @@ -126,20 +103,18 @@ private function configureVerbosity(): int
$this->verbosity = self::VERBOSITY_VERBOSE;
$shellVerbosity = 1;
}

return $shellVerbosity;
}

private function checkColorSupport(): bool
private function checkColorSupport() : bool
{
if ($this->hasParameter(['--ansi'])) {
return true;
return \true;
}
if ($this->hasParameter(['--no-ansi'])) {
return false;
return \false;
}
if (DIRECTORY_SEPARATOR === '\\') {
return function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(STDOUT) || false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM');
return function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(STDOUT) || \false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM');
}
if (function_exists('stream_isatty')) {
return stream_isatty(STDOUT);
Expand All @@ -148,7 +123,6 @@ private function checkColorSupport(): bool
return posix_isatty(STDOUT);
}
$stat = fstat(STDOUT);

return $stat ? 0o20000 === ($stat['mode'] & 0o170000) : false;
return $stat ? 020000 === ($stat['mode'] & 0170000) : \false;
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

declare (strict_types=1);
namespace HumbugBox451\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
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

declare (strict_types=1);
namespace HumbugBox451\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
Loading

0 comments on commit 441d22b

Please sign in to comment.