|
1
|
|
|
<?php |
|
2
|
|
|
namespace Laravoole\Wrapper; |
|
3
|
|
|
|
|
4
|
|
|
use Laravoole\Workerman\Worker; |
|
5
|
|
|
use Garveen\FastCgi\FastCgi; |
|
6
|
|
|
|
|
7
|
|
|
class WorkermanFastCGIWrapper extends Workerman implements ServerInterface |
|
8
|
|
|
{ |
|
9
|
4 |
|
public function __construct($host, $port) |
|
10
|
|
|
{ |
|
11
|
4 |
|
parent::__construct($host, $port); |
|
12
|
4 |
|
$this->server = new Worker("tcp://{$host}:{$port}"); |
|
13
|
4 |
|
} |
|
14
|
|
|
|
|
15
|
4 |
|
public function start() |
|
16
|
|
|
{ |
|
17
|
4 |
|
$this->on('Receive', [$this, 'onReceive']); |
|
18
|
4 |
|
return parent::start(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
4 |
|
public function onWorkerStart($worker) |
|
22
|
|
|
{ |
|
23
|
4 |
|
$worker->log("Workerman worker {$worker->id} starting\n"); |
|
24
|
4 |
|
parent::onWorkerStart($worker); |
|
25
|
4 |
|
$this->fastcgi = new FastCgi([$this, 'requestCallback'], [$this, 'sendCallback'], [$this, 'closeCallback'], function($level, $info) use ($worker) { |
|
|
|
|
|
|
26
|
4 |
|
$worker->log("$level $info"); |
|
27
|
4 |
|
}); |
|
28
|
4 |
|
} |
|
29
|
|
|
|
|
30
|
4 |
|
public function onReceive($connection, $data) |
|
31
|
|
|
{ |
|
32
|
4 |
|
$this->connections[$connection->id]['connection'] = $connection; |
|
|
|
|
|
|
33
|
4 |
|
return $this->fastcgi->receive($connection->id, $data); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
4 |
|
public function requestCallback($psrRequest) |
|
37
|
|
|
{ |
|
38
|
4 |
|
return $this->onPsrRequest($psrRequest); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
4 |
|
public function sendCallback($fd, $payload) |
|
42
|
|
|
{ |
|
43
|
4 |
|
$this->send($fd, $payload); |
|
44
|
4 |
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
public function closeCallback($fd) |
|
47
|
|
|
{ |
|
48
|
4 |
|
$this->close($fd); |
|
49
|
4 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
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: