| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Support\XDebug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Models\PhpVersion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Models\Setting; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Porter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\Support\Console\DockerCompose\CliCommandFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class XDebug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** @var Porter */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $porter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** @var CliCommandFactory */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $dockerCompose; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 204 |  |     public function __construct(Porter $porter, CliCommandFactory $dockerCompose) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 204 |  |         $this->porter = $porter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 204 |  |         $this->dockerCompose = $dockerCompose; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 204 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 5 |  |     public function turnOn() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 5 |  |         Setting::updateOrCreate('use_xdebug', 'on'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 5 |  |         foreach (PhpVersion::active()->get() as $version) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 2 |  |             if (!$this->enable($version)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 // If enable fails it's because it was already enabled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |             $this->porter->softRestart($version->getFpmNameAttribute()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |     public function turnOff() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  |         Setting::updateOrCreate('use_xdebug', 'off'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |         foreach (PhpVersion::active()->get() as $version) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |             if (!$this->disable($version)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 // If disable fails it's because it was already disabled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |             $this->porter->softRestart($version->getFpmNameAttribute()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Move the ini file to .ini to enable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param PhpVersion $version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |     protected function enable(PhpVersion $version) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |         return $this->moveIniFile($version, 'xdebug.bak', 'xdebug.ini'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Move the ini file to .bak to disable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param PhpVersion $version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 2 |  |     protected function disable(PhpVersion $version) | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 2 |  |         return $this->moveIniFile($version, 'xdebug.ini', 'xdebug.bak'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * Move the ini file, If it fails to move, return false;. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param PhpVersion $version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param $from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 4 |  |     protected function moveIniFile(PhpVersion $version, $from, $to) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  |         $move = "mv /etc/php/{$version->version_number}/mods-available/{$from} /etc/php/{$version->version_number}/mods-available/{$to}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         // We wrap in a buffer because we don't want everything output to the screen. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         // If exit code !== 0, its because the move failed since the starting file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         // didn't exist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 4 |  |         ob_start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 4 |  |         $exitCode = $this->dockerCompose->execContainer($version->getFpmNameAttribute()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 4 |  |             ->append($move) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  |             ->interactive() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  |             ->perform(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 4 |  |         ob_end_clean(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 4 |  |         return !(bool) $exitCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  |