1 | <?php |
||
20 | class FastCGI extends AbstractAdapter |
||
21 | { |
||
22 | /** |
||
23 | * @var Client |
||
24 | */ |
||
25 | protected $client; |
||
26 | |||
27 | /** |
||
28 | * @var Connection |
||
29 | */ |
||
30 | protected $connection; |
||
31 | |||
32 | /** |
||
33 | * @var Array of patterns matching php socket files |
||
34 | */ |
||
35 | protected $possibleSocketFilePatterns = [ |
||
36 | '/var/run/php*.sock', |
||
37 | '/var/run/php/*.sock' |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $host; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $chroot; |
||
49 | |||
50 | /** |
||
51 | * @param string $host 127.0.0.1:9000 or /var/run/php5-fpm.sock |
||
52 | * @param string $chroot |
||
53 | */ |
||
54 | 8 | public function __construct($host = null, $chroot = null) |
|
55 | { |
||
56 | // try to guess where it is |
||
57 | 8 | if ($host === null) { |
|
58 | 7 | foreach ($this->possibleSocketFilePatterns as $possibleSocketFilePattern) { |
|
59 | 7 | $possibleSocketFile = current(glob($possibleSocketFilePattern)); |
|
60 | 7 | if (file_exists($possibleSocketFile)) { |
|
61 | 7 | $host = $possibleSocketFile; |
|
62 | 7 | break; |
|
63 | } |
||
64 | } |
||
65 | 7 | if ($host === null) { |
|
66 | $host = '127.0.0.1:9000'; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | 8 | $this->host = $host; |
|
71 | |||
72 | 8 | if (false !== strpos($host, ':')) { |
|
73 | 1 | [$host, $port] = explode(':', $host); |
|
|
|||
74 | 1 | $this->connection = new NetworkSocket( |
|
75 | 1 | $host, # Hostname |
|
76 | $port, # Port |
||
77 | 1 | 5000, # Connect timeout in milliseconds (default: 5000) |
|
78 | 1 | 120000 # Read/write timeout in milliseconds (default: 5000) |
|
79 | ); |
||
80 | } else { |
||
81 | 7 | $this->connection = new UnixDomainSocket( |
|
82 | 7 | $host, # Socket path to php-fpm |
|
83 | 7 | 5000, # Connect timeout in milliseconds (default: 5000) |
|
84 | 7 | 120000 # Read/write timeout in milliseconds (default: 5000) |
|
85 | ); |
||
86 | } |
||
87 | |||
88 | 8 | $this->client = new Client(); |
|
89 | |||
90 | 8 | if ($chroot !== null) { |
|
91 | 2 | $this->chroot = rtrim($chroot, '/'); |
|
92 | } |
||
93 | 8 | } |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 2 | protected function doRun(Code $code) |
|
108 | |||
109 | 2 | protected function request(Code $code) |
|
110 | { |
||
111 | 2 | $file = $this->createTemporaryFile(); |
|
112 | 2 | $this->logger->info(sprintf('FastCGI: Dumped code to file: %s', $file)); |
|
113 | |||
114 | try { |
||
115 | 2 | $code->writeTo($file); |
|
116 | |||
117 | $this->logger->info(sprintf('FastCGI: Requesting FPM using socket: %s', $this->host)); |
||
118 | $request = new PostRequest($this->getScriptFileName($file), ''); |
||
119 | $response = $this->client->sendRequest($this->connection, $request); |
||
120 | $this->logger->debug(sprintf('FastCGI: Response: %s', json_encode($response))); |
||
121 | |||
122 | if (!@unlink($file)) { |
||
123 | $this->logger->debug(sprintf('FastCGI: Could not delete file: %s', $file)); |
||
124 | } |
||
125 | |||
126 | return $response->getBody(); |
||
127 | 2 | } catch (\Exception $e) { |
|
128 | 2 | if (!@unlink($file)) { |
|
129 | 2 | $this->logger->debug(sprintf('FastCGI: Could not delete file: %s', $file)); |
|
130 | } |
||
131 | |||
132 | 2 | throw new \RuntimeException( |
|
133 | 2 | sprintf('FastCGI error: %s (%s)', $e->getMessage(), $this->host), |
|
134 | 2 | $e->getCode(), |
|
135 | $e |
||
136 | ); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param string $file |
||
142 | * @return string |
||
143 | * @throws \RuntimeException |
||
144 | */ |
||
145 | 2 | protected function getScriptFileName($file) |
|
155 | } |
||
156 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.