|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OPCache; |
|
4
|
|
|
|
|
5
|
|
|
use OPCache\FCGIRequest; |
|
6
|
|
|
use OPCache\HTTPRequest; |
|
7
|
|
|
use OPCache\OPCacheConfiguration; |
|
8
|
|
|
use OPCache\OPCacheStatus; |
|
9
|
|
|
|
|
10
|
|
|
class OPCache { |
|
11
|
|
|
|
|
12
|
|
|
private $queryString; |
|
13
|
|
|
private $scripts; |
|
14
|
|
|
private $servers; |
|
15
|
|
|
private $uri; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct() { |
|
18
|
|
|
$this->servers = variable_get('opcache_backends', NULL); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
private function buildQueryString($params) { |
|
22
|
|
|
$this->buildUri($params); |
|
23
|
|
|
$this->queryString = 'q=' . $this->uri; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
private function buildUri($params) { |
|
27
|
|
|
$token = $this->getToken(); |
|
28
|
|
|
$this->uri = 'opcache/' . REQUEST_TIME . '/' . $token . '/' . $params['op']; |
|
29
|
|
|
if (isset($params['script'])) { |
|
30
|
|
|
$this->uri .= '/' . $params['script']; |
|
31
|
|
|
} |
|
32
|
|
|
if ($params['op'] == 'reset' && $params['all']) { |
|
33
|
|
|
$this->uri .= '/all'; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function cacheClear() { |
|
38
|
|
|
if (!$this->servers) { |
|
39
|
|
|
return $this->reset(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Multiple backends must be cleared. |
|
43
|
|
|
$params = []; |
|
44
|
|
|
$params['op'] = 'reset'; |
|
45
|
|
|
$this->multiBackendRequest($params); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function config() { |
|
49
|
|
|
$config = new OPCacheConfiguration(); |
|
50
|
|
|
return $config->getDirectives(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function drushBuildUrl($server, $params) { |
|
54
|
|
|
$this->buildQueryString($params); |
|
55
|
|
|
$url = "{$server}?{$this->queryString}"; |
|
56
|
|
|
return $url; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function drushInvalidate($script) { |
|
60
|
|
|
$params = array(); |
|
61
|
|
|
$params['op'] = 'invalidate'; |
|
62
|
|
|
$params['script'] = $script; |
|
63
|
|
|
$this->drushRequest($params); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function drushReset($all) { |
|
67
|
|
|
$params = array(); |
|
68
|
|
|
$params['op'] = 'reset'; |
|
69
|
|
|
$params['all'] = $all; |
|
70
|
|
|
$this->drushRequest($params); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function drushStatus() { |
|
74
|
|
|
$params = array(); |
|
75
|
|
|
$params['op'] = 'status'; |
|
76
|
|
|
return $this->drushRequest($params); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function drushRequest($params = array()) { |
|
80
|
|
|
global $base_url; |
|
81
|
|
|
if (preg_match('/default$/', $base_url) && !$this->servers) { |
|
82
|
|
|
drush_log(dt("In order to properly reset the OPcache cache, please use the -l/--uri flag to specify the correct URL of this Drupal installation, or specify paths to the PHP proxy servers in the OPcache module's settings form."), 'error'); |
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
if (!$this->servers) { |
|
86
|
|
|
$server = url('<front>', array('absolute' => TRUE)); |
|
87
|
|
|
$this->httpRequest($server, $params); |
|
88
|
|
|
} |
|
89
|
|
|
else { |
|
90
|
|
|
$this->multiBackendRequest($params); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function fcgiRequest($server, $params) { |
|
|
|
|
|
|
95
|
|
|
$fcgi = substr($server, 7); |
|
96
|
|
|
try { |
|
97
|
|
|
$command = new FCGIRequest($fcgi, $this->uri, $this->queryString); |
|
98
|
|
|
$command->run(); |
|
99
|
|
|
} catch (\Exception $e) { |
|
100
|
|
|
watchdog('opcache', 'An error was encountered clearing OPCache on %server. Message: %error', array('%server' => $server, '%error' => $e->getMessage()), WATCHDOG_ERROR); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function getScripts() { |
|
105
|
|
|
$status = new OPCacheStatus(TRUE); |
|
106
|
|
|
$scripts = $status->getScripts(); |
|
107
|
|
|
foreach ($scripts as $script) { |
|
108
|
|
|
if (strpos($script['full_path'], DRUPAL_ROOT) !== FALSE) { |
|
109
|
|
|
$this->scripts[] = $script['full_path']; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getToken($request_time = REQUEST_TIME) { |
|
115
|
|
|
return drupal_hmac_base64('opcache:' . $request_time, drupal_get_private_key() . drupal_get_hash_salt()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function httpRequest($server, $params) { |
|
119
|
|
|
global $base_url; |
|
120
|
|
|
$urldata = @parse_url($base_url); |
|
121
|
|
|
|
|
122
|
|
|
$url = $this->drushBuildUrl($server, $params); |
|
123
|
|
|
try { |
|
124
|
|
|
$client = new HTTPRequest(); |
|
125
|
|
|
$request = $client->createRequest('GET', $url); |
|
|
|
|
|
|
126
|
|
|
$request->setHeader('Host', $urldata['host']); |
|
127
|
|
|
$response = $client->send($request); |
|
128
|
|
|
$status = $response->getStatusCode(); |
|
129
|
|
|
$this->logResponse($server, $status, $params); |
|
130
|
|
|
} catch (\Exception $e) { |
|
131
|
|
|
watchdog('opcache', 'An error was encountered clearing OPCache on %server. Message: %error', array('%server' => $server, '%error' => $e->getMessage()), WATCHDOG_ERROR); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function isEnabled() { |
|
136
|
|
|
$status = new OPCacheStatus(); |
|
137
|
|
|
$info = $status->getCurrentStatus(); |
|
138
|
|
|
if ($info['opcache_enabled']) { |
|
139
|
|
|
return TRUE; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function invalidate($script, $force = FALSE) { |
|
144
|
|
|
return opcache_invalidate($script, $force); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function invalidateMultiple(array $scripts, $force = FALSE) { |
|
148
|
|
|
$invalidation_counter = 0; |
|
149
|
|
|
if (!empty($scripts)) { |
|
150
|
|
|
foreach ($scripts as $script) { |
|
151
|
|
|
$this->invalidate($script, $force); |
|
152
|
|
|
$invalidation_counter++; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
watchdog('opcache', '@scripts scripts were invalidated in OPCache.', array('@scripts' => $invalidation_counter), WATCHDOG_INFO); |
|
156
|
|
|
} |
|
157
|
|
|
else { |
|
158
|
|
|
watchdog('opcache', 'No scripts were available for invalidation in OPCache.', array(), WATCHDOG_INFO); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
private function logResponse($server, $status, $params) { |
|
163
|
|
|
switch ($status) { |
|
164
|
|
|
case 200: |
|
165
|
|
|
if ($params['op'] === 'reset') { |
|
166
|
|
|
watchdog('opcache', 'OPcache was reset at @server.', array('@server' => $server), WATCHDOG_INFO); |
|
167
|
|
|
} |
|
168
|
|
|
elseif ($params['op'] === 'invalidate') { |
|
169
|
|
|
watchdog('opcache', '@script was invalidated in OPcache at @server.', array('@script' => $params['script'], '@server' => $server), WATCHDOG_INFO); |
|
170
|
|
|
} |
|
171
|
|
|
break; |
|
172
|
|
|
case 404: |
|
173
|
|
|
watchdog('opcache', 'OPcache operation at @server failed; the reset path could not be found (404).', array('@server' => $server), WATCHDOG_ERROR); |
|
174
|
|
|
break; |
|
175
|
|
|
case 403: |
|
176
|
|
|
watchdog('opcache', 'OPcache operation at @server failed; access to the reset path was denied (403). This may happen if too much time elapsed during the request process. Please try again.', array('@server' => $server), WATCHDOG_ERROR); |
|
177
|
|
|
break; |
|
178
|
|
|
case 0: |
|
179
|
|
|
watchdog('opcache', 'OPcache operation at @server failed; server could not be reached.', array('@server' => $server), WATCHDOG_ERROR); |
|
180
|
|
|
break; |
|
181
|
|
|
default: |
|
182
|
|
|
watchdog('opcache', 'OPcache operation at @server failed; status code @code.', array('@server' => $server, '@code' => $status), WATCHDOG_ERROR); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
private function multiBackendRequest($params) { |
|
187
|
|
|
foreach ($this->servers as $server) { |
|
188
|
|
|
if (substr($server, 0, 7) == 'fcgi://') { |
|
189
|
|
|
$method = 'fcgiRequest'; |
|
190
|
|
|
} |
|
191
|
|
|
else { |
|
192
|
|
|
$method = 'httpRequest'; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$this->{$method}($server, $params); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function reset($all = FALSE) { |
|
200
|
|
|
if ($all) { |
|
201
|
|
|
return opcache_reset(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$this->getScripts(); |
|
205
|
|
|
$this->invalidateMultiple($this->scripts, TRUE); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function status() { |
|
209
|
|
|
$status = new OPCacheStatus(); |
|
210
|
|
|
return $status->getStatusData(); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function verifyToken($request_time, $token) { |
|
214
|
|
|
return $token === $this->getToken($request_time); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.