| 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\StreamWrapper\FileBuffer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use TQ\Vcs\Buffer\FileBufferInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | use TQ\Vcs\StreamWrapper\FileBuffer\Factory\CommitFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | use TQ\Vcs\StreamWrapper\FileBuffer\Factory\DefaultFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | use TQ\Vcs\StreamWrapper\FileBuffer\Factory\HeadFileFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | use TQ\Vcs\StreamWrapper\PathInformationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  * Resolves the file stream factory to use on a stream_open call | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  * @author     Stefan Gehrig <gehrigteqneers.de> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  * @category   TQ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  * @package    TQ_VCS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  * @subpackage VCS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * @copyright  Copyright (C) 2018 by TEQneers GmbH & Co. KG | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | class Factory implements FactoryInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * The list containing the possible factories | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @var \SplPriorityQueue | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     protected $factoryList; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * Returns a default factory | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * includes: | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * - CommitFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * - HeadFileFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * - DefaultFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return  Factory | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 116 |  |     public static function getDefault() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         /** @var $factory Factory */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 116 |  |         $factory    = new static(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 116 |  |         $factory->addFactory(new CommitFactory(), 100) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 116 |  |                 ->addFactory(new HeadFileFactory(), 80) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 116 |  |                 ->addFactory(new DefaultFactory(), -100); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 116 |  |         return $factory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * Creates a new factory resolver | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 119 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 119 |  |         $this->factoryList  = new \SplPriorityQueue(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 | 119 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * Adds a factory to the list of possible factories | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @param   FactoryInterface    $factory    The factory to add | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * @param   integer             $priority   The priority | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * @return  Factory                         The factory | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 119 |  |     public function addFactory(FactoryInterface $factory, $priority = 10) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 119 |  |         $this->factoryList->insert($factory, $priority); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 119 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * Returns the file stream factory to handle the requested path | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * @param   PathInformationInterface     $path   The path information | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * @param   string                       $mode   The mode used to open the path | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @return  Factory                              The file buffer factory to handle the path | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @throws  \RuntimeException                    If no factory is found to handle to the path | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 107 | 55 |  |     public function findFactory(PathInformationInterface $path, $mode) | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 | 55 |  |         $factoryList    = clone $this->factoryList; | 
            
                                                                        
                            
            
                                    
            
            
                | 110 | 55 |  |         foreach ($factoryList as $factory) { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |             /** @var $factory Factory */ | 
            
                                                                        
                            
            
                                    
            
            
                | 112 | 55 |  |             if ($factory->canHandle($path, $mode)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 | 55 |  |                 return $factory; | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 116 | 1 |  |         throw new \RuntimeException('No factory found to handle the requested path'); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      * Returns true if this factory can handle the requested path | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |      * @param   PathInformationInterface     $path   The path information | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |      * @param   string                       $mode   The mode used to open the file | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |      * @return  boolean                              True if this factory can handle the path | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     public function canHandle(PathInformationInterface $path, $mode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             $this->findFactory($path, $mode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } catch (\RuntimeException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * Returns the file stream to handle the requested path | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * @param   PathInformationInterface     $path   The path information | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * @param   string                       $mode   The mode used to open the path | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * @return  FileBufferInterface                  The file buffer to handle the path | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 52 |  |     public function createFileBuffer(PathInformationInterface $path, $mode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 52 |  |         $factory    = $this->findFactory($path, $mode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 52 |  |         return $factory->createFileBuffer($path, $mode); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 148 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |  |