-
Notifications
You must be signed in to change notification settings - Fork 41
/
Patchwork.php
144 lines (115 loc) · 3.24 KB
/
Patchwork.php
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
<?php
/**
* @author Ignas Rudaitis <[email protected]>
* @copyright 2010-2023 Ignas Rudaitis
* @license http://www.opensource.org/licenses/mit-license.html
*/
namespace Patchwork;
if (function_exists('Patchwork\replace')) {
return;
}
require_once __DIR__ . '/src/Exceptions.php';
require_once __DIR__ . '/src/CallRerouting.php';
require_once __DIR__ . '/src/CodeManipulation.php';
require_once __DIR__ . '/src/Utils.php';
require_once __DIR__ . '/src/Stack.php';
require_once __DIR__ . '/src/Config.php';
function redefine($subject, callable $content)
{
$handle = null;
foreach (array_slice(func_get_args(), 1) as $content) {
$handle = CallRerouting\connect($subject, $content, $handle);
}
$handle->silence();
return $handle;
}
function relay(?array $args = null)
{
return CallRerouting\relay($args);
}
function fallBack()
{
throw new Exceptions\NoResult;
}
function restore(CallRerouting\Handle $handle)
{
$handle->expire();
}
function restoreAll()
{
CallRerouting\disconnectAll();
}
function silence(CallRerouting\Handle $handle)
{
$handle->silence();
}
function assertEventuallyDefined(CallRerouting\Handle $handle)
{
$handle->unsilence();
}
function getClass()
{
return Stack\top('class');
}
function getCalledClass()
{
return Stack\topCalledClass();
}
function getFunction()
{
return Stack\top('function');
}
function getMethod()
{
return getClass() . '::' . getFunction();
}
function configure()
{
Config\locate();
}
function hasMissed($callable)
{
return Utils\callableWasMissed($callable);
}
function always($value)
{
return function() use ($value) {
return $value;
};
}
Utils\alias('Patchwork', [
'redefine' => ['replace', 'replaceLater'],
'relay' => 'callOriginal',
'fallBack' => 'pass',
'restore' => 'undo',
'restoreAll' => 'undoAll',
]);
configure();
Utils\markMissedCallables();
CodeManipulation\Stream::discoverOtherWrapper();
CodeManipulation\Stream::wrap();
CodeManipulation\register([
CodeManipulation\Actions\CodeManipulation\propagateThroughEval(),
CodeManipulation\Actions\CallRerouting\injectCallInterceptionCode(),
CodeManipulation\Actions\RedefinitionOfInternals\spliceNamedFunctionCalls(),
CodeManipulation\Actions\RedefinitionOfInternals\spliceDynamicCalls(),
CodeManipulation\Actions\RedefinitionOfNew\spliceAllInstantiations,
CodeManipulation\Actions\RedefinitionOfNew\publicizeConstructors,
CodeManipulation\Actions\ConflictPrevention\preventImportingOtherCopiesOfPatchwork(),
]);
CodeManipulation\onImport([
CodeManipulation\Actions\CallRerouting\markPreprocessedFiles(),
]);
Utils\clearOpcodeCaches();
register_shutdown_function('Patchwork\Utils\clearOpcodeCaches');
CallRerouting\createStubsForInternals();
CallRerouting\connectDefaultInternals();
require __DIR__ . '/src/Redefinitions/LanguageConstructs.php';
CodeManipulation\register([
CodeManipulation\Actions\RedefinitionOfLanguageConstructs\spliceAllConfiguredLanguageConstructs(),
CodeManipulation\Actions\CallRerouting\injectQueueDeploymentCode(),
CodeManipulation\Actions\CodeManipulation\injectStreamWrapperReinstatementCode(),
]);
if (Utils\wasRunAsConsoleApp()) {
require __DIR__ . '/src/Console.php';
}