|
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
|
|
|
|
|
11
|
|
|
use function realpath; |
|
12
|
|
|
use function sprintf; |
|
13
|
|
|
|
|
14
|
|
|
final class CompilePreload |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var NewInstance */ |
|
17
|
|
|
private $newInstance; |
|
18
|
|
|
|
|
19
|
|
|
/** @var CompileAutoload */ |
|
20
|
|
|
private $dumpAutoload; |
|
21
|
|
|
|
|
22
|
|
|
/** @var ArrayObject<int, string> */ |
|
23
|
|
|
private $classes; |
|
24
|
|
|
|
|
25
|
|
|
/** @var FilePutContents */ |
|
26
|
|
|
private $filePutContents; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
private $context; |
|
30
|
|
|
|
|
31
|
|
|
/** @var FakeRun */ |
|
32
|
|
|
private $fakeRun; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ArrayObject<int, string> $classes |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(FakeRun $fakeRun, NewInstance $newInstance, CompileAutoload $dumpAutoload, FilePutContents $filePutContents, ArrayObject $classes, string $context) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->fakeRun = $fakeRun; |
|
40
|
|
|
$this->newInstance = $newInstance; |
|
41
|
|
|
$this->dumpAutoload = $dumpAutoload; |
|
42
|
|
|
$this->classes = $classes; |
|
43
|
|
|
$this->filePutContents = $filePutContents; |
|
44
|
|
|
$this->context = $context; |
|
45
|
|
|
$this->fakeRun = $fakeRun; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function __invoke(AbstractAppMeta $appMeta, string $context): string |
|
49
|
|
|
{ |
|
50
|
|
|
($this->fakeRun)(); |
|
51
|
|
|
$this->loadResources($appMeta->name, $context, $appMeta->appDir); |
|
52
|
|
|
/** @var list<string> $classes */ |
|
53
|
|
|
$classes = (array) $this->classes; |
|
54
|
|
|
$paths = $this->dumpAutoload->getPaths($classes); |
|
|
|
|
|
|
55
|
|
|
$requiredOnceFile = ''; |
|
56
|
|
|
foreach ($paths as $path) { |
|
57
|
|
|
$requiredOnceFile .= sprintf( |
|
58
|
|
|
"require %s;\n", |
|
59
|
|
|
$path |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$preloadFile = sprintf("<?php |
|
64
|
|
|
|
|
65
|
|
|
// %s preload |
|
66
|
|
|
require __DIR__ . '/vendor/autoload.php'; |
|
67
|
|
|
|
|
68
|
|
|
%s", $this->context, $requiredOnceFile); |
|
69
|
|
|
$fileName = realpath($appMeta->appDir) . '/preload.php'; |
|
70
|
|
|
($this->filePutContents)($fileName, $preloadFile); |
|
71
|
|
|
|
|
72
|
|
|
return $fileName; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function loadResources(string $appName, string $context, string $appDir): void |
|
76
|
|
|
{ |
|
77
|
|
|
$meta = new Meta($appName, $context, $appDir); |
|
78
|
|
|
|
|
79
|
|
|
$resMetas = $meta->getGenerator('*'); |
|
80
|
|
|
foreach ($resMetas as $resMeta) { |
|
81
|
|
|
($this->newInstance)($resMeta->class); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|