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
|
|
|
use Symplify\PHP7_Sculpin\Configuration\Configuration; |
19
|
|
|
|
20
|
|
|
final class HttpServer |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Configuration |
24
|
|
|
*/ |
25
|
|
|
private $configuration; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var StreamSelectLoop |
29
|
|
|
*/ |
30
|
|
|
private $streamSelectLoop; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ReactHttpServer |
34
|
|
|
*/ |
35
|
|
|
private $reactHttpServer; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
private $port; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ReactSocketServer |
44
|
|
|
*/ |
45
|
|
|
private $reactSocketServer; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var OutputInterface |
49
|
|
|
*/ |
50
|
|
|
private $output; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var ResponseWriter |
54
|
|
|
*/ |
55
|
|
|
private $responseWriter; |
56
|
|
|
|
57
|
5 |
|
public function __construct(Configuration $configuration, OutputInterface $output, ResponseWriter $responseWriter) |
58
|
|
|
{ |
59
|
5 |
|
$this->configuration = $configuration; |
60
|
5 |
|
$this->output = $output; |
61
|
5 |
|
$this->responseWriter = $responseWriter; |
62
|
5 |
|
} |
63
|
|
|
|
64
|
1 |
|
public function init(int $port = 8000) |
65
|
|
|
{ |
66
|
1 |
|
$this->setupDependencies(); |
67
|
|
|
|
68
|
1 |
|
$this->port = $port; |
69
|
|
|
|
70
|
1 |
|
$this->reactHttpServer->on('request', function (Request $request, Response $response) { |
71
|
|
|
$path = $this->configuration->getOutputDirectory() . '/' . ltrim(rawurldecode($request->getPath()), '/'); |
72
|
|
|
if (is_dir($path)) { |
73
|
|
|
$path .= '/index.html'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (! file_exists($path)) { |
77
|
|
|
$this->responseWriter->send404Response($request, $response); |
78
|
|
|
} else { |
79
|
|
|
$this->responseWriter->send200Response($request, $response, $path); |
80
|
|
|
} |
81
|
1 |
|
}); |
82
|
|
|
|
83
|
1 |
|
$this->reactSocketServer->listen($this->port, '0.0.0.0'); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
1 |
|
public function addPeriodicTimer(int $interval, callable $callback) |
87
|
|
|
{ |
88
|
1 |
|
$this->streamSelectLoop->addPeriodicTimer($interval, $callback); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
|
|
public function run() |
92
|
|
|
{ |
93
|
|
|
$this->output->writeln(sprintf( |
94
|
|
|
'Starting Sculpin at <info>http://localhost:%s</info>', |
95
|
|
|
$this->port |
96
|
|
|
)); |
97
|
|
|
$this->output->writeln('Quit the server with <info>CONTROL-C</info>.'); |
98
|
|
|
|
99
|
|
|
$this->streamSelectLoop->run(); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
private function setupDependencies() |
103
|
|
|
{ |
104
|
1 |
|
$this->streamSelectLoop = new StreamSelectLoop(); |
105
|
1 |
|
$this->reactSocketServer = new ReactSocketServer($this->streamSelectLoop); |
106
|
1 |
|
$this->reactHttpServer = new ReactHttpServer($this->reactSocketServer); |
107
|
1 |
|
} |
108
|
|
|
} |
109
|
|
|
|