-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMonkeyPatchManager.php
More file actions
392 lines (319 loc) · 9.62 KB
/
Copy pathMonkeyPatchManager.php
File metadata and controls
392 lines (319 loc) · 9.62 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
declare(strict_types=1);
/*
* Copyright (c) 2021 Kenji Suzuki
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/kenjis/monkey-patch
*/
namespace Kenjis\MonkeyPatch;
use Kenjis\MonkeyPatch\Exception\ExitException;
use Kenjis\MonkeyPatch\Patcher\FunctionPatcher;
use LogicException;
use PhpParser\ParserFactory;
use function assert;
use function constant;
use function date;
use function explode;
use function file;
use function file_get_contents;
use function file_put_contents;
use function fopen;
use function fwrite;
use function in_array;
use function is_array;
use function is_readable;
use function is_resource;
use function is_string;
use function microtime;
use function rewind;
use function substr;
use function trim;
use const FILE_APPEND;
class MonkeyPatchManager
{
/** @var bool */
public static $debug = false;
/** @var int */
private static $php_version_major = 5;
/** @var int */
private static $php_version_minor = 1;
/**
* The path to the log file if `$debug` is true.
* Will be set in {@link MonkeyPatchManager::setDebug}.
*
* @var string */
public static $log_file = __DIR__ . '/debug.log';
/** @var bool */
private static $load_patchers = false;
/** @var class-string */
private static $exit_exception_classname = ExitException::class;
/** @var string[] list of patcher classname */
private static $patcher_list = [
'ExitPatcher',
'FunctionPatcher',
'MethodPatcher',
'ConstantPatcher',
];
public static function log(string $message): void
{
if (! self::$debug) {
return;
}
$time = date('Y-m-d H:i:s');
[$usec] = explode(' ', microtime());
$usec = substr($usec, 1);
$log = "[$time $usec] $message\n";
file_put_contents(self::$log_file, $log, FILE_APPEND);
}
/**
* @param class-string $name
*/
public static function setExitExceptionClassname(string $name): void
{
self::$exit_exception_classname = $name;
}
public static function getExitExceptionClassname(): string
{
return self::$exit_exception_classname;
}
public static function getPhpVersionMajor(): int
{
return self::$php_version_major;
}
public static function getPhpVersionMinor(): int
{
return self::$php_version_minor;
}
/**
* @param array<string, mixed> $config
*/
protected static function setPhpVersion(array $config): void
{
if (isset($config['php_version_major'])) {
self::$php_version_major = $config['php_version_major'];
}
if (isset($config['php_version_minor'])) {
self::$php_version_minor = $config['php_version_minor'];
}
}
/**
* @param array<string, mixed> $config
*/
protected static function setDebug(array $config): void
{
if (isset($config['debug'])) {
self::$debug = $config['debug'];
}
if (isset($config['log_file'])) {
self::$log_file = $config['log_file'];
}
}
/**
* @param array<string, mixed> $config
*/
protected static function setDir(array $config): void
{
if (! isset($config['root_dir'])) {
throw new LogicException('You have to set "root_dir"');
}
Cache::setProjectRootDir($config['root_dir']);
if (! isset($config['cache_dir'])) {
throw new LogicException('You have to set "cache_dir"');
}
self::setCacheDir($config['cache_dir']);
}
/**
* @param array<string, mixed> $config
*/
protected static function setPaths(array $config): void
{
if (! isset($config['include_paths'])) {
throw new LogicException('You have to set "include_paths"');
}
self::setIncludePaths($config['include_paths']);
if (isset($config['exclude_paths'])) {
self::setExcludePaths($config['exclude_paths']);
}
}
/**
* @param array<string, mixed> $config
*
* @codeCoverageIgnore
*/
public static function init(array $config): void
{
self::setDebug($config);
self::setPhpVersion($config);
self::setDir($config);
self::setPaths($config);
Cache::createTmpListDir();
if (isset($config['patcher_list'])) {
self::setPatcherList($config['patcher_list']);
}
self::checkPatcherListUpdate();
self::checkPathsUpdate();
self::loadPatchers();
self::addTmpFunctionBlacklist();
if (isset($config['functions_to_patch'])) {
FunctionPatcher::addWhitelists($config['functions_to_patch']);
}
self::checkFunctionWhitelistUpdate();
FunctionPatcher::lockFunctionList();
if (isset($config['exit_exception_classname'])) {
self::setExitExceptionClassname($config['exit_exception_classname']);
}
// Register include stream wrapper for monkey patching
self::wrap();
}
protected static function checkPathsUpdate(): void
{
$cached = Cache::getTmpIncludePaths();
$current = PathChecker::getIncludePaths();
// Updated?
if ($cached !== $current) {
self::log('clear_src_cache: from ' . __METHOD__);
Cache::clearSrcCache();
Cache::writeTmpIncludePaths($current);
}
$cached = Cache::getTmpExcludePaths();
$current = PathChecker::getExcludePaths();
// Updated?
if ($cached !== $current) {
self::log('clear_src_cache: from ' . __METHOD__);
Cache::clearSrcCache();
Cache::writeTmpExcludePaths($current);
}
}
protected static function checkPatcherListUpdate(): void
{
$cached = Cache::getTmpPatcherList();
// Updated?
if ($cached !== self::$patcher_list) {
self::log('clear_src_cache: from ' . __METHOD__);
Cache::clearSrcCache();
Cache::writeTmpPatcherList(self::$patcher_list);
}
}
protected static function checkFunctionWhitelistUpdate(): void
{
$cached = Cache::getTmpFunctionWhitelist();
$current = FunctionPatcher::getFunctionWhitelist();
// Updated?
if ($cached !== $current) {
self::log('clear_src_cache: from ' . __METHOD__);
Cache::clearSrcCache();
Cache::writeTmpFunctionWhitelist($current);
}
}
protected static function addTmpFunctionBlacklist(): void
{
$list = file(Cache::getTmpFunctionBlacklistFile());
assert(is_array($list));
foreach ($list as $function) {
FunctionPatcher::addBlacklist(trim($function));
}
}
public static function isEnabled(string $patcher): bool
{
return in_array($patcher, self::$patcher_list);
}
/**
* @param string[] $list
*/
public static function setPatcherList(array $list): void
{
if (self::$load_patchers) {
throw new LogicException("Can't change patcher list after initialisation");
}
self::$patcher_list = $list;
}
public static function setCacheDir(string $dir): void
{
Cache::setCacheDir($dir);
}
/**
* @param string[] $dir_list
*/
public static function setIncludePaths(array $dir_list): void
{
PathChecker::setIncludePaths($dir_list);
}
/**
* @param string[] $dir_list
*/
public static function setExcludePaths(array $dir_list): void
{
PathChecker::setExcludePaths($dir_list);
}
public static function wrap(): void
{
IncludeStream::wrap();
}
public static function unwrap(): void
{
IncludeStream::unwrap();
}
/**
* @param string $path original source file path
*
* @return resource
*
* @throws LogicException
*/
public static function patch(string $path)
{
if (! is_readable($path)) {
throw new LogicException("Can't read '$path'");
}
// Check cache file
$cache_file = Cache::getValidSrcCachePath($path);
if ($cache_file) {
self::log('cache_hit: ' . $path);
$resource = fopen($cache_file, 'r');
assert(is_resource($resource));
return $resource;
}
self::log('cache_miss: ' . $path);
$source = file_get_contents($path);
assert(is_string($source));
[$new_source] = self::execPatchers($source);
// Write to cache file
self::log('write_cache: ' . $path);
Cache::writeSrcCacheFile($path, $new_source);
$resource = fopen('php://memory', 'rb+');
assert(is_resource($resource));
fwrite($resource, $new_source);
rewind($resource);
return $resource;
}
/**
* @codeCoverageIgnore
*/
protected static function loadPatchers(): void
{
if (self::$load_patchers) {
return;
}
self::$load_patchers = true;
}
/**
* @return array{mixed, bool}
*/
protected static function execPatchers(string $source): array
{
$patched = false;
foreach (self::$patcher_list as $classname) {
$classname = 'Kenjis\MonkeyPatch\Patcher\\' . $classname;
$patcher = new $classname();
[$source, $patched_this] = $patcher->patch($source);
$patched = $patched || $patched_this;
}
return [
$source,
$patched,
];
}
}