| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Sunnysideup\UpgradeToSilverstripe4\Api\SearchAndReplaceAPI; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Replaces a bunch of code snippets in preparation of the upgrade. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Controversial replacements will be replaced with a comment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * next to it so you can review replacements easily. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | class FixRequirements extends Task | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     protected $taskStep = 's30'; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     protected $debug = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     private $ignoreFolderArray = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         '.git', | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public function getTitle() | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         return 'Finds requirements (Requirements::) and fixes them to be exposed properly'; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function getDescription() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |         return ' | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             Finds Requirements:: instances and fixes them to be used properly for modules | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             - e.g. [vendorname] / [modulename] : location/for/my/script.js'; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function setIgnoreFolderArray($a) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->ignoreFolderArray = $a; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |         return $this; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function runActualTask($params = []): ?string | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         //replacement data patterns that will be searched for | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $replacementArray = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             'src' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                 'php' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                     'Requirements::javascript(' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                         'R' => '', | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                     'Requirements::css(' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                         'R' => '', | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                     'Requirements::themedCSS(' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                         'R' => '', | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         if ($this->debug) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             $this->mu()->colourPrint($replacementArray); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             //Start search machine from the module location. replace API | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             $textSearchMachine = new SearchAndReplaceAPI($moduleDir); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             $textSearchMachine->setIsReplacingEnabled(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             $textSearchMachine->addToIgnoreFolderArray($this->ignoreFolderArray); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             /*For all the different patterns listed in the replacement array | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             * iterate over them such that the $path would be 'src' and $patharray would be 'php' | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             * together making it ['src']['php'] | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             */ | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             foreach ($replacementArray as $path => $pathArray) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 $path = $moduleDir . '/' . $path ?: ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 $path = $this->mu()->checkIfPathExistsAndCleanItUp($path); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                 if (! file_exists($path)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                     $this->mu()->colourPrint("SKIPPING ${path} as it does not exist."); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                     $textSearchMachine->setSearchPath($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                     foreach ($pathArray as $extension => $extensionArray) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                         //setting extensions to search files within | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                         $textSearchMachine->setExtensions(explode('|', $extension)); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                         $this->mu()->colourPrint( | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                             "++++++++++++++++++++++++++++++++++++\n" . | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |                             "CHECKING\n" . | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                             "IN ${path}\n" . | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                             "FOR ${extension} FILES\n" . | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                             'BASE ' . $moduleDir . "\n" . | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                             "++++++++++++++++++++++++++++++++++++\n" | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |                         foreach ($extensionArray as $find => $findDetails) { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                             $replace = $findDetails['R'] ?? $find; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                             $caseSensitive = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                             $isStraightReplace = true; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |                             $replacementType = 'BASIC'; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |                             // REPLACMENT PATTERN! | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |                             //Requirements::javascript(moduledirfolder/bla); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |                             //Requirements::javascript(vpl: bla); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                             // $findWithPackageName = $find . strtolower($this->mu()->getPackageName()); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                             $vendorAndPackageFolderNameForInstall = | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                                 $this->mu()->getVendorAndPackageFolderNameForInstall(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                             foreach (['\'', '"'] as $quoteMark) { | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                                 $finalReplace = $find . $quoteMark . $vendorAndPackageFolderNameForInstall . ': '; | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                                 if (! $finalReplace && $finalReplace !== ' ') { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |                                     user_error(" | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |                                         no replace is specified, find is: ${find}. | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                                         We suggest setting your final replace to a single space | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                                         if you would like to replace with NOTHING. | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                                     "); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |                                 $finalFind = $find . $quoteMark; | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |                                 $this->mu()->colourPrint( | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                                     '    --- FIND: ' . $finalFind . "\n" . | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                                     '    --- REPLACE: ' . $finalReplace . "\n" | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |                                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |                                 $textSearchMachine->setSearchKey($finalFind, $caseSensitive, $replacementType); | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |                                 $textSearchMachine->setReplacementKey($finalReplace); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |                                 $textSearchMachine->startSearchAndReplace(); | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |                             } | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |                         } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |                         //fix double-ups | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |                         //fixes things like | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |                         //vendor/packagename: silverstripe/admin | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |                         //to | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |                         //silverstripe/admin: only | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |                         $isCaseSensitive = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |                         foreach (['cms', 'framework', 'siteconfig', 'reports'] as $ssModule) { | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |                             $finalFind = $vendorAndPackageFolderNameForInstall . ': silverstripe/' . $ssModule . ': '; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |                             $finalReplace = 'silverstripe/' . $ssModule . ': '; | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |                             $this->mu()->colourPrint( | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |                                 '    --- FIND: ' . $finalFind . "\n" . | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |                                 '    --- REPLACE: ' . $finalReplace . "\n" | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |                             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |                             $textSearchMachine->setSearchKey( | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |                                 $finalFind, | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |                                 $isCaseSensitive, | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |                                 'silverstripe/' . $ssModule . '/@@@@double-up@@@@' | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |                             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |                             $textSearchMachine->setReplacementKey($finalReplace); | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |                             $textSearchMachine->startSearchAndReplace(); | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |                         } | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |                         //SHOW TOTALS | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |                         $replacements = $textSearchMachine->showFormattedSearchTotals(); | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |                         if (! $replacements) { | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |                             //flush output anyway! | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |                             $this->mu()->colourPrint("No replacements for  ${extension}"); | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |                         } | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |                         $this->mu()->colourPrint($textSearchMachine->getOutput()); | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |         return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     protected function hasCommitAndPush() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  |         return true; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |  |