forked from php-pm/php-pm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.php
67 lines (55 loc) · 1.59 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace PHPPM;
class Client
{
/**
* @var int
*/
protected $controllerPort = 5100;
/**
* @var \React\EventLoop\ExtEventLoop|\React\EventLoop\LibEventLoop|\React\EventLoop\LibEvLoop|\React\EventLoop\StreamSelectLoop
*/
protected $loop;
/**
* @var \React\Socket\Connection
*/
protected $connection;
public function __construct($controllerPort = 8080)
{
$this->controllerPort = $controllerPort;
$this->loop = \React\EventLoop\Factory::create();
}
/**
* @return \React\Socket\Connection
*/
protected function getConnection()
{
if ($this->connection) {
$this->connection->close();
unset($this->connection);
}
$client = stream_socket_client('tcp://127.0.0.1:5500');
$this->connection = new \React\Socket\Connection($client, $this->loop);
return $this->connection;
}
protected function request($command, $options, $callback)
{
$data['cmd'] = $command;
$data['options'] = $options;
$connection = $this->getConnection();
$result = '';
$connection->on('data', function($data) use ($result) {
$result .= $data;
});
$connection->on('close', function() use ($callback, $result) {
$callback($result);
});
$connection->write(json_encode($data));
}
public function getStatus(callable $callback)
{
$this->request('status', [], function($result) use ($callback) {
$callback(json_decode($result, true));
});
}
}