Completed
Pull Request — 2.x (#117)
by Akihito
01:50 queued 30s
created

Weaver::newInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
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->aopClassName)($class, $this->bindName);
57
        if (! class_exists($aopClass)) {
58
            $this->compiler->compile($class, $this->bind);
59
            assert(class_exists($aopClass));
60
        }
61
        $instance = (new ReflectionClass($aopClass))->newInstanceArgs($args);
62
        $instance->bindings = $this->bind->getBindings();
63
64
        return $instance;
65
    }
66
67
    private function regsterLoader()
68
    {
69
        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...
70
            return;
71
        }
72
        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...
73
        spl_autoload_register(
74
            function (string $class) {
75
                $file = sprintf('%s/%s.php', $this->classDir, str_replace('\\', '_', $class));
76
                if (file_exists($file)) {
77
                    include $file; //@codeCoverageIgnore
78
                }
79
            }
80
        );
81
    }
82
}
83