| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of Symplify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace Symplify\PHP7_Sculpin\HttpServer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use React\EventLoop\StreamSelectLoop; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use React\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use React\Http\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use React\Http\Server as ReactHttpServer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use React\Socket\Server as ReactSocketServer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | final class HttpServer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $outputDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var StreamSelectLoop | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $streamSelectLoop; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var ReactHttpServer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $reactHttpServer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $port; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var ReactSocketServer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private $reactSocketServer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @var OutputInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     private $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @var ResponseWriter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private $responseWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |     public function __construct(string $outputDirectory, OutputInterface $output, ResponseWriter $responseWriter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |         $this->outputDirectory = $outputDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         $this->output = $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         $this->responseWriter = $responseWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |     public function init(int $port = 8000) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |         $this->setupDependencies(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         $this->port = $port; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |         $this->reactHttpServer->on('request', function (Request $request, Response $response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $path = $this->outputDirectory.'/'.ltrim(rawurldecode($request->getPath()), '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             if (is_dir($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $path .= '/index.html'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             if (!file_exists($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $this->responseWriter->send404Response($request, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $this->responseWriter->send200Response($request, $response, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |         $this->reactSocketServer->listen($this->port, '0.0.0.0'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |     public function addPeriodicTimer(int $interval, callable $callback) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |         $this->streamSelectLoop->addPeriodicTimer($interval, $callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->output->writeln(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'Starting Sculpin at <info>http://localhost:%s</info>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $this->port | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->output->writeln('Quit the server with <info>CONTROL-C</info>.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->streamSelectLoop->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |     private function setupDependencies() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |         $this->streamSelectLoop = new StreamSelectLoop(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         $this->reactSocketServer = new ReactSocketServer($this->streamSelectLoop); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |         $this->reactHttpServer = new ReactHttpServer($this->reactSocketServer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  |