Completed
Pull Request — master (#4173)
by Craig
05:20
created

RequirementChecker::verify()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
nc 10
nop 0
dl 0
loc 23
rs 9.2222
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreInstallerBundle\Util;
15
16
use Symfony\Component\Yaml\Yaml;
17
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
18
19
class RequirementChecker
20
{
21
    private static $parameters;
22
23
    /**
24
     * If not installed, or if currentVersion != installedVersion run
25
     * requirement checks. Die on failure.
26
     */
27
    public static function verify(): void
28
    {
29
        if (version_compare($_ENV['ZIKULA_INSTALLED'], ZikulaKernel::VERSION, '<')) {
30
            self::loadParametersFromFile();
31
            $versionChecker = new ZikulaRequirements();
32
            $versionChecker->runSymfonyChecks(self::$parameters);
33
            if (empty($versionChecker->requirementsErrors)) {
34
                return;
35
            }
36
37
            // formatting for both HTML and CLI display
38
            if ('cli' !== PHP_SAPI) {
39
                echo '<html><body><pre>';
40
            }
41
            echo 'The following errors were discovered when checking the' . PHP_EOL . 'Zikula Core system/environment requirements:' . PHP_EOL;
42
            echo '******************************************************' . PHP_EOL . PHP_EOL;
43
            foreach ($versionChecker->requirementsErrors as $error) {
44
                echo $error . PHP_EOL;
45
            }
46
            if ('cli' !== PHP_SAPI) {
47
                echo '</pre></body></html>';
48
            }
49
            die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
50
        }
51
    }
52
53
    private static function loadParametersFromFile(): void
54
    {
55
        if (is_array(self::$parameters)) {
56
            return;
57
        }
58
        $projectDir = dirname(__DIR__, 4); // should work when Bundle in vendor too
59
        $kernelConfig = Yaml::parse(file_get_contents(realpath($projectDir . '/config/services.yaml')));
60
        if (is_readable($file = $projectDir . '/config/services_custom.yaml')) {
61
            $kernelConfig = array_merge($kernelConfig, Yaml::parse(file_get_contents($file)));
62
        }
63
        $parameters = $kernelConfig['parameters'];
64
        $parameters['kernel.project_dir'] = $projectDir;
65
66
        self::$parameters = $parameters;
67
    }
68
}
69