| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the puli/manager package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Bernhard Schussek <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Puli\Manager\Repository\Mapping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Puli\Manager\Api\Repository\PathConflict; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Puli\Manager\Api\Repository\PathMapping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Puli\Manager\Conflict\ModuleConflict; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Puli\Manager\Conflict\ModuleConflictDetector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Puli\Manager\Transaction\AtomicOperation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Webmozart\PathUtil\Path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Updates the resource path conflicts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @since  1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @author Bernhard Schussek <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class UpdateConflicts implements AtomicOperation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $repositoryPaths; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var ModuleConflictDetector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $conflictDetector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var ConflictCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private $conflicts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @var PathMappingCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     private $mappingsByResource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private $addedConflicts = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @var PathMapping[][] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     private $removedConflicts = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 63 |  |     public function __construct(array $repositoryPaths, ModuleConflictDetector $conflictDetector, ConflictCollection $conflicts, PathMappingCollection $mappingsByResource) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 63 |  |         $this->repositoryPaths = $repositoryPaths; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 63 |  |         $this->conflictDetector = $conflictDetector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 63 |  |         $this->conflicts = $conflicts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 63 |  |         $this->mappingsByResource = $mappingsByResource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 63 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 63 |  |     public function execute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 63 |  |         if (!$this->repositoryPaths) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             // Check all paths if none are passed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 21 |  |             $this->repositoryPaths = $this->conflicts->getRepositoryPaths(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         // Mark all as resolved | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 63 |  |         foreach ($this->repositoryPaths as $repositoryPath) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 80 | 54 | View Code Duplication |             if ($this->conflicts->has($repositoryPath)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 7 |  |                 $conflict = $this->conflicts->get($repositoryPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 7 |  |                 $this->conflicts->remove($repositoryPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 7 |  |                 $this->removedConflicts[$repositoryPath] = $conflict->getMappings(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 54 |  |                 $conflict->resolve(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 63 |  |         $moduleConflicts = $this->conflictDetector->detectConflicts($this->repositoryPaths); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 63 |  |         $this->deduplicateModuleConflicts($moduleConflicts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 63 |  |         foreach ($moduleConflicts as $moduleConflict) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 20 |  |             $repositoryPath = $moduleConflict->getConflictingToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 20 |  |             $conflict = new PathConflict($repositoryPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 20 |  |             foreach ($moduleConflict->getModuleNames() as $moduleName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 20 |  |                 $conflict->addMapping($this->mappingsByResource->get($repositoryPath, $moduleName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 20 |  |             $this->conflicts->add($conflict); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 20 |  |             $this->addedConflicts[] = $repositoryPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 63 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |     public function rollback() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |         foreach ($this->addedConflicts as $repositoryPath) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 111 | 2 | View Code Duplication |             if ($this->conflicts->has($repositoryPath)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |                 $conflict = $this->conflicts->get($repositoryPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |                 $conflict->resolve(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |                 $this->conflicts->remove($repositoryPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |         foreach ($this->removedConflicts as $repositoryPath => $conflictingMappings) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 2 |  |             $conflict = new PathConflict($repositoryPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |             $conflict->addMappings($conflictingMappings); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |             $this->conflicts->add($conflict); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @param ModuleConflict[] $moduleConflicts | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 128 | 63 |  |     private function deduplicateModuleConflicts(array &$moduleConflicts) | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 130 | 63 |  |         $indicesByPath = array(); | 
            
                                                        
            
                                    
            
            
                | 131 | 63 |  |         $indicesToRemove = array(); | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 133 | 63 |  |         foreach ($moduleConflicts as $index => $moduleConflict) { | 
            
                                                        
            
                                    
            
            
                | 134 | 20 |  |             $indicesByPath[$moduleConflict->getConflictingToken()] = $index; | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 137 | 63 |  |         foreach ($indicesByPath as $repositoryPath => $index) { | 
            
                                                        
            
                                    
            
            
                | 138 | 20 |  |             foreach ($indicesByPath as $otherPath => $otherIndex) { | 
            
                                                        
            
                                    
            
            
                | 139 | 20 |  |                 if ($otherPath !== $repositoryPath && Path::isBasePath($otherPath, $repositoryPath)) { | 
            
                                                        
            
                                    
            
            
                | 140 | 20 |  |                     $indicesToRemove[$index] = true; | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 145 | 63 |  |         foreach ($indicesToRemove as $index => $true) { | 
            
                                                        
            
                                    
            
            
                | 146 | 3 |  |             unset($moduleConflicts[$index]); | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |         // Reorganize indices | 
            
                                                        
            
                                    
            
            
                | 150 | 63 |  |         $moduleConflicts = array_values($moduleConflicts); | 
            
                                                        
            
                                    
            
            
                | 151 | 63 |  |     } | 
            
                                                        
            
                                    
            
            
                | 152 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |  | 
            
                        
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.