| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\ComposerJsonFixes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Updates the composer requirements to reflect the new version and package names | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * in the composer file of your module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class UpdateComposerRequirements extends Task | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $taskStep = 's20'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $package = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $newVersion = 'error'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $replacementPackage = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $isObsolete = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $isNew = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $replacementArray = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $changed = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         'silverstripe/recipe-cms', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $runCommit = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function getTitle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return 'Update composer.json requirements'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function getDescription() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             Change requirements in composer.json file from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ' . ($this->package ?: 'an Old Package') . ' to ' . ($this->getReplacementPackage() ?: 'a New Package') . ':' . ($this->newVersion ?: ' (and New Version)') . ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             For example, we upgrade silverstripe/framework requirement from 3 to 4. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             Any packages that are not specified will be set to "*".'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function runActualTask($params = []): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if (is_array($this->replacementArray) && count($this->replacementArray)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             foreach ($this->replacementArray as $replacementDetails) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 $this->package = $replacementDetails['package']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $this->newVersion = $replacementDetails['newVersion'] ?? 'error'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $this->isObsolete = $replacementDetails['isObsolete'] ?? false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $this->isNew = $replacementDetails['isNew'] ?? false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 $this->replacementPackage = $replacementDetails['replacementPackage'] ?? ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 $this->runActualTaskInner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->runActualTaskInner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->upgradeAllPackages(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->setCommitMessage('API:  upgrading composer requirements to latest versions ... '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     public function getReplacementPackage() | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         if (empty($this->replacementPackage)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             $newPackage = $this->package; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             $newPackage = $this->replacementPackage; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         return $newPackage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     protected function runActualTaskInner() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $package = $this->package; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         // it is possible to run without any changes ... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if ($package) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->changed[$package] = $package; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $this->runCommit = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $newVersion = $this->newVersion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $newPackage = $this->getReplacementPackage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             if ($this->isObsolete) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 $command = | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                     'if(isset($data["require"]["' . $package . '"])) { ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     . '    unset($data["require"]["' . $package . '"]);' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     . '}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $comment = 'removing the requirement for ' . $package; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } elseif ($this->isNew) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $command = '$data["require"]["' . $newPackage . '"] = "' . $newVersion . '"; '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $comment = 'add a NEW package: ' . $package . ' with ' . $newPackage . ':' . $newVersion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 $command = | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                     'if(isset($data["require"]["' . $package . '"])) { ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                     . '    unset($data["require"]["' . $package . '"]);' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     . '    $data["require"]["' . $newPackage . '"] = "' . $newVersion . '"; ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     . '}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $comment = 'replace the require for ' . $package . ' with ' . $newPackage . ':' . $newVersion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             ComposerJsonFixes::inst($this->mu())->UpdateJSONViaCommandLine( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $this->mu()->getGitRootDir(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 $command, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $comment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     protected function upgradeAllPackages() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $json = ComposerJsonFixes::inst($this->mu())->getJSON($this->mu()->getGitRootDir()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         foreach ($json['require'] as $package => $version) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             if (! isset($this->changed[$package])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 $json['require'][$package] = '*'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         ComposerJsonFixes::inst($this->mu())->setJSON($this->mu()->getGitRootDir(), $json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     protected function hasCommitAndPush() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return $this->runCommit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  |