| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | // +---------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | // | ThinkPHP [ WE CAN DO IT JUST THINK ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | // +---------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | // | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | // +---------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | // +---------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | // | Author: Slince <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | // +---------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | declare (strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace think\console\command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use think\console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use think\console\Input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use think\console\input\Option; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use think\console\Output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class RunServer extends Command | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     public function configure() | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         $this->setName('run') | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             ->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                 'host', | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |                 'H', | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |                 Option::VALUE_OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |                 'The host to server the application on', | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |                 '0.0.0.0' | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             ->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |                 'port', | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |                 'p', | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                 Option::VALUE_OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                 'The port to server the application on', | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |                 8000 | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             ->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |                 'root', | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |                 'r', | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |                 Option::VALUE_OPTIONAL, | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |                 'The document root of the application', | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |                 '' | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             ->setDescription('PHP Built-in Server for ThinkPHP'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function execute(Input $input, Output $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $host = $input->getOption('host'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $port = $input->getOption('port'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $root = $input->getOption('root'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if (empty($root)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $root = $this->app->getRootPath() . 'public'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $command = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             '%s -S %s:%d -t %s %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->getPhpBinary(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $host, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $port, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             escapeshellarg($root), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             escapeshellarg($root . DIRECTORY_SEPARATOR . 'router.php') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $output->writeln(sprintf('ThinkPHP Development server is started On <http://%s:%s/>', $host, $port)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $output->writeln(sprintf('You can exit with <info>`CTRL-C`</info>')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $output->writeln(sprintf('Document root is: %s', $root)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         passthru($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * 获取 PHP 路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     protected function getPhpBinary(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if (is_executable(PHP_BINARY)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             return PHP_BINARY; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $path = PHP_BINDIR . DIRECTORY_SEPARATOR . 'php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if (file_exists($path) && is_executable($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return 'php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  |