| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Copyright © Vaimo Group. All rights reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * See LICENSE_VAIMO.txt for license details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace Vaimo\ComposerPatches\Factories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Vaimo\ComposerPatches\Config as PluginConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class SourcesResolverFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var \Composer\Composer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private $composer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param \Composer\Composer $composer | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         \Composer\Composer $composer | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         $this->composer = $composer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function create(PluginConfig $pluginConfig) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $patcherConfig = $pluginConfig->getPatcherConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $sourceConfig = $this->resolveSourceConfig($patcherConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $listSources = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             'project' => new \Vaimo\ComposerPatches\Sources\ProjectSource($this->composer->getPackage()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             'vendors' => new \Vaimo\ComposerPatches\Sources\VendorSource( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 isset($sourceConfig['vendors']) && is_array($sourceConfig['vendors']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                     ? $sourceConfig['vendors'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                     : array() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'packages' => new \Vaimo\ComposerPatches\Sources\PackageSource( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 isset($sourceConfig['packages']) && is_array($sourceConfig['packages']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     ? $sourceConfig['packages'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     : array() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return new \Vaimo\ComposerPatches\Patch\SourcesResolver( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             array_intersect_key( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 $listSources, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 array_filter($sourceConfig) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private function resolveSourceConfig($patcherConfig) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $sourceConfig = $patcherConfig[PluginConfig::PATCHER_SOURCES]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if (isset($sourceConfig['packages'], $sourceConfig['vendors'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             if (is_array($sourceConfig['packages']) && !is_array($sourceConfig['vendors'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $sourceConfig['vendors'] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             } elseif (is_array($sourceConfig['vendors']) && !is_array($sourceConfig['packages'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 $sourceConfig['packages'] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } elseif ($sourceConfig['packages'] === false || $sourceConfig['vendors'] === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $sourceConfig['packages'] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $sourceConfig['vendors'] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return $sourceConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  |