|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Package\Compiler; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayObject; |
|
8
|
|
|
use BEAR\AppMeta\AbstractAppMeta; |
|
9
|
|
|
use BEAR\AppMeta\Meta; |
|
10
|
|
|
use BEAR\Package\Injector; |
|
11
|
|
|
|
|
12
|
|
|
use function realpath; |
|
13
|
|
|
use function sprintf; |
|
14
|
|
|
|
|
15
|
|
|
final class CompilePreload |
|
16
|
|
|
{ |
|
17
|
|
|
/** @param ArrayObject<int, string> $classes */ |
|
18
|
|
|
public function __construct( |
|
19
|
|
|
private FakeRun $fakeRun, |
|
20
|
|
|
private CompileAutoload $dumpAutoload, |
|
21
|
|
|
private FilePutContents $filePutContents, |
|
22
|
|
|
private ArrayObject $classes, |
|
23
|
|
|
private string $context, |
|
24
|
|
|
) { |
|
25
|
|
|
$this->fakeRun = $fakeRun; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function __invoke(AbstractAppMeta $appMeta, string $context): string |
|
29
|
|
|
{ |
|
30
|
|
|
($this->fakeRun)(); |
|
31
|
|
|
$this->loadResources($appMeta->name, $context, $appMeta->appDir); |
|
32
|
|
|
/** @var list<string> $classes */ |
|
33
|
|
|
$classes = (array) $this->classes; |
|
34
|
|
|
$paths = $this->dumpAutoload->getPaths($classes); |
|
|
|
|
|
|
35
|
|
|
$requiredOnceFile = ''; |
|
36
|
|
|
foreach ($paths as $path) { |
|
37
|
|
|
$requiredOnceFile .= sprintf( |
|
38
|
|
|
"require %s;\n", |
|
39
|
|
|
$path, |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$preloadFile = sprintf("<?php |
|
44
|
|
|
|
|
45
|
|
|
// %s preload |
|
46
|
|
|
require __DIR__ . '/vendor/autoload.php'; |
|
47
|
|
|
|
|
48
|
|
|
%s", $this->context, $requiredOnceFile); |
|
49
|
|
|
$fileName = realpath($appMeta->appDir) . '/preload.php'; |
|
50
|
|
|
($this->filePutContents)($fileName, $preloadFile); |
|
51
|
|
|
|
|
52
|
|
|
return $fileName; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function loadResources(string $appName, string $context, string $appDir): void |
|
56
|
|
|
{ |
|
57
|
|
|
$meta = new Meta($appName, $context, $appDir); |
|
58
|
|
|
$injector = Injector::getInstance($appName, $context, $appDir); |
|
59
|
|
|
|
|
60
|
|
|
$resMetas = $meta->getGenerator('*'); |
|
61
|
|
|
foreach ($resMetas as $resMeta) { |
|
62
|
|
|
$injector->getInstance($resMeta->class); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|