1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\Package; |
6
|
|
|
|
7
|
|
|
use BEAR\AppMeta\AbstractAppMeta; |
8
|
|
|
use BEAR\AppMeta\Meta; |
9
|
|
|
use BEAR\AppMeta\ResMeta; |
10
|
|
|
use BEAR\Package\Provide\Error\NullPage; |
11
|
|
|
use BEAR\Resource\Exception\ParameterException; |
12
|
|
|
use BEAR\Resource\NamedParameterInterface; |
13
|
|
|
use BEAR\Resource\ResourceObject; |
14
|
|
|
use BEAR\Resource\Uri; |
15
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
16
|
|
|
use Doctrine\Common\Annotations\Reader; |
17
|
|
|
use Doctrine\Common\Cache\Cache; |
18
|
|
|
use function file_exists; |
19
|
|
|
use Ray\Di\AbstractModule; |
20
|
|
|
use Ray\Di\Bind; |
21
|
|
|
use Ray\Di\InjectorInterface; |
22
|
|
|
use ReflectionClass; |
23
|
|
|
|
24
|
|
|
final class Compiler |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* string[] |
28
|
|
|
*/ |
29
|
|
|
private $classes = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Compile application |
33
|
|
|
* |
34
|
|
|
* @param string $appName application name "MyVendor|MyProject" |
35
|
|
|
* @param string $context application context "prod-app" |
36
|
|
|
* @param string $appDir application path |
37
|
|
|
*/ |
38
|
|
|
public function __invoke(string $appName, string $context, string $appDir) : string |
39
|
|
|
{ |
40
|
|
|
$this->registerLoader($appDir); |
41
|
|
|
$autoload = $this->compileAutoload($appName, $context, $appDir); |
42
|
|
|
$log = $this->compileDiScripts($appName, $context, $appDir); |
43
|
|
|
$preload = $this->compilePreload($appName, $context, $appDir); |
44
|
|
|
|
45
|
|
|
return sprintf("Compile Log: %s\nautoload.php: %s\npreload.php: %s", $log, $autoload, $preload); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function registerLoader(string $appDir) : void |
49
|
|
|
{ |
50
|
|
|
$loaderFile = $appDir . '/vendor/autoload.php'; |
51
|
|
|
if (! file_exists($loaderFile)) { |
52
|
|
|
throw new \RuntimeException('no loader'); |
53
|
|
|
} |
54
|
|
|
$loaderFile = require $loaderFile; |
55
|
|
|
spl_autoload_register( |
56
|
|
|
function ($class) use ($loaderFile) { |
57
|
|
|
$loaderFile->loadClass($class); |
58
|
|
|
if ($class !== NullPage::class) { |
59
|
|
|
$this->classes[] = $class; |
60
|
|
|
} |
61
|
|
|
}, |
62
|
|
|
false, |
63
|
|
|
true |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function compileDiScripts(string $appName, string $context, string $appDir) : string |
68
|
|
|
{ |
69
|
|
|
$appMeta = new Meta($appName, $context, $appDir); |
70
|
|
|
$injector = new AppInjector($appName, $context, $appMeta, 'compile'); |
71
|
|
|
$cache = $injector->getInstance(Cache::class); |
72
|
|
|
$reader = $injector->getInstance(AnnotationReader::class); |
73
|
|
|
/* @var $reader \Doctrine\Common\Annotations\Reader */ |
74
|
|
|
$namedParams = $injector->getInstance(NamedParameterInterface::class); |
75
|
|
|
/* @var $namedParams NamedParameterInterface */ |
76
|
|
|
|
77
|
|
|
// create DI factory class and AOP compiled class for all resources and save $app cache. |
78
|
|
|
(new Bootstrap)->newApp($appMeta, $context, $cache); |
79
|
|
|
|
80
|
|
|
// check resource injection and create annotation cache |
81
|
|
|
foreach ($appMeta->getResourceListGenerator() as [$className]) { |
82
|
|
|
$this->scanClass($injector, $reader, $namedParams, (string) $className); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
$logFile = realpath($appMeta->logDir) . '/compile.log'; |
85
|
|
|
$this->saveCompileLog($appMeta, $context, $logFile); |
86
|
|
|
|
87
|
|
|
return $logFile; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function compileAutoload(string $appName, string $context, string $appDir) : string |
91
|
|
|
{ |
92
|
|
|
$this->runBootstrap($context, $appName, $appDir); |
93
|
|
|
$paths = $this->getPaths($this->classes, $appDir); |
94
|
|
|
|
95
|
|
|
return $this->dumpAutoload($appDir, $paths); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function dumpAutoload(string $appDir, array $paths) : string |
99
|
|
|
{ |
100
|
|
|
$autoloadFile = '<?php' . PHP_EOL; |
101
|
|
|
foreach ($paths as $path) { |
102
|
|
|
$autoloadFile .= sprintf( |
103
|
|
|
"require %s';\n", |
104
|
|
|
$this->getRelativePath($appDir, $path) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
$autoloadFile .= "require __DIR__ . '/vendor/autoload.php';" . PHP_EOL; |
108
|
|
|
$loaderFile = realpath($appDir) . '/autoload.php'; |
109
|
|
|
file_put_contents($loaderFile, $autoloadFile); |
110
|
|
|
|
111
|
|
|
return $loaderFile; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function compilePreload(string $appName, string $context, string $appDir) : string |
115
|
|
|
{ |
116
|
|
|
$this->loadResources($appName, $context, $appDir); |
117
|
|
|
$paths = $this->getPaths($this->classes, $appDir); |
118
|
|
|
$output = '<?php' . PHP_EOL; |
119
|
|
|
$output .= "require __DIR__ . '/vendor/autoload.php';" . PHP_EOL; |
120
|
|
|
foreach ($paths as $path) { |
121
|
|
|
$output .= sprintf( |
122
|
|
|
"require_once %s';\n", |
123
|
|
|
$this->getRelativePath($appDir, $path) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
$autoLoadFilesPath = $appDir . '/vendor/composer/autoload_files.php'; |
127
|
|
|
assert(file_exists($autoLoadFilesPath)); |
128
|
|
|
$output = $this->appendIncludedFiles($appDir, require $autoLoadFilesPath, $output); |
129
|
|
|
$diFiles = (array) glob($appDir . "/var/tmp/{$context}/di/*.php"); |
130
|
|
|
$output = $this->appendIncludedFiles($appDir, $diFiles, $output); |
131
|
|
|
$preloadFile = realpath($appDir) . '/preload.php'; |
132
|
|
|
file_put_contents($preloadFile, $output); |
133
|
|
|
|
134
|
|
|
return $preloadFile; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function getRelativePath(string $rootDir, string $file) : string |
138
|
|
|
{ |
139
|
|
|
$dir = (string) realpath($rootDir); |
140
|
|
|
if (strpos($file, $dir) !== false) { |
141
|
|
|
return (string) preg_replace('#^' . preg_quote($dir, '#') . '#', "__DIR__ . '", $file); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $file; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private function runBootstrap(string $context, string $appName, string $appDir) : void |
148
|
|
|
{ |
149
|
|
|
$ro = new NullPage; |
150
|
|
|
$ro->uri = new Uri('app://self/'); |
151
|
|
|
$this->bootstrap($context, $appName, $appDir, $ro); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private function scanClass(InjectorInterface $injector, Reader $reader, NamedParameterInterface $namedParams, string $className) : void |
155
|
|
|
{ |
156
|
|
|
try { |
157
|
|
|
$instance = $injector->getInstance($className); |
158
|
|
|
} catch (\Exception $e) { |
159
|
|
|
error_log(sprintf('Failed to instantiate [%s]: %s(%s) in %s on line %s', $className, get_class($e), $e->getMessage(), $e->getFile(), $e->getLine())); |
160
|
|
|
|
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
assert(class_exists($className)); |
164
|
|
|
$class = new ReflectionClass($className); |
165
|
|
|
$reader->getClassAnnotations($class); |
166
|
|
|
$methods = $class->getMethods(); |
167
|
|
|
foreach ($methods as $method) { |
168
|
|
|
$methodName = $method->getName(); |
|
|
|
|
169
|
|
|
if ($this->isMagicMethod($methodName)) { |
170
|
|
|
continue; |
171
|
|
|
} |
172
|
|
|
$this->saveNamedParam($namedParams, $instance, $methodName); |
173
|
|
|
// method annotation |
174
|
|
|
$reader->getMethodAnnotations($method); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
private function isMagicMethod(string $method) : bool |
179
|
|
|
{ |
180
|
|
|
return \in_array($method, ['__sleep', '__wakeup', 'offsetGet', 'offsetSet', 'offsetExists', 'offsetUnset', 'count', 'ksort', 'asort', 'jsonSerialize'], true); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function saveNamedParam(NamedParameterInterface $namedParameter, $instance, string $method) : void |
184
|
|
|
{ |
185
|
|
|
// named parameter |
186
|
|
|
if (! \in_array($method, ['onGet', 'onPost', 'onPut', 'onPatch', 'onDelete', 'onHead'], true)) { |
187
|
|
|
return; |
188
|
|
|
} |
189
|
|
|
$callable = [$instance, $method]; |
190
|
|
|
if (! is_callable($callable)) { |
191
|
|
|
return; |
192
|
|
|
} |
193
|
|
|
try { |
194
|
|
|
$namedParameter->getParameters($callable, []); |
195
|
|
|
} catch (ParameterException $e) { |
196
|
|
|
return; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private function saveCompileLog(AbstractAppMeta $appMeta, string $context, string $logFile) : void |
201
|
|
|
{ |
202
|
|
|
$module = (new Module)($appMeta, $context); |
203
|
|
|
/** @var AbstractModule $module */ |
204
|
|
|
$container = $module->getContainer(); |
205
|
|
|
foreach ($appMeta->getResourceListGenerator() as [$class]) { |
206
|
|
|
new Bind($container, (string) $class); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
file_put_contents($logFile, (string) $module); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
private function getPaths(array $classes, string $appDir) : array |
212
|
|
|
{ |
213
|
|
|
$paths = []; |
214
|
|
|
foreach ($classes as $class) { |
215
|
|
|
// could be phpdoc tag by annotation loader |
216
|
|
|
$isAutoloadFailed = ! class_exists($class, false) && ! interface_exists($class, false) && ! trait_exists($class, false); |
217
|
|
|
if ($isAutoloadFailed) { |
218
|
|
|
continue; |
219
|
|
|
} |
220
|
|
|
$filePath = (string) (new ReflectionClass($class))->getFileName(); |
221
|
|
|
if (! file_exists($filePath) || strpos($filePath, 'phar') === 0) { |
222
|
|
|
continue; |
223
|
|
|
} |
224
|
|
|
$paths[] = $this->getRelativePath($appDir, $filePath); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $paths; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
private function loadResources(string $appName, string $context, string $appDir) : void |
231
|
|
|
{ |
232
|
|
|
$meta = new Meta($appName, $context, $appDir); |
233
|
|
|
/* @var ResMeta $resMeta */ |
234
|
|
|
$injector = new AppInjector($appName, $context, $meta, 'compile'); |
235
|
|
|
foreach ($meta->getGenerator('*') as $resMeta) { |
236
|
|
|
$injector->getInstance($resMeta->class); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
private function appendIncludedFiles(string $appDir, array $includeFiles, string $output) : string |
241
|
|
|
{ |
242
|
|
|
foreach ($includeFiles as $includeFile) { |
243
|
|
|
$relPath = $this->getRelativePath($appDir, $includeFile); |
244
|
|
|
if (strpos($output, $relPath) === false) { |
245
|
|
|
$output .= sprintf( |
246
|
|
|
"opcache_compile_file(%s');\n", |
247
|
|
|
$relPath |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $output; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
private function bootstrap(string $context, string $name, string $appDir, ResourceObject $ro) |
256
|
|
|
{ |
257
|
|
|
$server = [ |
258
|
|
|
'REQUEST_METHOD' => 'GET', |
259
|
|
|
'REQUEST_URI' => '/' |
260
|
|
|
]; |
261
|
|
|
$globals = [ |
262
|
|
|
'_GET' => [], |
263
|
|
|
'_POST' => [] |
264
|
|
|
]; |
265
|
|
|
$app = (new Bootstrap)->getApp($name, $context, $appDir); |
266
|
|
|
$app->httpCache->isNotModified($server); |
267
|
|
|
$request = $app->router->match($globals, $server); |
268
|
|
|
try { |
269
|
|
|
$response = $app->resource->get->object($ro)(); |
|
|
|
|
270
|
|
|
/* @var ResourceObject $response */ |
271
|
|
|
$response->transfer($app->responder, $server); |
272
|
|
|
} catch (\Exception $e) { |
273
|
|
|
$app->error->handle($e, $request)->transfer(); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.