| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Licensed under The GPL-3.0 License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * For full copyright and license information, please see the LICENSE.txt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Redistributions of files must retain the above copyright notice. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @since    2.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author   Christopher Castro <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @link     http://www.quickappscms.org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license  http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace CMS\Core\Package\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use CMS\Core\Package\BasePackage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use CMS\Core\Package\Composer\Package\LinkConstraint\VersionConstraint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use CMS\Core\Package\Composer\Package\Version\VersionParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use CMS\Core\Package\PackageFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use CMS\Core\StaticCacheTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Checks that the given set of rules are meet. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class RuleChecker | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     use StaticCacheTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * List of rules to check. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $_rules = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * List of rules marked as FAIL. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     protected $_fail = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * List of rules marked as PASS. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     protected $_pass = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Whether rules were checked using check() or not. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     protected $_checked = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * ### Basic usage: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * $rules = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      *     'php' => '5.3.*', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      *     'quickapps/cms' => '2.*', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * $checker = new RuleChecker($rules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * if ($checker->check()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      *     // all OK | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      *     // ERROR, get failing rules: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      *     $checker->fail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * ### Without invoking check(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * You can also use `pass()` or `fail()` methods before invoking `check()` as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * in the example below. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * $checker = new RuleChecker($rules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * $pass = $checker->pass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * $fail = $checker->fail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * ### Providing packages as objects: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * $rules = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      *     new MyPackage('vendor/package', '/path/to/package') => '1.3.*', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      *     'quickapps/cms' => '2.*', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param array $rules A list of rules given as `package` => `constraints`, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      *  where the left hand side (package) can be either a string representing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      *  a package (as "vendor/package") or an object extending | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      *  \CMS\Core\Package\BasePackage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function __construct(array $rules) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         foreach ($rules as $lhs => $rhs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $this->_rules[] = new Rule($lhs, $rhs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Checks all the rules of this class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return bool True if all rules are meet | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     public function check() | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         $pass = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         foreach ($this->_rules as $rule) { | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             if ($rule->lhs() instanceof BasePackage) { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |                 $package = $rule->lhs(); | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |                 $package = PackageFactory::create((string)$rule->lhs()); | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             if (!$package->versionMatch($rule->rhs())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |                 $this->_fail($rule); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |                 $pass = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |                 $this->_pass($rule); | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         $this->_checked = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |         return $pass; | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * Gets all rules marked as PASS. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param bool $asString True will returns a comma separated string of rules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @return array|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 145 |  | View Code Duplication |     public function pass($asString = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         if (!$this->_checked) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             $this->check(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         if (!$asString) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             return $this->_pass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $items = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         foreach ($this->_pass as $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $items[] = "{$rule}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         return implode(', ', $items); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * Gets all rules marked as FAIL. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @param bool $asString True will returns a comma separated string of rules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * @return array|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 169 |  | View Code Duplication |     public function fail($asString = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         if (!$this->_checked) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             $this->check(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         if (!$asString) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             return $this->_fail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         $items = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         foreach ($this->_fail as $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             $items[] = "{$rule}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         return implode(', ', $items); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * Marks a rule as PASS. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * @param \CMS\Core\Package\Rule\Rule $rule The rule to mark | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     protected function _pass(Rule $rule) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         $this->_pass[] = $rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * Marks a rule as FAIL. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      * @param \CMS\Core\Package\Rule\Rule $rule The rule to mark | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     protected function _fail(Rule $rule) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         $this->_fail[] = $rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 208 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 209 |  |  |  |