|
2 | 2 |
|
3 | 3 | namespace Pre\Plugin; |
4 | 4 |
|
5 | | -function find($file, $iterations = 10, $prefix = __DIR__) |
6 | | -{ |
7 | | - $folder = "../"; |
| 5 | +if (!function_exists("\\Pre\\Plugin\\find")) { |
| 6 | + function find($file, $iterations = 10, $prefix = __DIR__) { |
| 7 | + $folder = "../"; |
8 | 8 |
|
9 | | - if ($prefix) { |
10 | | - $folder = "{$prefix}/{$folder}"; |
11 | | - } |
| 9 | + if ($prefix) { |
| 10 | + $folder = "{$prefix}/{$folder}"; |
| 11 | + } |
12 | 12 |
|
13 | | - for ($i = 0; $i < $iterations; $i++) { |
14 | | - $try = "{$folder}{$file}"; |
| 13 | + for ($i = 0; $i < $iterations; $i++) { |
| 14 | + $try = "{$folder}{$file}"; |
15 | 15 |
|
16 | | - if (file_exists($try)) { |
17 | | - return realpath($try); |
18 | | - } |
| 16 | + if (file_exists($try)) { |
| 17 | + return realpath($try); |
| 18 | + } |
19 | 19 |
|
20 | | - $folder .= "../"; |
| 20 | + $folder .= "../"; |
| 21 | + } |
21 | 22 | } |
22 | 23 | } |
23 | 24 |
|
24 | | -function base() |
25 | | -{ |
26 | | - $vendor = find("vendor"); |
27 | | - return realpath("{$vendor}/../"); |
| 25 | +if (!function_exists("\\Pre\\Plugin\\base")) { |
| 26 | + function base() { |
| 27 | + $vendor = find("vendor"); |
| 28 | + return realpath("{$vendor}/../"); |
| 29 | + } |
28 | 30 | } |
29 | 31 |
|
30 | | -function defer($code) |
31 | | -{ |
32 | | - $hidden = realpath(__DIR__ . "/../hidden/vendor/autoload.php"); |
33 | | - $visible = find("autoload.php"); |
34 | | - |
35 | | - if (!$visible) { |
36 | | - // the plugin is being used/tested directly |
37 | | - $visible = __DIR__ . "/../vendor/autoload.php"; |
38 | | - } |
39 | 32 |
|
40 | | - $defer = " |
41 | | - require '{$hidden}'; |
42 | | - require '{$visible}'; |
| 33 | +if (!function_exists("\\Pre\\Plugin\\defer")) { |
| 34 | + function defer($code) { |
| 35 | + $hidden = realpath(__DIR__ . "/../hidden/vendor/autoload.php"); |
| 36 | + $visible = find("autoload.php"); |
43 | 37 |
|
44 | | - \$function = function() { |
45 | | - {$code}; |
46 | | - }; |
| 38 | + if (!$visible) { |
| 39 | + // the plugin is being used/tested directly |
| 40 | + $visible = __DIR__ . "/../vendor/autoload.php"; |
| 41 | + } |
47 | 42 |
|
48 | | - print base64_encode(serialize(\$function())); |
49 | | - "; |
| 43 | + $defer = " |
| 44 | + require '{$hidden}'; |
| 45 | + require '{$visible}'; |
50 | 46 |
|
51 | | - $result = exec( |
52 | | - "php -r 'eval(base64_decode(\"" . base64_encode($defer) . "\"));'" |
53 | | - ); |
54 | | - |
55 | | - return unserialize(base64_decode($result)); |
56 | | -} |
| 47 | + \$function = function() { |
| 48 | + {$code}; |
| 49 | + }; |
| 50 | +
|
| 51 | + print base64_encode(serialize(\$function())); |
| 52 | + "; |
57 | 53 |
|
58 | | -function instance() |
59 | | -{ |
60 | | - static $instance = null; |
| 54 | + $result = exec( |
| 55 | + "php -r 'eval(base64_decode(\"" . base64_encode($defer) . "\"));'" |
| 56 | + ); |
61 | 57 |
|
62 | | - if ($instance === null) { |
63 | | - $instance = new Parser(); |
| 58 | + return unserialize(base64_decode($result)); |
64 | 59 | } |
| 60 | +} |
| 61 | + |
| 62 | +if (!function_exists("\\Pre\\Plugin\\instance")) { |
| 63 | + function instance() { |
| 64 | + static $instance = null; |
| 65 | + |
| 66 | + if ($instance === null) { |
| 67 | + $instance = new Parser(); |
| 68 | + } |
65 | 69 |
|
66 | | - return $instance; |
| 70 | + return $instance; |
| 71 | + } |
67 | 72 | } |
68 | 73 |
|
69 | | -function process($from) |
70 | | -{ |
71 | | - $instance = instance(); |
72 | | - return $instance->process($from); |
| 74 | +if (!function_exists("\\Pre\\Plugin\\process")) { |
| 75 | + function process($from) { |
| 76 | + $instance = instance(); |
| 77 | + return $instance->process($from); |
| 78 | + } |
73 | 79 | } |
74 | 80 |
|
75 | | -function compile($from, $to, $format = true, $comment = true) |
76 | | -{ |
77 | | - $instance = instance(); |
78 | | - return $instance->compile($from, $to, $format, $comment); |
| 81 | +if (!function_exists("\\Pre\\Plugin\\compile")) { |
| 82 | + function compile($from, $to, $format = true, $comment = true) { |
| 83 | + $instance = instance(); |
| 84 | + return $instance->compile($from, $to, $format, $comment); |
| 85 | + } |
79 | 86 | } |
80 | 87 |
|
81 | | -function parse($code) |
82 | | -{ |
83 | | - $instance = instance(); |
84 | | - return $instance->parse($code); |
| 88 | +if (!function_exists("\\Pre\\Plugin\\parse")) { |
| 89 | + function parse($code) { |
| 90 | + $instance = instance(); |
| 91 | + return $instance->parse($code); |
| 92 | + } |
85 | 93 | } |
86 | 94 |
|
87 | | -function format($code) |
88 | | -{ |
89 | | - $instance = instance(); |
90 | | - return $instance->format($code); |
| 95 | +if (!function_exists("\\Pre\\Plugin\\format")) { |
| 96 | + function format($code) { |
| 97 | + $instance = instance(); |
| 98 | + return $instance->format($code); |
| 99 | + } |
91 | 100 | } |
92 | 101 |
|
93 | | -function addMacro($macro) |
94 | | -{ |
95 | | - $instance = instance(); |
96 | | - return $instance->addMacro($macro); |
| 102 | +if (!function_exists("\\Pre\\Plugin\\addMacro")) { |
| 103 | + function addMacro($macro) { |
| 104 | + $instance = instance(); |
| 105 | + return $instance->addMacro($macro); |
| 106 | + } |
97 | 107 | } |
98 | 108 |
|
99 | | -function removeMacro($macro) |
100 | | -{ |
101 | | - $instance = instance(); |
102 | | - return $instance->removeMacro($macro); |
| 109 | +if (!function_exists("\\Pre\\Plugin\\removeMacro")) { |
| 110 | + function removeMacro($macro) { |
| 111 | + $instance = instance(); |
| 112 | + return $instance->removeMacro($macro); |
| 113 | + } |
103 | 114 | } |
104 | 115 |
|
105 | | -function addCompiler($compiler, $callable) |
106 | | -{ |
107 | | - $instance = instance(); |
108 | | - return $instance->addCompiler($compiler, $callable); |
| 116 | +if (!function_exists("\\Pre\\Plugin\\addCompiler")) { |
| 117 | + function addCompiler($compiler, $callable) { |
| 118 | + $instance = instance(); |
| 119 | + return $instance->addCompiler($compiler, $callable); |
| 120 | + } |
109 | 121 | } |
110 | 122 |
|
111 | | -function removeCompiler($compiler) |
112 | | -{ |
113 | | - $instance = instance(); |
114 | | - return $instance->removeCompiler($compiler); |
| 123 | +if (!function_exists("\\Pre\\Plugin\\removeCompiler")) { |
| 124 | + function removeCompiler($compiler) { |
| 125 | + $instance = instance(); |
| 126 | + return $instance->removeCompiler($compiler); |
| 127 | + } |
115 | 128 | } |
116 | 129 |
|
117 | | -function addFunction(...$args) |
118 | | -{ |
119 | | - $instance = instance(); |
120 | | - return $instance->addFunction(...$args); |
| 130 | +if (!function_exists("\\Pre\\Plugin\\addFunction")) { |
| 131 | + function addFunction(...$args) { |
| 132 | + $instance = instance(); |
| 133 | + return $instance->addFunction(...$args); |
| 134 | + } |
121 | 135 | } |
122 | 136 |
|
123 | | -function getFunction(...$args) |
124 | | -{ |
125 | | - $instance = instance(); |
126 | | - return $instance->getFunction(...$args); |
| 137 | +if (!function_exists("\\Pre\\Plugin\\getFunction")) { |
| 138 | + function getFunction(...$args) { |
| 139 | + $instance = instance(); |
| 140 | + return $instance->getFunction(...$args); |
| 141 | + } |
127 | 142 | } |
0 commit comments