forked from php-pm/php-pm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessCommunicationTrait.php
More file actions
144 lines (123 loc) · 3.55 KB
/
ProcessCommunicationTrait.php
File metadata and controls
144 lines (123 loc) · 3.55 KB
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
<?php
namespace PHPPM;
use React\Socket\ConnectionInterface;
/**
* 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 ConnectionInterface $conn
* @param string $data
*
* @throws \Exception when invalid 'cmd' in $data.
*/
public function processMessage(ConnectionInterface $conn, $data)
{
$array = \json_decode($data, true);
$method = 'command' . \ucfirst($array['cmd']);
if (\is_callable([$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 ConnectionInterface $conn
*/
protected function bindProcessMessage(ConnectionInterface $conn)
{
$buffer = '';
$conn->on('data', 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($conn, $message);
}
}
$buffer = '';
}
});
}
/**
* Sends a message through $conn.
*
* @param ConnectionInterface $conn
* @param string $command
* @param array $message
*/
protected function sendMessage(ConnectionInterface $conn, $command, array $message = [])
{
$message['cmd'] = $command;
$conn->write(\json_encode($message) . PHP_EOL);
}
/**
*
* @param string $affix
* @param bool $overwrite
* @return string
*/
protected function getSockFile($affix, $overwrite)
{
//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 .= '/';
}
/* https://github.com/kalessil/phpinspectionsea/blob/master/docs/probable-bugs.md#mkdir-race-condition */
if (!\is_dir($run) && !\mkdir($run, 0777, true) && !\is_dir($run)) {
throw new \RuntimeException(\sprintf('Could not create %s folder.', $run));
}
$sock = $run. $affix . '.sock';
if ($overwrite && \file_exists($sock)) {
\unlink($sock);
}
return 'unix://' . $sock;
}
/**
* @param int $port
*
* @return string
*/
protected function getSlaveSocketPath($port, $overwrite = false)
{
return $this->getSockFile($port, $overwrite);
}
/**
* @param bool $overwrite
* @return string
*/
protected function getControllerSocketPath($overwrite = true)
{
return $this->getSockFile('controller', $overwrite);
}
/**
* @return string
*/
public function getSocketPath()
{
return $this->socketPath;
}
/**
* @param string $socketPath
*/
public function setSocketPath($socketPath)
{
$this->socketPath = $socketPath;
}
}