Completed
Pull Request — master (#4251)
by Axel
09:58 queued 04:33
created

RequirementChecker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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 - 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
    /**
22
     * @var array
23
     */
24
    private $parameters;
25
26
    /**
27
     * @var string
28
     */
29
    private $installedVersion;
30
31
    public function __construct(string $installed)
32
    {
33
        $this->installedVersion = $installed;
34
    }
35
36
    /**
37
     * If not installed, or if currentVersion != installedVersion run
38
     * requirement checks. Die on failure.
39
     */
40
    public function verify(): void
41
    {
42
        // on install or upgrade, check if system requirements are met.
43
        if (version_compare($this->installedVersion, ZikulaKernel::VERSION, '<')) {
44
            $this->loadParametersFromFile();
45
            $versionChecker = new ZikulaRequirements();
46
            $versionChecker->runSymfonyChecks($this->parameters);
47
            if (empty($versionChecker->requirementsErrors)) {
48
                return;
49
            }
50
51
            // formatting for both HTML and CLI display
52
            if ('cli' !== PHP_SAPI) {
53
                echo '<html><body><pre>';
54
            }
55
            echo 'The following errors were discovered when checking the' . PHP_EOL . 'Zikula Core system/environment requirements:' . PHP_EOL;
56
            echo '******************************************************' . PHP_EOL . PHP_EOL;
57
            foreach ($versionChecker->requirementsErrors as $error) {
58
                echo $error . PHP_EOL;
59
            }
60
            if ('cli' !== PHP_SAPI) {
61
                echo '</pre></body></html>';
62
            }
63
            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...
64
        }
65
    }
66
67
    public function getParameter($name)
68
    {
69
        $this->loadParametersFromFile();
70
71
        return $this->parameters[$name];
72
    }
73
74
    private function loadParametersFromFile(): void
75
    {
76
        if (is_array($this->parameters)) {
0 ignored issues
show
introduced by
The condition is_array($this->parameters) is always true.
Loading history...
77
            return;
78
        }
79
        $projectDir = dirname(__DIR__, 4); // should work when Bundle in vendor too
80
        $kernelConfig = Yaml::parse(file_get_contents(realpath($projectDir . '/config/services.yaml')));
81
        if (is_readable($file = $projectDir . '/config/services_custom.yaml')) {
82
            $kernelConfig = array_merge($kernelConfig, Yaml::parse(file_get_contents($file)));
83
        }
84
        $parameters = $kernelConfig['parameters'];
85
        $parameters['kernel.project_dir'] = $projectDir;
86
87
        $this->parameters = $parameters;
88
    }
89
}
90