| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Backpack\CRUD\app\Console\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class AddCustomRouteContent extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * The name and signature of the console command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $signature = 'backpack:add-custom-route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |                                 {code : HTML/PHP code that registers a route. Use either single quotes or double quotes. Never both. } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |                                 {--route-file=routes/backpack/custom.php : The file where the code should be added relative to the root of the project. }'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * The console command description. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $description = 'Add HTML/PHP code to the routes/backpack/custom.php file'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Create a new command instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Execute the console command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function handle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $routeFilePath = base_path($this->option('route-file')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if (! file_exists($routeFilePath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             if ($routeFilePath !== base_path($this->backpackCustomRouteFile)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 $this->info('The route file <fg=blue>'.$routeFilePath.'</> does not exist. Please create it first.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $createRouteFile = $this->confirm('The route file <fg=blue>'.$routeFilePath.'</> does not exist. Should we create it?', 'yes'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             if ($createRouteFile === 'yes') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $this->call('vendor:publish', ['--provider' => \Backpack\CRUD\BackpackServiceProvider::class, '--tag' => 'custom_routes']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 $this->info('The route file <fg=blue>'.$routeFilePath.'</> does not exist. Please create it first.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $code = $this->argument('code'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->progressBlock("Adding route to <fg=blue>$routeFilePath</>"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $originalContent = file($routeFilePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         // clean the content from comments etc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $cleanContent = $this->cleanContentArray($originalContent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         // if the content contains code, don't add it again. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (array_search($code, $cleanContent, true) !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $this->closeProgressBlock('Already existed', 'yellow'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         // get the last element of the array contains '}' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $lastLine = $this->getLastLineNumberThatContains('}', $cleanContent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if ($lastLine === false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $this->closeProgressBlock('Could not find the last line, file '.$routeFilePath.' may be corrupted.', 'red'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         // add the code to the line before the last line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         array_splice($originalContent, $lastLine, 0, '    '.$code.PHP_EOL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         // write the new content to the file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         if (file_put_contents($routeFilePath, implode('', $originalContent)) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $this->closeProgressBlock('Failed to add route. Failed writing the modified route file. Maybe check file permissions?', 'red'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->closeProgressBlock('done', 'green'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     private function cleanContentArray(array $content) | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         return array_filter(array_map(function ($line) { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             $lineText = trim($line); | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             if ($lineText === '' || | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                 $lineText === '\n' || | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                 $lineText === '\r' || | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                 $lineText === '\r\n' || | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |                 $lineText === PHP_EOL || | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |                 str_starts_with($lineText, '<?php') || | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                 str_starts_with($lineText, '?>') || | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                 str_starts_with($lineText, '//') || | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                 str_starts_with($lineText, '/*') || | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                 str_starts_with($lineText, '*/') || | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |                 str_ends_with($lineText, '*/') || | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |                 str_starts_with($lineText, '*') || | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                 str_starts_with($lineText, 'use ') || | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                 str_starts_with($lineText, 'return ') || | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |                 str_starts_with($lineText, 'namespace ')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |                 return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |             return $lineText; | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         }, $content)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * Parse the given file stream and return the line number where a string is found. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param  string  $needle  The string that's being searched for. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param  array  $haystack  The file where the search is being performed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @return bool|int The last line number where the string was found. Or false. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     private function getLastLineNumberThatContains($needle, $haystack) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $matchingLines = array_filter($haystack, function ($k) use ($needle) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             return strpos($k, $needle) !== false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         if ($matchingLines) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             return array_key_last($matchingLines); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  |