These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | /** |
||
24 | * @var React\EventLoop\LoopInterface |
||
25 | */ |
||
26 | private $loop; |
||
27 | private $port; |
||
28 | |||
29 | 12 | public function __construct( |
|
30 | InputInterface $input, |
||
31 | OutputInterface $output, |
||
32 | QuestionHelper $questionHelper |
||
33 | ) |
||
34 | { |
||
35 | 12 | $this->input = $input; |
|
36 | 12 | $this->output = $output; |
|
37 | 12 | $this->questionHelper = $questionHelper; |
|
38 | 12 | } |
|
39 | |||
40 | 12 | public function start() |
|
41 | { |
||
42 | 12 | $this->loop = React\EventLoop\Factory::create(); |
|
43 | $server = new React\Http\Server($this->loop, function (ServerRequestInterface $request) { |
||
44 | try { |
||
45 | 4 | return $this->router($request); |
|
46 | } catch (Throwable $exception) { |
||
47 | Deployer::printException($this->output, $exception); |
||
48 | return new React\Http\Message\Response(500, ['Content-Type' => 'text/plain'], 'Master error: ' . $exception->getMessage()); |
||
49 | } |
||
50 | 12 | }); |
|
51 | 12 | $socket = new React\Socket\Server(0, $this->loop); |
|
52 | 12 | $server->listen($socket); |
|
53 | 12 | $address = $socket->getAddress(); |
|
54 | 12 | $this->port = parse_url($address, PHP_URL_PORT); |
|
55 | 12 | } |
|
56 | |||
57 | 4 | private function router(ServerRequestInterface $request): Response |
|
58 | { |
||
59 | 4 | $path = $request->getUri()->getPath(); |
|
60 | 4 | switch ($path) { |
|
61 | 4 | case '/load': |
|
62 | 4 | ['host' => $host] = json_decode($request->getBody(), true); |
|
63 | |||
64 | 4 | $host = getHost($host); |
|
65 | 4 | $config = json_encode($host->config()->persist()); |
|
66 | |||
67 | 4 | return new Response(200, ['Content-Type' => 'application/json'], $config); |
|
68 | |||
69 | 4 | case '/save': |
|
70 | 4 | ['host' => $host, 'config' => $config] = json_decode($request->getBody(), true); |
|
71 | |||
72 | 4 | $host = getHost($host); |
|
73 | 4 | $host->config()->update($config); |
|
74 | |||
75 | 4 | return new Response(200, ['Content-Type' => 'application/json'], 'true'); |
|
76 | |||
77 | 1 | case '/proxy': |
|
78 | 1 | ['host' => $host, 'func' => $func, 'arguments' => $arguments] = json_decode($request->getBody(), true); |
|
0 ignored issues
–
show
|
|||
79 | |||
80 | 1 | Context::push(new Context(getHost($host), $this->input, $this->output)); |
|
81 | 1 | $answer = call_user_func($func, ...$arguments); |
|
82 | 1 | Context::pop(); |
|
83 | |||
84 | 1 | return new Response(200, ['Content-Type' => 'application/json'], json_encode($answer)); |
|
85 | |||
86 | default: |
||
87 | throw new Exception('Server path not found: ' . $request->getUri()->getPath()); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | 4 | public function addPeriodicTimer($interval, $callback) |
|
92 | { |
||
93 | 4 | $this->loop->addPeriodicTimer($interval, $callback); |
|
94 | 4 | } |
|
95 | |||
96 | 4 | public function addTimer($interval, $callback) |
|
97 | { |
||
98 | 4 | $this->loop->addTimer($interval, $callback); |
|
99 | 4 | } |
|
100 | |||
101 | 4 | public function cancelTimer($timer) |
|
102 | { |
||
103 | 4 | $this->loop->cancelTimer($timer); |
|
104 | 4 | } |
|
105 | |||
106 | 4 | public function run() |
|
107 | { |
||
108 | 4 | $this->loop->run(); |
|
109 | 4 | } |
|
110 | |||
111 | 4 | public function stop() |
|
112 | { |
||
113 | 4 | $this->loop->stop(); |
|
114 | 4 | } |
|
115 | |||
116 | 4 | public function getPort(): int |
|
117 | { |
||
118 | 4 | return $this->port; |
|
119 | } |
||
120 | } |
||
121 |
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.