1 | <?php |
||
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) |
|
55 | |||
56 | 4 | public function close($fd) |
|
60 | |||
61 | 8 | public function onWorkerStart($worker) |
|
66 | |||
67 | 8 | public function on($event, callable $callback) |
|
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() |
||
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: