|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Part of ci-phpunit-test |
|
4
|
|
|
* |
|
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
* @copyright 2015 Kenji Suzuki |
|
8
|
|
|
* @link https://github.com/kenjis/ci-phpunit-test |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Kenjis\MonkeyPatch\Patcher\ConstantPatcher; |
|
12
|
|
|
|
|
13
|
|
|
class_alias('Kenjis\MonkeyPatch\Patcher\ConstantPatcher\Proxy', '__ConstProxy__'); |
|
14
|
|
|
|
|
15
|
|
|
use LogicException; |
|
16
|
|
|
use ReflectionConstant; |
|
17
|
|
|
use ReflectionException; |
|
18
|
|
|
|
|
19
|
|
|
use Kenjis\MonkeyPatch\Patcher\ConstantPatcher; |
|
20
|
|
|
use Kenjis\MonkeyPatch\Patcher\Backtrace; |
|
21
|
|
|
use Kenjis\MonkeyPatch\MonkeyPatchManager; |
|
22
|
|
|
use Kenjis\MonkeyPatch\Cache; |
|
23
|
|
|
use Kenjis\MonkeyPatch\InvocationVerifier; |
|
24
|
|
|
|
|
25
|
|
|
class Proxy |
|
26
|
|
|
{ |
|
27
|
|
|
private static $patches = []; |
|
28
|
|
|
private static $patches_to_apply = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Set a constant patch |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $constant constant name |
|
34
|
|
|
* @param mixed $value value |
|
35
|
|
|
* @param string $class_method class::method to apply this patch |
|
36
|
|
|
* |
|
37
|
|
|
* @throws LogicException |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function patch($constant, $value, $class_method = null) |
|
40
|
|
|
{ |
|
41
|
|
|
self::$patches[$constant] = $value; |
|
42
|
|
|
self::$patches_to_apply[$constant] = strtolower($class_method); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Clear all patches and invocation data |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function reset() |
|
49
|
|
|
{ |
|
50
|
|
|
self::$patches = []; |
|
51
|
|
|
self::$patches_to_apply = []; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected static function logInvocation($constant) |
|
55
|
|
|
{ |
|
56
|
|
|
if (MonkeyPatchManager::$debug) |
|
57
|
|
|
{ |
|
58
|
|
|
$trace = debug_backtrace(); |
|
59
|
|
|
$info = Backtrace::getInfo('ConstantPatcher', $trace); |
|
60
|
|
|
|
|
61
|
|
|
$file = $info['file']; |
|
62
|
|
|
$line = $info['line']; |
|
63
|
|
|
$method = isset($info['class_method']) ? $info['class_method'] : $info['function']; |
|
64
|
|
|
|
|
65
|
|
|
MonkeyPatchManager::log( |
|
66
|
|
|
'invoke_const: ' . $constant . ') on line ' . $line . ' in ' . $file . ' by ' . $method . '()' |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
View Code Duplication |
protected static function checkCalledMethod($constant) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$trace = debug_backtrace(); |
|
74
|
|
|
$info = Backtrace::getInfo('ConstantPatcher', $trace); |
|
75
|
|
|
|
|
76
|
|
|
$class = strtolower($info['class']); |
|
77
|
|
|
$class_method = strtolower($info['class_method']); |
|
78
|
|
|
|
|
79
|
|
|
// Patches the constants only in the class |
|
80
|
|
|
if (strpos(self::$patches_to_apply[$constant], '::') === false) |
|
81
|
|
|
{ |
|
82
|
|
|
if (self::$patches_to_apply[$constant] !== $class) |
|
83
|
|
|
{ |
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
//Patches the constants only in the class method |
|
89
|
|
|
else |
|
90
|
|
|
{ |
|
91
|
|
|
if (self::$patches_to_apply[$constant] !== $class_method) |
|
92
|
|
|
{ |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get patched constant value |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $constant |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function get($constant) |
|
106
|
|
|
{ |
|
107
|
|
|
self::logInvocation($constant); |
|
108
|
|
|
|
|
109
|
|
View Code Duplication |
if (isset(self::$patches_to_apply[$constant])) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
if (! self::checkCalledMethod($constant)) |
|
112
|
|
|
{ |
|
113
|
|
|
MonkeyPatchManager::log( |
|
114
|
|
|
'invoke_const: ' . $constant . ' not patched (out of scope)' |
|
115
|
|
|
); |
|
116
|
|
|
return constant($constant); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (array_key_exists($constant, self::$patches)) |
|
121
|
|
|
{ |
|
122
|
|
|
MonkeyPatchManager::log('invoke_const: ' . $constant . ' patched'); |
|
123
|
|
|
return self::$patches[$constant]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
MonkeyPatchManager::log( |
|
127
|
|
|
'invoke_const: ' . $constant . ' not patched (no patch)' |
|
128
|
|
|
); |
|
129
|
|
|
return constant($constant); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.