-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
190 additions
and
607 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 7 additions & 28 deletions
35
fixtures/bench/with-compactors/res/requirement-checker/bin/check-requirements.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
@@ -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'); | ||
|
@@ -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': | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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 | ||
{ | ||
|
@@ -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: | ||
|
@@ -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); | ||
|
@@ -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; | ||
} | ||
} |
18 changes: 2 additions & 16 deletions
18
fixtures/bench/with-compactors/res/requirement-checker/src/IsExtensionConflictFulfilled.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
18 changes: 2 additions & 16 deletions
18
fixtures/bench/with-compactors/res/requirement-checker/src/IsExtensionFulfilled.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
Oops, something went wrong.