Completed
Pull Request — 2.x (#216)
by Akihito
07:38
created

OnDemandCompiler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 67
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 21 5
A loadPointcuts() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Compiler;
6
7
use Ray\Aop\Compiler;
8
use Ray\Aop\Pointcut;
9
use Ray\Compiler\Exception\Unbound;
10
use Ray\Di\AbstractModule;
11
use Ray\Di\Bind;
12
use Ray\Di\Dependency;
13
use Ray\Di\Exception\NotFound;
14
15
final class OnDemandCompiler
16
{
17
    /**
18
     * @var string
19
     */
20
    private $scriptDir;
21
22
    /**
23
     * @var ScriptInjector
24
     */
25
    private $injector;
26
27
    /**
28
     * @var AbstractModule
29
     */
30
    private $module;
31
32
    public function __construct(ScriptInjector $injector, string $sctiptDir, AbstractModule $module)
33
    {
34
        $this->scriptDir = $sctiptDir;
35
        $this->injector = $injector;
36
        $this->module = $module;
37
    }
38
39
    /**
40
     * Compile dependency on demand
41
     */
42
    public function __invoke(string $dependencyIndex) : void
43
    {
44
        [$class] = \explode('-', $dependencyIndex);
0 ignored issues
show
Bug introduced by
The variable $class 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
        $containerObject = $this->module->getContainer();
46
        try {
47
            new Bind($containerObject, $class);
48
        } catch (NotFound $e) {
49
            throw new Unbound($dependencyIndex, 0, $e);
50
        }
51
        $containerArray = $containerObject->getContainer();
52
        if (! isset($containerArray[$dependencyIndex])) {
53
            throw new Unbound($dependencyIndex, 0);
54
        }
55
        $dependency = $containerArray[$dependencyIndex];
56
        $pointCuts = $this->loadPointcuts();
57
        if ($dependency instanceof Dependency && \is_array($pointCuts)) {
58
            $dependency->weaveAspects(new Compiler($this->scriptDir), $pointCuts);
59
        }
60
        $code = (new DependencyCode($containerObject, $this->injector))->getCode($dependency);
61
        (new DependencySaver($this->scriptDir))($dependencyIndex, $code);
62
    }
63
64
    /**
65
     * @return array<Pointcut>|false
66
     */
67
    private function loadPointcuts()
68
    {
69
        $pointcutsPath = $this->scriptDir . ScriptInjector::AOP;
70
        if (! \file_exists($pointcutsPath)) {
71
            return false;
72
        }
73
        $serialized = \file_get_contents($pointcutsPath);
74
        assert(! is_bool($serialized));
75
        $er = error_reporting(error_reporting() ^ E_NOTICE);
76
        $pointcuts = \unserialize($serialized, ['allowed_classes' => true]);
77
        error_reporting($er);
78
79
        return $pointcuts;
80
    }
81
}
82