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
|
11 |
|
public function __construct( |
27
|
|
|
InputInterface $input, |
28
|
|
|
OutputInterface $output, |
29
|
|
|
QuestionHelper $questionHelper |
30
|
|
|
) |
31
|
|
|
{ |
32
|
11 |
|
$this->input = $input; |
33
|
11 |
|
$this->output = $output; |
34
|
11 |
|
$this->questionHelper = $questionHelper; |
35
|
11 |
|
} |
36
|
|
|
|
37
|
11 |
|
public function start() |
38
|
|
|
{ |
39
|
11 |
|
$this->loop = React\EventLoop\Factory::create(); |
40
|
|
|
$server = new React\Http\Server($this->loop, function (ServerRequestInterface $request) { |
41
|
|
|
try { |
42
|
1 |
|
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
|
11 |
|
}); |
48
|
11 |
|
$socket = new React\Socket\Server(0, $this->loop); |
49
|
11 |
|
$server->listen($socket); |
50
|
11 |
|
$address = $socket->getAddress(); |
51
|
11 |
|
$this->port = parse_url($address, PHP_URL_PORT); |
52
|
11 |
|
} |
53
|
|
|
|
54
|
1 |
|
private function router(ServerRequestInterface $request): Response |
55
|
|
|
{ |
56
|
1 |
|
switch ($request->getUri()->getPath()) { |
57
|
1 |
|
case '/proxy': |
58
|
1 |
|
$body = $request->getBody(); |
59
|
1 |
|
['host' => $host, 'func' => $func, 'arguments' => $arguments] = json_decode($body, true); |
|
|
|
|
60
|
|
|
|
61
|
1 |
|
Context::push(new Context(getHost($host), $this->input, $this->output)); |
62
|
1 |
|
$answer = call_user_func($func, ...$arguments); |
63
|
1 |
|
Context::pop(); |
64
|
|
|
|
65
|
1 |
|
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
|
2 |
|
public function addPeriodicTimer($interval, $callback) |
74
|
|
|
{ |
75
|
2 |
|
$this->loop->addPeriodicTimer($interval, $callback); |
76
|
2 |
|
} |
77
|
|
|
|
78
|
2 |
|
public function run() |
79
|
|
|
{ |
80
|
2 |
|
$this->loop->run(); |
81
|
2 |
|
} |
82
|
|
|
|
83
|
2 |
|
public function stop() |
84
|
|
|
{ |
85
|
2 |
|
$this->loop->stop(); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
2 |
|
public function getPort(): int |
89
|
|
|
{ |
90
|
2 |
|
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.