| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Sunnysideup\UpgradeToSilverstripe4\Api\SearchAndReplaceAPI; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class ChangeControllerInitToProtected extends Task | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     protected $taskStep = 's10'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $debug = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $extensionArray = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         'php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $findArray = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         '    public function init()', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         "\t" . 'public function init()', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         '    function init()', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         "\t" . 'function init()', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function getTitle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         return 'Change Controller::init function to protected'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function getDescription() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             Look for all init functions in Controllers (based on file name) and change to protected functions.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function setExtensionArray($a) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->extensionArray = $a; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     public function setFindArray($a) | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->findArray = $a; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function runActualTask($params = []): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $moduleDir = $this->mu()->findMyCodeDir($moduleDir); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             //Start search machine from the module location. replace API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $textSearchMachine = new SearchAndReplaceAPI($moduleDir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $textSearchMachine->setIsReplacingEnabled(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $textSearchMachine->setFileReplacementMaxCount(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $textSearchMachine->setFileNameMustContain('Controller'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $this->mu()->colourPrint("Checking ${moduleDir}"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $moduleDir = $this->mu()->checkIfPathExistsAndCleanItUp($moduleDir); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             if (! file_exists($moduleDir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 $this->mu()->colourPrint("SKIPPING ${moduleDir} as it does not exist."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $textSearchMachine->setSearchPath($moduleDir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $textSearchMachine->setExtensions($this->extensionArray); //setting extensions to search files within | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $this->mu()->colourPrint( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     "++++++++++++++++++++++++++++++++++++\n" . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     "CHECKING\n" . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     "IN ${moduleDir}\n" . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     'FOR ' . implode(',', $this->extensionArray) . " FILES\n" . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     'BASE ' . $moduleDir . "\n" . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     "++++++++++++++++++++++++++++++++++++\n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 foreach ($this->findArray as $finalFind) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     $caseSensitive = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     $replacementType = 'COMPLEX'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     $comment = 'Controller init functions are now protected  please check that is a controller.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     $finalReplace = '    protected function init()'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     $this->mu()->colourPrint( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                         '    --- FIND: ' . $finalFind . "\n" . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                         '    --- REPLACE: ' . $finalReplace . "\n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     $textSearchMachine->setSearchKey($finalFind, $caseSensitive, $replacementType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     $textSearchMachine->setReplacementKey($finalReplace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     $textSearchMachine->setComment($comment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     $textSearchMachine->startSearchAndReplace(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 //SHOW TOTALS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 $replacements = $textSearchMachine->showFormattedSearchTotals(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 if (! $replacements) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                     //flush output anyway! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     $this->mu()->colourPrint('No replacements for  ' . implode(',', $this->extensionArray)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $this->mu()->colourPrint($textSearchMachine->getOutput()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     protected function hasCommitAndPush() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  |