| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | declare(ticks=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace Yiisoft\Yii\Debug\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Style\SymfonyStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yiisoft\VarDumper\VarDumper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yiisoft\Yii\Console\ExitCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Yiisoft\Yii\Debug\DebugServer\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | final class DebugServerBroadcastCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public const COMMAND_NAME = 'dev:broadcast'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected static $defaultName = self::COMMAND_NAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected static $defaultDescription = 'Runs PHP built-in web server'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function configure(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             ->setHelp( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 'Broadcasts a message to all connected clients.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ->addOption('message', 'm', InputOption::VALUE_OPTIONAL, 'A text to broadcast', 'Test message') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ->addOption('env', 'e', InputOption::VALUE_OPTIONAL, 'It is only used for testing.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     protected function execute(InputInterface $input, OutputInterface $output): int | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $io = new SymfonyStyle($input, $output); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $io->title('Yii3 Debug Server'); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $io->writeln('https://yiiframework.com' . "\n"); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $env = $input->getOption('env'); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         if ($env === 'test') { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             return ExitCode::OK; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $socket = Connection::create(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         if (\function_exists('pcntl_signal')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             $io->success('Quit the server with CTRL-C or COMMAND-C.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             \pcntl_signal(\SIGINT, static function () use ($socket): void { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 $socket->close(); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 exit(1); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             }); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $data = $input->getOption('message'); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $socket->broadcast(Connection::MESSAGE_TYPE_LOGGER, $data); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $socket->broadcast(Connection::MESSAGE_TYPE_VAR_DUMPER, VarDumper::create(['$data' => $data])->asJson(false)); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         return ExitCode::OK; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  | 
            
                        
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.