1
|
|
|
<?php |
2
|
|
|
namespace Laravoole\Wrapper; |
3
|
|
|
|
4
|
|
|
use Laravoole\Base; |
5
|
|
|
use Exception; |
6
|
|
|
use Workerman\Worker; |
7
|
|
|
|
8
|
|
|
abstract class Workerman extends Base implements ServerInterface |
9
|
|
|
{ |
10
|
|
|
protected $eventMapper = [ |
11
|
|
|
'WorkerStart' => 'onWorkerStart', |
12
|
|
|
'WorkerStop' => 'onWorkerStop', |
13
|
|
|
'Connect' => 'onConnect', |
14
|
|
|
'Receive' => 'onMessage', |
15
|
|
|
'Close' => 'onClose', |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
public static function getParams() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
|
|
'name', |
22
|
|
|
'user', |
23
|
|
|
'reloadable', |
24
|
|
|
'transport', |
25
|
|
|
'daemonize', |
26
|
|
|
'stdoutFile', |
27
|
|
|
'reusePort', |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
8 |
|
public function __construct($host, $port) |
32
|
|
|
{ |
33
|
8 |
|
if (file_exists(__DIR__ . '/../../vendor/workerman/workerman/Autoloader.php')) { |
34
|
8 |
|
require __DIR__ . '/../../vendor/workerman/workerman/Autoloader.php'; |
35
|
4 |
|
} else { |
36
|
|
|
require __DIR__ . '/../../../../workerman/workerman/Autoloader.php'; // @codeCoverageIgnore |
37
|
|
|
} |
38
|
8 |
|
} |
39
|
|
|
|
40
|
8 |
|
public function start() |
41
|
|
|
{ |
42
|
8 |
|
$this->set(['pidFile' => $this->pid_file]); |
43
|
8 |
|
if (!empty($this->handler_config)) { |
44
|
8 |
|
$this->set($this->handler_config); |
45
|
4 |
|
} |
46
|
8 |
|
$this->on('WorkerStart', [$this, 'onWorkerStart']); |
47
|
|
|
|
48
|
8 |
|
return $this->server->runAll(); |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
public function send($fd, $content) |
52
|
|
|
{ |
53
|
4 |
|
return $this->connections[$fd]['connection']->send($content); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
public function close($fd) |
57
|
|
|
{ |
58
|
4 |
|
return $this->connections[$fd]['connection']->close(); |
59
|
|
|
} |
60
|
|
|
|
61
|
8 |
|
public function onWorkerStart($worker) |
62
|
|
|
{ |
63
|
8 |
|
$this->server = $worker; |
64
|
8 |
|
parent::prepareKernel(); |
|
|
|
|
65
|
8 |
|
} |
66
|
|
|
|
67
|
8 |
|
public function on($event, callable $callback) |
68
|
|
|
{ |
69
|
8 |
|
if (!isset($this->eventMapper[$event])) { |
70
|
|
|
throw new Exception("Event $event not exists", 1); // @codeCoverageIgnore |
71
|
|
|
} |
72
|
|
|
|
73
|
8 |
|
$this->server->{$this->eventMapper[$event]} = $callback; |
74
|
8 |
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
8 |
|
public function set($settings) |
78
|
|
|
{ |
79
|
8 |
|
$server = $this->server; |
80
|
8 |
|
foreach ($settings as $key => $value) { |
81
|
8 |
|
$server::$$key = $value; |
82
|
4 |
|
} |
83
|
8 |
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @codeCoverageIgnore |
88
|
|
|
*/ |
89
|
|
|
public function getPid() |
90
|
|
|
{ |
91
|
|
|
throw new Exception("Can't read pid from Workerman", 1); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: