| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Backpack\CRUD\app\Console\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Process\Process; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | class RequireDevTools extends Command | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     protected $progressBar; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * The name and signature of the console command. | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     protected $signature = 'backpack:require:devtools | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |                                 {--debug} : Show process output or not. Useful for debugging.'; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * The console command description. | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     protected $description = 'Require Backpack DevTools on dev.'; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * Execute the console command. | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @return mixed Command-line output | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function handle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->progressBar = $this->output->createProgressBar(15); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->progressBar->minSecondsBetweenRedraws(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->progressBar->maxSecondsBetweenRedraws(120); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->progressBar->setRedrawFrequency(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->progressBar->start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->info(' Requiring DevTools. Please wait...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->progressBar->advance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         // Check if auth exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $details = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $process = new Process(['composer', 'config', 'http-basic.backpackforlaravel.com']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $process->run(function ($type, $buffer) use (&$details) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             if ($type !== Process::ERR && $buffer !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $details = json_decode($buffer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             } elseif (File::exists('auth.json')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $details = json_decode(File::get('auth.json'), true)['http-basic']['backpackforlaravel.com'] ?? false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $this->progressBar->advance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         // Create an auth.json file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if (! $details) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $this->info(' Creating auth.json file with your authentication token'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $this->line(' (Find your access token details on https://backpackforlaravel.com/user/tokens)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $username = $this->ask('Access token username'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $password = $this->ask('Access token password'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $process = new Process(['composer', 'config', 'http-basic.backpackforlaravel.com', $username, $password]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $process->run(function ($type, $buffer) use ($username, $password) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 if ($type === Process::ERR) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     // Fallback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     $authFile = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                         'http-basic' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                             'backpackforlaravel.com' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                                 'username' => $username, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                                 'password' => $password, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     if (File::exists('auth.json')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                         $currentFile = json_decode(File::get('auth.json'), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                         if (! ($currentFile['http-basic']['backpackforlaravel.com'] ?? false)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                             $authFile = array_merge_recursive($authFile, $currentFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     File::put('auth.json', json_encode($authFile, JSON_PRETTY_PRINT)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $this->progressBar->advance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         // Check if repositories exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $details = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $process = new Process(['composer', 'config', 'repositories.backpack/devtools']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $process->run(function ($type, $buffer) use (&$details) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             if ($type !== Process::ERR && $buffer !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $details = json_decode($buffer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 $details = json_decode(File::get('composer.json'), true)['repositories']['backpack/devtools'] ?? false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->progressBar->advance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         // Create repositories | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (! $details) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $this->info(' Creating repositories entry in composer.json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $process = new Process(['composer', 'config', 'repositories.backpack/devtools', 'composer', 'https://repo.backpackforlaravel.com']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $process->run(function ($type, $buffer) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 if ($type === Process::ERR) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                     // Fallback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                     $composerJson = Str::of(File::get('composer.json')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                     $currentRepositories = json_decode($composerJson, true)['repositories'] ?? false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                     $repositories = Str::of(json_encode([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                         'repositories' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                             'backpack/devtools' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                                 'type' => 'composer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                                 'url' => 'https://repo.backpackforlaravel.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                     ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     $composerJson = $currentRepositories | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                         // Replace current repositories | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                         ? preg_replace('/"repositories":\s{\r?\n/', $repositories->match("/\{\n\s+([\s\S]+)\n\s{4}\}/m")->append(",\n"), $composerJson) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                         // Append to the end of the file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                         : preg_replace("/\r?\n}/", $repositories->replaceFirst('{', ','), $composerJson); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                     File::put('composer.json', $composerJson); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 $this->progressBar->advance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         // Require package | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $process = new Process(['composer', 'require', '--dev', '--with-all-dependencies', 'backpack/devtools']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $process->run(function ($type, $buffer) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             $this->progressBar->advance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         // Finish | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $this->progressBar->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $this->info(' DevTools is now required.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         // DevTools inside installer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $this->info(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $this->info(' Now running the DevTools installation command.'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 152 |  |  |         $this->call('backpack:devtools:install'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |  | 
            
                        
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.