| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Backpack\CRUD\app\Console\Commands\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Artisan; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Process\Exception\ProcessFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Process\Process; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | trait PrettyCommandOutput | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      * Run a SSH command. | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * @param  string  $command  The SSH command that needs to be run | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * @param  bool  $beforeNotice  Information for the user before the command is run | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @param  bool  $afterNotice  Information for the user after the command is run | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @return mixed Command-line output | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function executeProcess($command, $beforeNotice = false, $afterNotice = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->echo('info', $beforeNotice ? ' '.$beforeNotice : implode(' ', $command)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         // make sure the command is an array as per Symphony 4.3+ requirement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $command = is_string($command) ? explode(' ', $command) : $command; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $process = new Process($command, null, null, null, $this->option('timeout')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $process->run(function ($type, $buffer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             if (Process::ERR === $type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 $this->echo('comment', $buffer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 $this->echo('line', $buffer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         // executes after the command finishes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         if (! $process->isSuccessful()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             throw new ProcessFailedException($process); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if ($this->progressBar) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $this->progressBar->advance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if ($afterNotice) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |             $this->echo('info', $afterNotice); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * Run an artisan command. | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @param  string  $command  The artisan command to be run. | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @param  array  $arguments  Key-value array of arguments to the artisan command. | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @param  bool  $beforeNotice  Information for the user before the command is run | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @param  bool  $afterNotice  Information for the user after the command is run | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @return mixed Command-line output | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     public function executeArtisanProcess($command, $arguments = [], $beforeNotice = false, $afterNotice = false) | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $beforeNotice = $beforeNotice ? ' '.$beforeNotice : 'php artisan '.implode(' ', (array) $command).' '.implode(' ', $arguments); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $this->echo('info', $beforeNotice); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             Artisan::call($command, $arguments); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         } catch (Exception $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             throw new ProcessFailedException($e); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         if ($this->progressBar) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             $this->progressBar->advance(); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         if ($afterNotice) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             $this->echo('info', $afterNotice); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * Write text to the screen for the user to see. | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @param  string  $type  line, info, comment, question, error | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * @param  string  $content | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function echo($type, $content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if ($this->option('debug') == false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         // skip empty lines | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if (trim($content)) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |             $this->{$type}($content); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * Write a title inside a box. | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @param  string  $content | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     public function box($content) | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         for ($i = 0, $line = ''; $i < strlen($content); ++$i, $line .= '─'); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         $this->line(''); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         $this->info("┌───{$line}───┐"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         $this->info("│   $content   │"); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         $this->info("└───{$line}───┘"); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * Write a title inside a box. | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * @param  string  $content | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function note($content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  |         $this->line("│ $content"); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 122 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 123 |  |  |  |