Issues (12)

src/Helpers/RequirementsChecker.php (2 issues)

1
<?php
2
3
namespace Turahe\LaravelInstaller\Helpers;
4
5
/**
6
 * Class RequirementsChecker.
7
 */
8
class RequirementsChecker
9
{
10
    /**
11
     * Minimum PHP Version Supported (Override is in installer.php config file).
12
     *
13
     * @var string
14
     */
15
    private string $_minPhpVersion = '7.4';
16
17
    /**
18
     * Check for the server requirements.
19
     *
20
     * @param array $requirements
21
     * @return array
22
     */
23
    public function check(array $requirements): array
24
    {
25
        $results = [];
26
27
        foreach ($requirements as $type => $requirement) {
28
            switch ($type) {
29
                // check php requirements
30
                case 'php':
31
                    foreach ($requirements[$type] as $requirement) {
0 ignored issues
show
Comprehensibility Bug introduced by
$requirement is overwriting a variable from outer foreach loop.
Loading history...
32
                        $results['requirements'][$type][$requirement] = true;
33
34
                        if (! extension_loaded($requirement)) {
35
                            $results['requirements'][$type][$requirement] = false;
36
37
                            $results['errors'] = true;
38
                        }
39
                    }
40
                    break;
41
                // check apache requirements
42
                case 'apache':
43
                    foreach ($requirements[$type] as $requirement) {
44
                        // if function doesn't exist we can't check apache modules
45
                        if (function_exists('apache_get_modules')) {
46
                            $results['requirements'][$type][$requirement] = true;
47
48
                            if (! in_array($requirement, apache_get_modules())) {
49
                                $results['requirements'][$type][$requirement] = false;
50
51
                                $results['errors'] = true;
52
                            }
53
                        }
54
                    }
55
                    break;
56
                default:
57
                    throw new \Exception('Unexpected value');
58
            }
59
        }
60
61
        return $results;
62
    }
63
64
    /**
65
     * Check PHP version requirement.
66
     *
67
     * @param null|string $minPhpVersion
68
     * @return array
69
     */
70
    public function checkPHPversion(string $minPhpVersion = null): array
71
    {
72
        $minVersionPhp = $minPhpVersion;
73
        $currentPhpVersion = $this->getPhpVersionInfo();
74
        $supported = false;
75
76
        if ($minPhpVersion == null) {
0 ignored issues
show
It seems like you are loosely comparing $minPhpVersion of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
77
            $minVersionPhp = $this->getMinPhpVersion();
78
        }
79
80
        if (version_compare($currentPhpVersion['version'], $minVersionPhp) >= 0) {
81
            $supported = true;
82
        }
83
84
        $phpStatus = [
85
            'full' => $currentPhpVersion['full'],
86
            'current' => $currentPhpVersion['version'],
87
            'minimum' => $minVersionPhp,
88
            'supported' => $supported,
89
        ];
90
91
        return $phpStatus;
92
    }
93
94
    /**
95
     * Get current Php version information.
96
     *
97
     * @return array
98
     */
99
    private static function getPhpVersionInfo(): array
100
    {
101
        $currentVersionFull = PHP_VERSION;
102
        preg_match("#^\d+(\.\d+)*#", $currentVersionFull, $filtered);
103
        $currentVersion = $filtered[0];
104
105
        return [
106
            'full' => $currentVersionFull,
107
            'version' => $currentVersion,
108
        ];
109
    }
110
111
    /**
112
     * Get minimum PHP version ID.
113
     *
114
     * @return string _minPhpVersion
115
     */
116
    protected function getMinPhpVersion()
117
    {
118
        return $this->_minPhpVersion;
119
    }
120
}
121