1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of CacheTool. |
5
|
|
|
* |
6
|
|
|
* (c) Samuel Gordalina <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CacheTool\Adapter; |
13
|
|
|
|
14
|
|
|
use CacheTool\Code; |
15
|
|
|
use Adoy\FastCGI\Client; |
16
|
|
|
|
17
|
|
|
class FastCGI extends AbstractAdapter |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Client |
21
|
|
|
*/ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Array of patterns matching php socket files |
26
|
|
|
*/ |
27
|
|
|
protected $possibleSocketFilePatterns = [ |
28
|
|
|
'/var/run/php*.sock', |
29
|
|
|
'/var/run/php/*.sock' |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $host; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $host 127.0.0.1:9000 or /var/run/php5-fpm.sock |
39
|
|
|
* @param string $tempDir |
40
|
|
|
*/ |
41
|
3 |
|
public function __construct($host = null, $tempDir = null) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
// try to guess where it is |
44
|
3 |
|
if ($host === null) { |
45
|
2 |
|
foreach ($this->possibleSocketFilePatterns as $possibleSocketFilePattern) { |
46
|
2 |
|
$possibleSocketFile = current(glob($possibleSocketFilePattern)); |
47
|
2 |
|
if (file_exists($possibleSocketFile)) { |
48
|
|
|
$host = $possibleSocketFile; |
49
|
|
|
break; |
50
|
|
|
} |
51
|
2 |
|
} |
52
|
2 |
|
if ($host === null) { |
53
|
2 |
|
$host = '127.0.0.1:9000'; |
54
|
2 |
|
} |
55
|
2 |
|
} |
56
|
|
|
|
57
|
3 |
|
$this->host = $host; |
58
|
|
|
|
59
|
3 |
|
if (false !== strpos($host, ':')) { |
60
|
3 |
|
list($host, $port) = explode(':', $host); |
61
|
3 |
|
$this->client = new Client($host, $port); |
62
|
3 |
|
} else { |
63
|
|
|
// socket |
64
|
|
|
$this->client = new Client('unix://' . $host, -1); |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
$this->client->setReadWriteTimeout(60 * 1000); |
68
|
3 |
|
$this->client->setPersistentSocket(false); |
69
|
3 |
|
$this->client->setKeepAlive(true); |
70
|
3 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
protected function doRun(Code $code) |
76
|
|
|
{ |
77
|
|
|
$response = $this->request($code); |
78
|
|
|
$parts = explode("\r\n\r\n", $response); |
79
|
|
|
|
80
|
|
|
// remove headers |
81
|
|
|
array_shift($parts); |
82
|
|
|
$body = implode("\r\n\r\n", $parts); |
83
|
|
|
|
84
|
|
|
if (@unserialize($body) === false) { |
85
|
|
|
throw new \RuntimeException(sprintf("Error: %s", $response)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $body; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function request(Code $code) |
92
|
|
|
{ |
93
|
|
|
$file = $this->createTemporaryFile(); |
94
|
|
|
|
95
|
|
|
$this->logger->info(sprintf('FastCGI: Dumped code to file: %s', $file)); |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
$code->writeTo($file); |
99
|
|
|
|
100
|
|
|
$environment = array( |
101
|
|
|
'REQUEST_METHOD' => 'POST', |
102
|
|
|
'REQUEST_URI' => '/', |
103
|
|
|
'SCRIPT_FILENAME' => $file, |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$response = $this->client->request($environment, ''); |
107
|
|
|
$this->logger->debug(sprintf('FastCGI: Response: %s', json_encode($response))); |
108
|
|
|
|
109
|
|
|
@unlink($file); |
|
|
|
|
110
|
|
|
return $response; |
111
|
|
|
} catch (\Exception $e) { |
112
|
|
|
@unlink($file); |
|
|
|
|
113
|
|
|
|
114
|
|
|
throw new \RuntimeException( |
115
|
|
|
sprintf('FastCGI error: %s (%s)', $e->getMessage(), $this->host), |
116
|
|
|
$e->getCode(), |
117
|
|
|
$e |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.