|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Package\Compiler; |
|
6
|
|
|
|
|
7
|
|
|
use BEAR\AppMeta\AbstractAppMeta; |
|
8
|
|
|
use BEAR\Resource\NamedParameterInterface; |
|
9
|
|
|
use BEAR\Sunday\Extension\Application\AppInterface; |
|
10
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
11
|
|
|
use Ray\Di\InjectorInterface; |
|
12
|
|
|
|
|
13
|
|
|
use function assert; |
|
14
|
|
|
use function class_exists; |
|
15
|
|
|
|
|
16
|
|
|
final class CompileDiScripts |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var CompileClassMetaInfo */ |
|
19
|
|
|
private $compilerScanClass; |
|
20
|
|
|
|
|
21
|
|
|
/** @var InjectorInterface */ |
|
22
|
|
|
private $injector; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(CompileClassMetaInfo $compilerScanClass, InjectorInterface $injector) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->compilerScanClass = $compilerScanClass; |
|
27
|
|
|
$this->injector = $injector; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function __invoke(AbstractAppMeta $appMeta): void |
|
31
|
|
|
{ |
|
32
|
|
|
$reader = $this->injector->getInstance(Reader::class); |
|
33
|
|
|
assert($reader instanceof Reader); |
|
34
|
|
|
$namedParams = $this->injector->getInstance(NamedParameterInterface::class); |
|
35
|
|
|
assert($namedParams instanceof NamedParameterInterface); |
|
36
|
|
|
// create DI factory class and AOP compiled class for all resources and save $app cache. |
|
37
|
|
|
$app = $this->injector->getInstance(AppInterface::class); |
|
38
|
|
|
assert($app instanceof AppInterface); |
|
39
|
|
|
|
|
40
|
|
|
// check resource injection and create annotation cache |
|
41
|
|
|
$metas = $appMeta->getResourceListGenerator(); |
|
42
|
|
|
/** @var array{0: string, 1:string} $meta */ |
|
43
|
|
|
foreach ($metas as $meta) { |
|
44
|
|
|
[$className] = $meta; |
|
|
|
|
|
|
45
|
|
|
assert(class_exists($className)); |
|
46
|
|
|
$this->scanClass($reader, $namedParams, $className); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Save annotation and method meta information |
|
52
|
|
|
* |
|
53
|
|
|
* @param class-string<T> $className |
|
|
|
|
|
|
54
|
|
|
* |
|
55
|
|
|
* @template T |
|
56
|
|
|
*/ |
|
57
|
|
|
private function scanClass(Reader $reader, NamedParameterInterface $namedParams, string $className): void |
|
58
|
|
|
{ |
|
59
|
|
|
($this->compilerScanClass)($reader, $namedParams, $className); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
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.