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\Proxy; |
13
|
|
|
|
14
|
|
|
use CacheTool\Adapter\AbstractAdapter; |
15
|
|
|
use CacheTool\Code; |
16
|
|
|
|
17
|
|
|
class OpcacheProxy implements ProxyInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var AbstractAdapter |
21
|
|
|
*/ |
22
|
|
|
protected $adapter; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
15 |
|
public function getFunctions() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
15 |
|
'opcache_compile_file', |
31
|
|
|
'opcache_compile_files', |
32
|
|
|
'opcache_get_configuration', |
33
|
|
|
'opcache_get_status', |
34
|
|
|
'opcache_invalidate', |
35
|
|
|
'opcache_reset', |
36
|
|
|
|
37
|
|
|
'opcache_version' |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
15 |
|
public function setAdapter(AbstractAdapter $adapter) |
45
|
|
|
{ |
46
|
15 |
|
$this->adapter = $adapter; |
47
|
15 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Compiles and caches a PHP script without executing it |
51
|
|
|
* |
52
|
|
|
* This function compiles a PHP script and adds it to the opcode cache without executing it. This can be used to |
53
|
|
|
* prime the cache after a Web server restart by pre-caching files that will be included in later requests. |
54
|
|
|
* |
55
|
|
|
* @since 5.5.5 |
56
|
|
|
* @since 7.0.2 |
57
|
|
|
* @param string $file The path to the PHP script to be compiled. |
58
|
|
|
* @return boolean Returns TRUE if file was compiled successfully or FALSE on failure. |
59
|
|
|
*/ |
60
|
1 |
|
public function opcache_compile_file($file) |
61
|
|
|
{ |
62
|
1 |
|
$code = new Code(); |
63
|
1 |
|
$code->addStatement(sprintf( |
64
|
1 |
|
'return opcache_compile_file(%s);', |
65
|
1 |
|
var_export($file, true) |
66
|
|
|
)); |
67
|
|
|
|
68
|
1 |
|
return $this->adapter->run($code); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Compiles and caches a PHP scripts without executing them |
73
|
|
|
* |
74
|
|
|
* This function compiles a PHP scripts and adds them to the opcode cache without executing them. This can be used to |
75
|
|
|
* prime the cache after a Web server restart by pre-caching files that will be included in later requests. |
76
|
|
|
* |
77
|
|
|
* @since 5.5.5 |
78
|
|
|
* @since 7.0.2 |
79
|
|
|
* @param array $files The array for paths to PHP scripts to be compiled. |
80
|
|
|
* @return boolean Returns TRUE if files were compiled successfully or FALSE on failure. |
81
|
|
|
*/ |
82
|
1 |
|
public function opcache_compile_files($files) |
83
|
|
|
{ |
84
|
1 |
|
$code = new Code(); |
85
|
1 |
|
$code->addStatement('$paths = ['); |
86
|
1 |
|
foreach ($files as $file) { |
87
|
1 |
|
$code->addStatement(sprintf(' %s,', var_export($file, true))); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$code->addStatement('];'); |
91
|
|
|
|
92
|
1 |
|
$code->addStatements([ |
93
|
1 |
|
'foreach ($paths as $path) {', |
94
|
|
|
' $compiled = opcache_compile_file($path);', |
95
|
|
|
' if (!$compiled) {', |
96
|
|
|
' return false;', |
97
|
|
|
' }', |
98
|
|
|
'}', |
99
|
|
|
]); |
100
|
|
|
|
101
|
1 |
|
$code->addStatement('return true;'); |
102
|
|
|
|
103
|
1 |
|
return $this->adapter->run($code); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get configuration information about the cache |
108
|
|
|
* |
109
|
|
|
* @since 5.5.5 |
110
|
|
|
* @since 7.0.2 |
111
|
|
|
* @return array Returns an array of information, including ini, blacklist and version |
112
|
|
|
*/ |
113
|
2 |
|
public function opcache_get_configuration() |
114
|
|
|
{ |
115
|
2 |
|
$code = new Code(); |
116
|
2 |
|
$code->addStatement('return opcache_get_configuration();'); |
117
|
|
|
|
118
|
2 |
|
return $this->adapter->run($code); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get status information about the cache |
123
|
|
|
* |
124
|
|
|
* @since 5.5.5 |
125
|
|
|
* @since 7.0.2 |
126
|
|
|
* @param boolean $get_scripts Include script specific state information |
127
|
|
|
* @return array Returns an array of information, optionally containing script specific state |
128
|
|
|
* information |
129
|
|
|
*/ |
130
|
3 |
|
public function opcache_get_status($get_scripts = true) |
131
|
|
|
{ |
132
|
3 |
|
$code = new Code(); |
133
|
3 |
|
$code->addStatement(sprintf( |
134
|
3 |
|
'return opcache_get_status(%s);', |
135
|
3 |
|
var_export($get_scripts, true) |
136
|
|
|
)); |
137
|
|
|
|
138
|
3 |
|
return $this->adapter->run($code); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get status information about the cache |
143
|
|
|
* |
144
|
|
|
* @since 5.5.0 |
145
|
|
|
* @since 7.0.0 |
146
|
|
|
* @param string $script The path to the script being invalidated. |
147
|
|
|
* @param boolean $force If set to TRUE, the script will be invalidated regardless of whether invalidation is |
148
|
|
|
* necessary. |
149
|
|
|
* @return boolean Returns TRUE if the opcode cache for script was invalidated or if there was nothing to |
150
|
|
|
* invalidate, or FALSE if the opcode cache is disabled. |
151
|
|
|
*/ |
152
|
1 |
|
public function opcache_invalidate($script, $force = false) |
153
|
|
|
{ |
154
|
1 |
|
$code = new Code(); |
155
|
1 |
|
$code->addStatement(sprintf( |
156
|
1 |
|
'return opcache_invalidate(%s, %s);', |
157
|
1 |
|
var_export($script, true), |
158
|
1 |
|
var_export($force, true) |
159
|
|
|
)); |
160
|
|
|
|
161
|
1 |
|
return $this->adapter->run($code); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Resets the contents of the opcode cache |
166
|
|
|
* |
167
|
|
|
* @since 5.5.0 |
168
|
|
|
* @since 7.0.0 |
169
|
|
|
* @return boolean Returns TRUE if the opcode cache was reset, or FALSE if the opcode cache is disabled. |
170
|
|
|
*/ |
171
|
2 |
|
public function opcache_reset() |
172
|
|
|
{ |
173
|
|
|
// Avoid using the opcache_reset() return value to workaround PHP bug |
174
|
|
|
// https://bugs.php.net/bug.php?id=71621 |
175
|
2 |
|
$code = new Code(); |
176
|
2 |
|
$code->addStatement('opcache_reset();'); |
177
|
2 |
|
$code->addStatement('return true;'); |
178
|
|
|
|
179
|
2 |
|
return $this->adapter->run($code); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
1 |
|
public function opcache_version() |
186
|
|
|
{ |
187
|
1 |
|
$code = new Code(); |
188
|
1 |
|
$code->addStatement('return phpversion("Zend OPcache");'); |
189
|
|
|
|
190
|
1 |
|
return $this->adapter->run($code); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|