| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Deployer\Executor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Deployer\Deployer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Deployer\Exception\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Deployer\Task\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use React; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use React\Http\Message\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Helper\QuestionHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use function Deployer\getHost; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class Server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $questionHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $loop; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $port; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 10 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         InputInterface $input, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         OutputInterface $output, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         QuestionHelper $questionHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 10 |  |         $this->input = $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 10 |  |         $this->output = $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 10 |  |         $this->questionHelper = $questionHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 10 |  |     public function start() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 10 |  |         $this->loop = React\EventLoop\Factory::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $server = new React\Http\Server($this->loop, function (ServerRequestInterface $request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 return $this->router($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             } catch (Throwable $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 Deployer::printException($this->output, $exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 return new React\Http\Message\Response(500, ['Content-Type' => 'text/plain'], 'Master error: ' . $exception->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 10 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 10 |  |         $socket = new React\Socket\Server(0, $this->loop); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 10 |  |         $server->listen($socket); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 10 |  |         $address = $socket->getAddress(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 10 |  |         $this->port = parse_url($address, PHP_URL_PORT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 10 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     private function router(ServerRequestInterface $request): Response | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         switch ($request->getUri()->getPath()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             case '/proxy': | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 $body = $request->getBody(); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 ['host' => $host, 'func' => $func, 'arguments' => $arguments] = json_decode($body, true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 Context::push(new Context(getHost($host), $this->input, $this->output)); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 $answer = call_user_func($func, ...$arguments); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 Context::pop(); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 return new Response(200, ['Content-Type' => 'application/json'], json_encode($answer)); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                 break; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             default: | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                 throw new Exception('Server path not found: ' . $request->getUri()->getPath()); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |     public function addPeriodicTimer($interval, $callback) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         $this->loop->addPeriodicTimer($interval, $callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |     public function run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         $this->loop->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |     public function stop() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         $this->loop->stop(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |     public function getPort(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         return $this->port; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                        
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.