Completed
Push — master ( 740056...fe51da )
by Axel
06:24
created

RequirementChecker::getParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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
        self::loadParametersFromFile();
30
31
        // on install or upgrade, check if system requirements are met.
32
        if ((false === self::$parameters['installed'])
33
            || (!empty(self::$parameters[ZikulaKernel::CORE_INSTALLED_VERSION_PARAM])
34
                && version_compare(self::$parameters[ZikulaKernel::CORE_INSTALLED_VERSION_PARAM], ZikulaKernel::VERSION, '<'))) {
35
            $versionChecker = new ZikulaRequirements();
36
            $versionChecker->runSymfonyChecks(self::$parameters);
37
            if (empty($versionChecker->requirementsErrors)) {
38
                return;
39
            }
40
41
            // formatting for both HTML and CLI display
42
            if ('cli' !== PHP_SAPI) {
43
                echo '<html><body><pre>';
44
            }
45
            echo 'The following errors were discovered when checking the' . PHP_EOL . 'Zikula Core system/environment requirements:' . PHP_EOL;
46
            echo '******************************************************' . PHP_EOL . PHP_EOL;
47
            foreach ($versionChecker->requirementsErrors as $error) {
48
                echo $error . PHP_EOL;
49
            }
50
            if ('cli' !== PHP_SAPI) {
51
                echo '</pre></body></html>';
52
            }
53
            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...
54
        }
55
    }
56
57
    public static function getParameter($name)
58
    {
59
        self::loadParametersFromFile();
60
61
        return self::$parameters[$name];
62
    }
63
64
    private static function loadParametersFromFile(): void
65
    {
66
        if (is_array(self::$parameters)) {
67
            return;
68
        }
69
        $projectDir = dirname(__DIR__, 4); // should work when Bundle in vendor too
70
        $kernelConfig = Yaml::parse(file_get_contents(realpath($projectDir . '/config/services.yaml')));
71
        if (is_readable($file = $projectDir . '/config/services_custom.yaml')) {
72
            $kernelConfig = array_merge($kernelConfig, Yaml::parse(file_get_contents($file)));
73
        }
74
        $parameters = $kernelConfig['parameters'];
75
        $parameters['kernel.project_dir'] = $projectDir;
76
77
        self::$parameters = $parameters;
78
    }
79
}
80