| 1 |  |  | <?php namespace Arcanedev\Composer\Entities\PackageTraits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Arcanedev\Composer\Entities\PluginState; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Composer\Package\RootPackageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Composer\Repository\RepositoryManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Trait     RepositoriesTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package  Arcanedev\Composer\Entities\PackageTraits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author   ARCANEDEV <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | trait RepositoriesTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      |  Properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var \Arcanedev\Composer\Utilities\Logger $logger */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var array $json */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      |  Getters & Setters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Get composer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return \Composer\Composer | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     abstract public function getComposer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      |  Main Functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Add a collection of repositories described by the given configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * to the given package and the global repository manager. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param  \Composer\Package\RootPackageInterface    $root | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param  \Arcanedev\Composer\Entities\PluginState  $state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private function addRepositories(RootPackageInterface $root, PluginState $state) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (isset($this->json['repositories'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $prepend      = $state->shouldPrependRepositories(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $repoManager  = $this->getComposer()->getRepositoryManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $repositories = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             foreach ($this->json['repositories'] as $repoJson) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $this->addRepository($repoManager, $repositories, $repoJson, $prepend); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             /** @var \Composer\Package\RootPackageInterface $unwrapped */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $unwrapped   = self::unwrapIfNeeded($root, 'setRepositories'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $mergedRepos = $prepend | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 ? array_merge($repositories, $root->getRepositories()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 : array_merge($root->getRepositories(), $repositories); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $unwrapped->setRepositories($mergedRepos); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /* ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      |  Other Functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      | ------------------------------------------------------------------------------------------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Add a repository to collection of repositories. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param  \Composer\Repository\RepositoryManager  $repoManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param  array                                   $repositories | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param  array                                   $repoJson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param  bool                                    $prepend | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     private function addRepository( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         RepositoryManager $repoManager, array &$repositories, $repoJson, $prepend | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if (isset($repoJson['type'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->logger->info( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $prepend ? "Prepending {$repoJson['type']} repository" : "Adding {$repoJson['type']} repository" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $repository = $repoManager->createRepository($repoJson['type'], $repoJson); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $prepend | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 ? $repoManager->prependRepository($repository) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 : $repoManager->addRepository($repository); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $repositories[] = $repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  |