Issues (224)

res/requirement-checker/src/Checker.php (1 issue)

Labels
Severity
1
<?php
2
3
declare (strict_types=1);
4
namespace HumbugBox451\KevinGH\RequirementChecker;
5
6
use InvalidArgumentException;
7
use function count;
8
use function sprintf;
9
/** @internal */
10
final class Checker
11
{
12
    private static $requirementsConfig;
13
    public static function checkRequirements() : bool
14
    {
15
        $requirements = self::retrieveRequirements();
16
        $checkPassed = $requirements->evaluateRequirements();
17
        $io = new IO();
18
        self::printCheck($checkPassed, new Printer($io->getVerbosity(), $io->hasColorSupport()), $requirements);
19
        return $checkPassed;
20
    }
21
    public static function printCheck($checkPassed, Printer $printer, RequirementCollection $requirements) : void
22
    {
23
        if (\false === $checkPassed && IO::VERBOSITY_VERY_VERBOSE > $printer->getVerbosity()) {
24
            $printer->setVerbosity(IO::VERBOSITY_VERY_VERBOSE);
25
        }
26
        $verbosity = IO::VERBOSITY_VERY_VERBOSE;
27
        $iniPath = $requirements->getPhpIniPath();
28
        $printer->title('Box Requirements Checker', $verbosity);
29
        $printer->printv('> Using PHP ', $verbosity);
30
        $printer->printvln(\PHP_VERSION, $verbosity, 'green');
31
        if ($iniPath) {
32
            $printer->printvln('> PHP is using the following php.ini file:', $verbosity);
33
            $printer->printvln('  ' . $iniPath, $verbosity, 'green');
0 ignored issues
show
Are you sure $iniPath of type array|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
            $printer->printvln('  ' . /** @scrutinizer ignore-type */ $iniPath, $verbosity, 'green');
Loading history...
34
        } else {
35
            $printer->printvln('> PHP is not using any php.ini file.', $verbosity, 'yellow');
36
        }
37
        $printer->printvln('', $verbosity);
38
        if (count($requirements) > 0) {
39
            $printer->printvln('> Checking Box requirements:', $verbosity);
40
            $printer->printv('  ', $verbosity);
41
        } else {
42
            $printer->printvln('> No requirements found.', $verbosity);
43
        }
44
        $errorMessages = [];
45
        foreach ($requirements->getRequirements() as $requirement) {
46
            if ($errorMessage = $printer->getRequirementErrorMessage($requirement)) {
47
                if (IO::VERBOSITY_DEBUG === $printer->getVerbosity()) {
48
                    $printer->printvln('✘ ' . $requirement->getTestMessage(), IO::VERBOSITY_DEBUG, 'red');
49
                    $printer->printv('  ', IO::VERBOSITY_DEBUG);
50
                    $errorMessages[] = $errorMessage;
51
                } else {
52
                    $printer->printv('E', $verbosity, 'red');
53
                    $errorMessages[] = $errorMessage;
54
                }
55
                continue;
56
            }
57
            if (IO::VERBOSITY_DEBUG === $printer->getVerbosity()) {
58
                $printer->printvln('✔ ' . $requirement->getTestMessage(), IO::VERBOSITY_DEBUG, 'green');
59
                $printer->printv('  ', IO::VERBOSITY_DEBUG);
60
            } else {
61
                $printer->printv('.', $verbosity, 'green');
62
            }
63
        }
64
        if (IO::VERBOSITY_DEBUG !== $printer->getVerbosity() && count($requirements) > 0) {
65
            $printer->printvln('', $verbosity);
66
        }
67
        if ($requirements->evaluateRequirements()) {
68
            $printer->block('OK', 'Your system is ready to run the application.', $verbosity, 'success');
69
        } else {
70
            $printer->block('ERROR', 'Your system is not ready to run the application.', $verbosity, 'error');
71
            $printer->title('Fix the following mandatory requirements:', $verbosity, 'red');
72
            foreach ($errorMessages as $errorMessage) {
73
                $printer->printv(' * ' . $errorMessage, $verbosity);
74
            }
75
        }
76
        $printer->printvln('', $verbosity);
77
    }
78
    private static function retrieveRequirements() : RequirementCollection
79
    {
80
        if (null === self::$requirementsConfig) {
81
            self::$requirementsConfig = __DIR__ . '/../.requirements.php';
82
        }
83
        $config = (require self::$requirementsConfig);
84
        $requirements = new RequirementCollection();
85
        foreach ($config as $constraint) {
86
            $requirements->addRequirement(self::createCondition($constraint['type'], $constraint['condition']), $constraint['message'], $constraint['helpMessage']);
87
        }
88
        return $requirements;
89
    }
90
    private static function createCondition($type, $condition) : IsFulfilled
91
    {
92
        switch ($type) {
93
            case 'php':
94
                return new IsPhpVersionFulfilled($condition);
95
            case 'extension':
96
                return new IsExtensionFulfilled($condition);
97
            case 'extension-conflict':
98
                return new IsExtensionConflictFulfilled($condition);
99
            default:
100
                throw new InvalidArgumentException(sprintf('Unknown requirement type "%s".', $type));
101
        }
102
    }
103
}
104