Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console-core / src / Utils / RequirementChecker.php
1 <?php
2
3 namespace Drupal\Console\Core\Utils;
4
5 use Symfony\Component\Yaml\Parser;
6
7 /**
8  * Class RequirementChecker
9  *
10  * @package Drupal\Console\Core\Utils
11  */
12 class RequirementChecker
13 {
14     /**
15      * @var Parser
16      */
17     protected $parser;
18
19     /**
20      * @var array
21      */
22     protected $requirements = [];
23
24     /**
25      * @var array
26      */
27     protected $checkResult = [];
28
29     /**
30      * @var bool
31      */
32     protected $valid = true;
33
34     /**
35      * @var bool
36      */
37     protected $overwritten = false;
38
39     /**
40      * RequirementChecker constructor.
41      */
42     public function __construct()
43     {
44         $this->parser = new Parser();
45     }
46
47     /**
48      *
49      */
50     private function checkPHPVersion()
51     {
52         $requiredPHP = $this->requirements['requirements']['php']['required'];
53         $currentPHP = phpversion();
54         $this->checkResult['php']['required'] = $requiredPHP;
55         $this->checkResult['php']['current'] = $currentPHP;
56         $this->valid = (version_compare($currentPHP, $requiredPHP) >= 0);
57         $this->checkResult['php']['valid'] = $this->valid;
58     }
59
60     /**
61      * checkRequiredExtensions
62      */
63     private function checkRequiredExtensions()
64     {
65         $this->checkResult['extensions']['required']['missing'] = [];
66         foreach ($this->requirements['requirements']['extensions']['required'] as $extension) {
67             if (!extension_loaded($extension)) {
68                 $this->checkResult['extensions']['required']['missing'][] = $extension;
69                 $this->valid = false;
70             }
71         }
72     }
73
74     /**
75      * checkRecommendedExtensions
76      */
77     private function checkRecommendedExtensions()
78     {
79         $this->checkResult['extensions']['recommended']['missing'] = [];
80         foreach ($this->requirements['requirements']['extensions']['recommended'] as $extension) {
81             if (!extension_loaded($extension)) {
82                 $this->checkResult['extensions']['recommended']['missing'][] = $extension;
83             }
84         }
85     }
86
87     /**
88      * checkRequiredConfigurations
89      */
90     private function checkRequiredConfigurations()
91     {
92         $this->checkResult['configurations']['required']['overwritten']  = [];
93         $this->checkResult['configurations']['required']['missing']  = [];
94         foreach ($this->requirements['requirements']['configurations']['required'] as $configuration) {
95             $defaultValue = null;
96             if (is_array($configuration)) {
97                 $defaultValue = current($configuration);
98                 $configuration = key($configuration);
99             }
100
101             if (!ini_get($configuration)) {
102                 if ($defaultValue) {
103                     ini_set($configuration, $defaultValue);
104                     $this->checkResult['configurations']['required']['overwritten'] = [
105                         $configuration => $defaultValue
106                     ];
107                     $this->overwritten = true;
108                     continue;
109                 }
110                 $this->valid = false;
111                 $this->checkResult['configurations']['required']['missing'][] = $configuration;
112             }
113         }
114     }
115
116     /**
117      * @param $file
118      * @return array
119      */
120     public function validate($file)
121     {
122         if (file_exists($file)) {
123             $this->requirements = $this->parser->parse(file_get_contents($file));
124         }
125
126         if (!$this->checkResult) {
127             $this->checkPHPVersion();
128             $this->checkRequiredExtensions();
129             $this->checkRecommendedExtensions();
130             $this->checkRequiredConfigurations();
131         }
132
133         return $this->checkResult;
134     }
135
136     /**
137      * @return array
138      */
139     public function getCheckResult()
140     {
141         return $this->checkResult;
142     }
143
144     /**
145      * @return boolean
146      */
147     public function isOverwritten()
148     {
149         return $this->overwritten;
150     }
151
152     /**
153      * @return bool
154      */
155     public function isValid()
156     {
157         return $this->valid;
158     }
159 }