| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Support\Valet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Models\Setting; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Models\Site; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Porter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\Support\Console\Cli; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use App\Support\Console\ConsoleWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Support\Facades\Artisan; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Valet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** @var Porter */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $porter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var Cli */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $cli; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var ConsoleWriter */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $writer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var bool hasWarned */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $hasWarned; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected const HTTP_PORT = 80; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected const HTTPS_PORT = 443; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected const COMPAT_HTTP_PORT = 8008; // 8080 is needed by the node container at the moment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected const COMPAT_HTTPS_PORT = 8443; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 206 |  |     public function __construct(Porter $porter, Cli $cli, ConsoleWriter $writer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 206 |  |         $this->porter = $porter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 206 |  |         $this->cli = $cli; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 206 |  |         $this->writer = $writer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 206 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 2 |  |     public function turnOn() | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 2 |  |         if (setting('use_valet', 'off') === 'on') { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 1 |  |             $this->writer->info('Valet compatibility already complete'); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 1 |  |         $this->sudoWarning(); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |         Setting::updateOrCreate('valet', 'on'); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |         Setting::updateOrCreate('http_port', static::COMPAT_HTTP_PORT); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |         Setting::updateOrCreate('https_port', static::COMPAT_HTTPS_PORT); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 1 |  |         Artisan::call('dns:off'); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  |         $this->cli->execRealTime('valet start'); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 1 |  |         $this->porter->compose(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 1 |  |         $this->porter->restart(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         // Run through all the porter sites and set up a proxy though valet... | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         Site::all()->each(function (Site $site) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1 |  |             $this->addSite($site); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 1 |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 1 |  |         $this->writer->line('Completed setting up valet compatibility'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  |     public function turnOff() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |         if (setting('use_valet', 'off') === 'off') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |             $this->writer->info('Valet compatibility already off'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         $this->sudoWarning(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         // Run through all the porter sites and set up a proxy though valet... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         Site::all()->each(function (Site $site) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |             $this->removeSite($site); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |         Setting::updateOrCreate('valet', 'off'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         Setting::updateOrCreate('http_port', static::HTTP_PORT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         Setting::updateOrCreate('https_port', static::HTTPS_PORT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |         $this->cli->execRealTime('valet stop'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |         $this->cli->execRealTime('sudo brew services stop dnsmasq'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |         Artisan::call('dns:on'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         $this->porter->compose(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |         $this->porter->restart(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         $this->writer->line('Completed removing valet compatibility'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 3 |  |     public function addSite(Site $site) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 3 |  |         $this->sudoWarning(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 3 |  |         if ($this->isProxied($site)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |             $this->removeSite($site); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 3 |  |         $port = $site->secure ? static::COMPAT_HTTPS_PORT : static::COMPAT_HTTP_PORT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 3 |  |         $protocol = $site->secure ? 'https://' : 'http://'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 3 |  |         $this->cli->exec("valet proxy {$site->name} {$protocol}127.0.0.1:{$port}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 3 |  |         $this->writer->line("Added {$site->name} proxy for Valet"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 3 |  |     public function removeSite(Site $site) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 3 |  |         $this->sudoWarning(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 3 |  |         $this->cli->exec("valet unproxy {$site->name}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 3 |  |         $this->writer->line("Removed Valet proxy for {$site->name}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 5 |  |     public function listSites() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 5 |  |         $this->sudoWarning(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 5 |  |         return $this->cli->exec('valet proxies'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 4 |  |     public function isProxied(Site $site) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 4 |  |         return Str::contains($this->listSites(), $site->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 7 |  |     protected function sudoWarning() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 7 |  |         if ($this->hasWarned) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 5 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 7 |  |         $this->writer->info('Requires Sudo permissions for Valet'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 7 |  |         $this->hasWarned = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 7 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  |