Completed
Pull Request — 2.x (#117)
by Akihito
01:19
created

Weaver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
final class Weaver
8
{
9
    /**
10
     * @var BindInterface
11
     */
12
    private $bind;
13
14
    /**
15
     * @var string
16
     */
17
    private $bindName;
18
19
    /**
20
     * @var string
21
     */
22
    private $classDir;
23
24
    /**
25
     * @var AopClassName
26
     */
27
    private $aopClassName;
28
29
    /**
30
     * @var string[]
31
     */
32
    private static $classDirs = [];
33
34
    /**
35
     * @var Compiler
36
     */
37
    private $compiler;
38
39
    public function __construct(BindInterface $bind, string $classDir)
40
    {
41
        $this->bind = $bind;
42
        $this->bindName = $bind->toString('');
43
        $this->compiler = new Compiler($classDir);
44
        $this->classDir = $classDir;
45
        $this->aopClassName = new AopClassName;
46
        $this->regsterLoader();
47
    }
48
49
    public function __wakeup()
50
    {
51
        $this->regsterLoader();
52
    }
53
54
    public function newInstance(string $class, array $args)
55
    {
56
        $aopClass = $this->weave($class);
57
        $instance = (new ReflectionClass($aopClass))->newInstanceArgs($args);
58
        $instance->bindings = $this->bind->getBindings();
59
60
        return $instance;
61
    }
62
63
    public function weave(string $class) : string
64
    {
65
        $aopClass = ($this->aopClassName)($class, $this->bindName);
66
        if (! class_exists($aopClass)) {
67
            $this->compiler->compile($class, $this->bind);
68
            assert(class_exists($aopClass));
69
        }
70
71
        return $aopClass;
72
    }
73
74
    private function regsterLoader()
75
    {
76
        if (\in_array($this->classDir, static::$classDirs, true)) {
0 ignored issues
show
Comprehensibility introduced by
Since Ray\Aop\Weaver is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
77
            return;
78
        }
79
        static::$classDirs[] = $this->classDir;
0 ignored issues
show
Comprehensibility introduced by
Since Ray\Aop\Weaver is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
80
        spl_autoload_register(
81
            function (string $class) {
82
                $file = sprintf('%s/%s.php', $this->classDir, str_replace('\\', '_', $class));
83
                if (file_exists($file)) {
84
                    include $file; //@codeCoverageIgnore
85
                }
86
            }
87
        );
88
    }
89
}
90