-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGoFunctionExecutor.php
More file actions
257 lines (211 loc) · 6.25 KB
/
Copy pathGoFunctionExecutor.php
File metadata and controls
257 lines (211 loc) · 6.25 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
<?php
namespace GT\Cron;
use Gt\Config\Config;
use Gt\Config\ConfigFactory;
use Gt\ServiceContainer\Container;
use Gt\ServiceContainer\Injector;
use ReflectionClass;
class GoFunctionExecutor {
private ?Config $config = null;
public function __construct(
private readonly string $projectDirectory,
) {
}
public function execute(CronScript $cronScript):void {
$this->withProjectDirectory(function() use($cronScript):void {
$this->loadProjectAutoloader();
$config = $this->loadConfig();
$this->setupProjectAutoloader($config);
$container = $this->createContainer($config);
$container->set(Input::fromQuery($cronScript->getQuery()));
$injector = new Injector($container);
$functionName = $this->loadGoFunction($cronScript->getPath());
$this->withProjectDirectory(
fn() => $injector->invoke(null, $functionName)
);
});
}
private function withProjectDirectory(callable $callback):void {
$originalDirectory = getcwd();
if(is_dir($this->projectDirectory)) {
chdir($this->projectDirectory);
}
try {
$callback();
}
finally {
if($originalDirectory !== false && is_dir($originalDirectory)) {
chdir($originalDirectory);
}
}
}
private function loadProjectAutoloader():void {
$autoloadPath = implode(DIRECTORY_SEPARATOR, [
$this->projectDirectory,
"vendor",
"autoload.php",
]);
if(is_file($autoloadPath)) {
require_once $autoloadPath;
}
}
private function loadConfig():Config {
if($this->config) {
return $this->config;
}
$defaultConfigPath = implode(DIRECTORY_SEPARATOR, [
$this->projectDirectory,
"vendor",
"phpgt",
"webengine",
"config.default.ini",
]);
if($this->hasProjectConfigFiles() || is_file($defaultConfigPath)) {
$this->config = ConfigFactory::createForProject(
$this->projectDirectory,
is_file($defaultConfigPath) ? $defaultConfigPath : null,
);
}
else {
$this->config = new Config();
}
return $this->config;
}
private function setupProjectAutoloader(Config $config):void {
$appNamespace = $config->getString("app.namespace");
$classDir = $config->getString("app.class_dir");
if(!$appNamespace || !$classDir) {
return;
}
$classDir = $this->resolveProjectPath($classDir);
(new ProjectAutoloader($appNamespace, $classDir))->setup();
}
private function createContainer(Config $config):Container {
$container = new Container();
$container->set($config);
$defaultLoaderClass = "GT\\WebEngine\\Service\\DefaultServiceLoader";
if(class_exists($defaultLoaderClass)) {
$container->addLoaderClass(new $defaultLoaderClass($config, $container));
}
$customLoaderClass = $this->getProjectServiceLoaderClass($config);
if($customLoaderClass && class_exists($customLoaderClass)) {
$container->addLoaderClass(new $customLoaderClass($config, $container));
}
return $container;
}
private function getProjectServiceLoaderClass(Config $config):?string {
$appNamespace = trim((string)$config->get("app.namespace"), "\\");
$serviceLoaderClass = trim((string)$config->get("app.service_loader"), "\\");
if($appNamespace === "" || $serviceLoaderClass === "") {
return null;
}
return implode("\\", [
$appNamespace,
$serviceLoaderClass,
]);
}
private function hasProjectConfigFiles():bool {
$configFileList = [
"config.default.ini",
"config.ini",
"config.dev.ini",
"config.deploy.ini",
"config.production.ini",
];
foreach($configFileList as $fileName) {
if(is_file($this->projectDirectory . DIRECTORY_SEPARATOR . $fileName)) {
return true;
}
}
return false;
}
private function resolveProjectPath(string $path):string {
if(str_starts_with($path, "/") || preg_match('/^[A-Za-z]:[\\\\\/]/', $path)) {
return $path;
}
return $this->projectDirectory . DIRECTORY_SEPARATOR . $path;
}
/** @SuppressWarnings(PHPMD.EvalExpression) */
private function loadGoFunction(string $path):string {
$path = realpath($path) ?: $path;
$namespace = "GT\\Cron\\Runtime\\File_" . md5($path);
$functionName = "$namespace\\go";
if(function_exists($functionName)) {
return $functionName;
}
$code = file_get_contents($path);
if($code === false) {
throw new FunctionExecutionException("$path::go");
}
$code = preg_replace('/^\xEF\xBB\xBF/', '', $code) ?? $code;
$code = preg_replace('/^\s*<\?(php)?/i', '', $code, 1) ?? $code;
$code = preg_replace('/\?>\s*$/', '', $code, 1) ?? $code;
$code = $this->replaceMagicConstants($code, $path);
eval("namespace $namespace;\n" . $this->getInternalAliasStatements($code) . $code);
return $functionName;
}
private function getInternalAliasStatements(string $code):string {
$aliasList = [];
$existingAliasList = $this->getExistingUseAliasList($code);
foreach([
...get_declared_classes(),
...get_declared_interfaces(),
...get_declared_traits(),
] as $className) {
$reflection = new ReflectionClass($className);
if(!$reflection->isInternal()) {
continue;
}
$shortName = $reflection->getShortName();
if(isset($existingAliasList[strtolower($shortName)])) {
continue;
}
$aliasList[$shortName] = "use \\" . ltrim($className, "\\") . ";\n";
}
ksort($aliasList);
return implode("", $aliasList);
}
/** @return array<string, true> */
private function getExistingUseAliasList(string $code):array {
$aliasList = [];
preg_match_all(
'/^\s*use\s+(?!function\b|const\b)([^;]+);/mi',
$code,
$matches
);
foreach($matches[1] as $useStatement) {
foreach(explode(",", $useStatement) as $usePart) {
$usePart = trim($usePart);
if(preg_match('/\s+as\s+(\w+)$/i', $usePart, $aliasMatch)) {
$alias = $aliasMatch[1];
}
else {
$alias = basename(str_replace("\\", "/", $usePart));
}
$aliasList[strtolower($alias)] = true;
}
}
return $aliasList;
}
private function replaceMagicConstants(string $code, string $path):string {
$file = var_export($path, true);
$directory = var_export(dirname($path), true);
$output = "";
foreach(token_get_all("<?php\n" . $code) as $index => $token) {
if($index === 0) {
continue;
}
if(!is_array($token)) {
$output .= $token;
continue;
}
$output .= match($token[0]) {
T_FILE => $file,
T_DIR => $directory,
T_NAME_QUALIFIED => "\\" . $token[1],
default => $token[1],
};
}
return $output;
}
}