1 | <?php |
||
21 | class FastCGI extends AbstractAdapter |
||
22 | { |
||
23 | /** |
||
24 | * @var Client |
||
25 | */ |
||
26 | protected $client; |
||
27 | |||
28 | /** |
||
29 | * @var ConfiguresSocketConnection |
||
30 | */ |
||
31 | protected $connection; |
||
32 | |||
33 | /** |
||
34 | * @var Array of patterns matching php socket files |
||
35 | */ |
||
36 | protected $possibleSocketFilePatterns = [ |
||
37 | '/var/run/php*.sock', |
||
38 | '/var/run/php/*.sock' |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $host; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $chroot; |
||
50 | |||
51 | /** |
||
52 | * @param string $host 127.0.0.1:9000 or /var/run/php5-fpm.sock |
||
53 | * @param string $chroot |
||
54 | */ |
||
55 | 11 | public function __construct($host = null, $chroot = null) |
|
56 | { |
||
57 | // try to guess where it is |
||
58 | 11 | if ($host === null) { |
|
59 | 6 | foreach ($this->possibleSocketFilePatterns as $possibleSocketFilePattern) { |
|
60 | 6 | $possibleSocketFile = current(glob($possibleSocketFilePattern)); |
|
61 | 6 | if (file_exists($possibleSocketFile)) { |
|
62 | $host = $possibleSocketFile; |
||
63 | break; |
||
64 | } |
||
65 | } |
||
66 | 6 | if ($host === null) { |
|
67 | 6 | $host = '127.0.0.1:9000'; |
|
68 | } |
||
69 | } |
||
70 | |||
71 | 11 | $this->host = $host; |
|
72 | |||
73 | 11 | if (false !== strpos($host, ':')) { |
|
74 | 10 | $last = strrpos($host, ':'); |
|
75 | 10 | $port = substr($host, $last + 1, strlen($host)); |
|
76 | 10 | $host = substr($host, 0, $last); |
|
77 | |||
78 | 10 | $IPv6 = '/^(?:[A-F0-9]{0,4}:){1,7}[A-F0-9]{0,4}$/'; |
|
79 | 10 | if (preg_match($IPv6, $host) === 1) { |
|
80 | // IPv6 addresses need to be surrounded by brackets |
||
81 | // see: https://www.php.net/manual/en/function.stream-socket-client.php#refsect1-function.stream-socket-client-notes |
||
82 | 1 | $host = "[${host}]"; |
|
83 | } |
||
84 | |||
85 | 10 | $this->connection = new NetworkSocket( |
|
86 | 10 | $host, # Hostname |
|
87 | $port, # Port |
||
88 | 10 | 5000, # Connect timeout in milliseconds (default: 5000) |
|
89 | 10 | 120000 # Read/write timeout in milliseconds (default: 5000) |
|
90 | ); |
||
91 | } else { |
||
92 | 1 | $this->connection = new UnixDomainSocket( |
|
93 | 1 | $host, # Socket path to php-fpm |
|
94 | 1 | 5000, # Connect timeout in milliseconds (default: 5000) |
|
95 | 1 | 120000 # Read/write timeout in milliseconds (default: 5000) |
|
96 | ); |
||
97 | } |
||
98 | |||
99 | 11 | $this->client = new Client(); |
|
100 | |||
101 | 11 | if ($chroot !== null) { |
|
102 | 3 | $this->chroot = rtrim($chroot, '/'); |
|
103 | } |
||
104 | 11 | } |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 4 | protected function doRun(Code $code) |
|
119 | |||
120 | 4 | protected function request(Code $code) |
|
150 | |||
151 | /** |
||
152 | * @param string $file |
||
153 | * @return string |
||
154 | * @throws \RuntimeException |
||
155 | */ |
||
156 | 3 | protected function getScriptFileName($file) |
|
166 | } |
||
167 |