-
Notifications
You must be signed in to change notification settings - Fork 18
/
CacheClearerService.php
182 lines (149 loc) · 4.72 KB
/
CacheClearerService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace SmartCore\Bundle\AcceleratorCacheBundle;
/**
* @author Kevin Bond <[email protected]>
*/
class CacheClearerService
{
const MODE_FOPEN = 'fopen';
const MODE_CURL = 'curl';
private $host;
private $webDir;
private $template;
private $mode;
private $options;
/**
* @param string $host
* @param string $webDir
* @param string $template
* @param string $mode
* @param array $options
*/
public function __construct($host, $webDir, $template, $mode = self::MODE_FOPEN, array $options = array())
{
if (!in_array($mode, array(self::MODE_FOPEN, self::MODE_CURL))) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid mode.', $mode));
}
$this->host = $host;
$this->webDir = $webDir;
$this->template = $template;
$this->mode = $mode;
$this->options = $options;
}
/**
* @param bool $user
* @param bool $opcode
* @param string|null $authentication
*
* @return array
*
* @throws \Exception
*/
public function clearCache($user = true, $opcode = true, $authentication = null)
{
$filename = $this->createTemporaryFile($user, $opcode);
$url = sprintf('%s/%s', $this->host, basename($filename));
try {
$result = $this->sendRequest($url, $authentication);
} catch (\Exception $e) {
unlink($filename);
throw $e;
}
unlink($filename);
return json_decode($result, true);
}
/**
* @param string $url
* @param string|null $authentication
*
* @return string
*/
private function sendRequest($url, $authentication = null)
{
if (self::MODE_FOPEN === $this->mode) {
return $this->sendFopenRequest($url, $authentication);
}
return $this->sendCurlRequest($url, $authentication);
}
/**
* @param string $url
* @param string|null $authentication
*
* @return string
*/
private function sendFopenRequest($url, $authentication = null)
{
$context = null;
if (null !== $authentication) {
$context = stream_context_create(array('http' => array(
'header' => 'Authorization: Basic '.base64_encode($authentication),
)));
}
$result = false;
for ($i = 0; $i < 5; $i++) {
if ($result = @file_get_contents($url, null, $context)) {
break;
}
sleep(1);
}
if (false === $result) {
throw new \RuntimeException(sprintf('Unable to read "%s", does the host locally resolve?', $url));
}
return $result;
}
/**
* @param string $url
* @param string|null $authentication
*
* @return string
*/
private function sendCurlRequest($url, $authentication = null)
{
$handle = curl_init($url);
curl_setopt_array($handle, array_replace($this->options, array(
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FAILONERROR => true,
)));
if (null !== $authentication) {
curl_setopt($handle, CURLOPT_USERPWD, $authentication);
}
$result = curl_exec($handle);
if (curl_errno($handle)) {
$error = curl_error($handle);
curl_close($handle);
throw new \RuntimeException(sprintf('Curl error reading "%s": %s', $url, $error));
}
curl_close($handle);
return $result;
}
/**
* Create the temporary file and return the filename.
*
* @param bool $user
* @param bool $opcode
*
* @return string
*/
private function createTemporaryFile($user, $opcode)
{
if (!is_dir($this->webDir)) {
throw new \InvalidArgumentException(sprintf('Web dir does not exist "%s"', $this->webDir));
}
if (!is_writable($this->webDir)) {
throw new \InvalidArgumentException(sprintf('Web dir is not writable "%s"', $this->webDir));
}
$filename = sprintf('%s/%s', $this->webDir, 'apc-'.md5(uniqid().mt_rand(0, 9999999).php_uname()).'.php');
$contents = strtr($this->template, array(
'%clearer_code%' => file_get_contents(__DIR__.'/AcceleratorCacheClearer.php'),
'%user%' => var_export($user, true),
'%opcode%' => var_export($opcode, true),
));
if (false === $handle = fopen($filename, 'w+')) {
throw new \RuntimeException(sprintf('Can\'t open "%s"', $filename));
}
fwrite($handle, $contents);
fclose($handle);
return $filename;
}
}