Completed
Push — 1.x ( fd0c91...cdd807 )
by Akihito
03:35
created

CompileDiScripts::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
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;
0 ignored issues
show
Bug introduced by
The variable $className does not exist. Did you forget to declare it?

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.

Loading history...
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
0 ignored issues
show
Documentation introduced by
The doc-type class-string<T> could not be parsed: Unknown type name "class-string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
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