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