| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Copyright (C) 2017 by TEQneers GmbH & Co. KG | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * in the Software without restriction, including without limitation the rights | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * furnished to do so, subject to the following conditions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * The above copyright notice and this permission notice shall be included in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * all copies or substantial portions of the Software. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * THE SOFTWARE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * Git Stream Wrapper for PHP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @category   TQ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @package    TQ_VCS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @subpackage VCS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @copyright  Copyright (C) 2018 by TEQneers GmbH & Co. KG | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | namespace TQ\Vcs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * A collection of methods used for interacting with the file system | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * @author     Stefan Gehrig <gehrigteqneers.de> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * @category   TQ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  * @package    TQ_VCS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  * @subpackage VCS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  * @copyright  Copyright (C) 2018 by TEQneers GmbH & Co. KG | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | class FileSystem | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * Normalizes the directory separator to / | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @param   string  $path       The path | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @return  string              The normalized path | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 197 |  |     public static function normalizeDirectorySeparator($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 197 |  |         return str_replace(array('\\', '/'), '/', $path); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * Bubbles up a path until $comparator returns true | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @param   string      $path              The path | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @param   \Closure    $comparator        The callback used inside when bubbling to determine a finding | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @return  string|null                    The path that is found or NULL otherwise | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 197 |  |     public static function bubble($path, \Closure $comparator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 197 |  |         $found   = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 197 |  |         $path    = self::normalizeDirectorySeparator($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 197 |  |         $drive  = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 197 |  |         if (preg_match('~^(\w:)(.+)~', $path, $parts)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $drive  = $parts[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $path   = $parts[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 197 |  |         $pathParts  = explode('/', $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 197 |  |         while (count($pathParts) > 0 && $found === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 197 |  |             $path   = implode('/', $pathParts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 197 |  |             if ($comparator($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 191 |  |                 $found  = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 197 |  |             array_pop($pathParts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 197 |  |         if ($drive && $found) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $found  = $drive.$found; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 197 |  |         return $found; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  | 
            
                        
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: