forked from php-pm/php-pm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessCommunicationTrait.php
145 lines (125 loc) · 3.5 KB
/
ProcessCommunicationTrait.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace PHPPM;
use React\Socket\Connection;
/**
* Little trait used in ProcessManager and ProcessSlave to have a simple json process communication.
*/
trait ProcessCommunicationTrait
{
/**
* Path to socket folder.
*
* @var string
*/
protected $socketPath = '.ppm/run/';
/**
* Parses a received message. Redirects to the appropriate `command*` method.
*
* @param array $data
* @param Connection $conn
*
* @throws \Exception when invalid 'cmd' in $data.
*/
public function processMessage($data, Connection $conn)
{
$array = json_decode($data, true);
$method = 'command' . ucfirst($array['cmd']);
if (is_callable(array($this, $method))) {
$this->$method($array, $conn);
} else {
throw new \Exception(sprintf('Command %s not found. Got %s', $method, $data));
}
}
/**
* Binds data-listener to $conn and waits for incoming commands.
*
* @param Connection $conn
*/
protected function bindProcessMessage(Connection $conn)
{
$buffer = '';
$conn->on(
'data',
\Closure::bind(
function ($data) use ($conn, &$buffer) {
$buffer .= $data;
if (substr($buffer, -strlen(PHP_EOL)) === PHP_EOL) {
foreach (explode(PHP_EOL, $buffer) as $message) {
if ($message) {
$this->processMessage($message, $conn);
}
}
$buffer = '';
}
},
$this
)
);
}
/**
* Sends a message through $conn.
*
* @param Connection $conn
* @param string $command
* @param array $message
*/
protected function sendMessage(Connection $conn, $command, array $message = [])
{
$message['cmd'] = $command;
$conn->write(json_encode($message) . PHP_EOL);
}
/**
*
* @param string $affix
* @param bool $removeOld
* @return string
*/
protected function getSockFile($affix, $removeOld)
{
if (Utils::isWindows()) {
//we have no unix domain sockets support
return '127.0.0.1';
}
//since all commands set setcwd() we can make sure we are in the current application folder
if ('/' === substr($this->socketPath, 0, 1)) {
$run = $this->socketPath;
} else {
$run = getcwd() . '/' . $this->socketPath;
}
if ('/' !== substr($run, -1)) {
$run .= '/';
}
if (!is_dir($run) && !mkdir($run, 0777, true)) {
throw new \RuntimeException(sprintf('Could not create %s folder.', $run));
}
$sock = $run. $affix . '.sock';
if ($removeOld && file_exists($sock)) {
unlink($sock);
}
return 'unix://' . $sock;
}
/**
* @param int $port
*
* @return string
*/
protected function getNewSlaveSocket($port)
{
return $this->getSockFile($port, true);
}
/**
* @param bool $removeOld
* @return string
*/
protected function getNewControllerHost($removeOld = true)
{
return $this->getSockFile('controller', $removeOld);
}
/**
* @param string $socketPath
*/
public function setSocketPath($socketPath)
{
$this->socketPath = $socketPath;
}
}