| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright (c) 2016-present Ganbaro Digital Ltd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * All rights reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Redistribution and use in source and binary forms, with or without | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * modification, are permitted provided that the following conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * are met: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  *   * Redistributions of source code must retain the above copyright | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  *     notice, this list of conditions and the following disclaimer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  *   * Redistributions in binary form must reproduce the above copyright | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  *     notice, this list of conditions and the following disclaimer in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  *     the documentation and/or other materials provided with the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *     distribution. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  *   * Neither the names of the copyright holders nor the names of his | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *     contributors may be used to endorse or promote products derived | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *     from this software without specific prior written permission. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * POSSIBILITY OF SUCH DAMAGE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @category  Libraries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * @package   OperatingSystem/OsType/ValueBuilders | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * @author    Stuart Herbert <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  * @link      http://code.ganbarodigital.com/php-operating-system | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | namespace GanbaroDigital\OperatingSystem\OsType\ValueBuilders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | use GanbaroDigital\Filesystem\Requirements\RequireReadableFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | use GanbaroDigital\OperatingSystem\OsType\Checks\HasEtcRedhatRelease; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | use GanbaroDigital\OperatingSystem\OsType\Values\CentOS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | use GanbaroDigital\OperatingSystem\OsType\Values\OsType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | class BuildTypeFromEtcRedhatRelease implements BuildTypeFromFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * use /etc/redhat-release (if it exists) to work out what flavour of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * RedHat Linux we are looking at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param  string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      *         path to the file to parse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return null|OsType | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      *         OsType if we can determine the operating system | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      *         null if we cannot | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function __invoke($path = '/etc/redhat-release') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return self::usingPath($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * use /etc/redhat-release (if it exists) to work out what flavour of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * RedHat Linux we are looking at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return null|OsType | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      *         OsType if we can determine the operating system | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      *         null if we cannot | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public static function usingDefaultPath() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return self::usingPath('/etc/redhat-release'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * use /etc/redhat-release (if it exists) to work out what flavour of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * RedHat Linux we are looking at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param  string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      *         path to the file to parse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return null|OsType | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      *         OsType if we can determine the operating system | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      *         null if we cannot | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     public static function usingPath($path) | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         // make sure we have the file! | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         if (!HasEtcRedhatRelease::check($path)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         // make sure the file is readable | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         RequireReadableFile::check($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         // what do we have? | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         $fileContents = file_get_contents($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         // do we have a match? | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         return self::matchContentsToType($fileContents); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * do we have a regex that matches the contents of our file? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param  string $fileContents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      *         the contents of the file to check | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @return null|OsType | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      *         OsType if we can determine the operating system | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      *         null if we cannot | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     private static function matchContentsToType($fileContents) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         foreach (self::$osTypes as $regex => $type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             if ($result = self::matchTypeToRegex($type, $regex, $fileContents)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * does the given regex match our file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param  string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      *         the OsType class to return if the regex matches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param  string $regex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      *         the regex to try | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param  string $fileContents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      *         the text that we apply the regex to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @return null|OsType | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      *         OsType if the regex matches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      *         null otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     private static function matchTypeToRegex($type, $regex, $fileContents) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $matches=[]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         if (!preg_match($regex, $fileContents, $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         // if we get here, we have a match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         /** @var OsType a type of operating system */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $osType = new $type($matches['version']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         return $osType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * a map of regexes to OsType classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     private static $osTypes = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         "|^CentOS release (?<version>\d+\.\d+)|" => CentOS::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         "|^CentOS Linux release (?<version>\d+\.\d+)|" => CentOS::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     ]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 164 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |  | 
            
                        
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.