Completed
Push — 2.x ( 4c102d...e762e8 )
by Akihito
02:20
created

Weaver::newInstance()   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 Compiler
31
     */
32
    private $compiler;
33
34
    public function __construct(BindInterface $bind, string $classDir)
35
    {
36
        $this->bind = $bind;
37
        $this->bindName = $bind->toString('');
38
        $this->compiler = new Compiler($classDir);
39
        $this->classDir = $classDir;
40
        $this->aopClassName = new AopClassName;
41
    }
42
43
    /**
44
     * @param class-string $class
0 ignored issues
show
Documentation introduced by
The doc-type class-string 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...
45
     * @param mixed[]      $args
46
     */
47
    public function newInstance(string $class, array $args) : object
48
    {
49
        $aopClass = $this->weave($class);
50
        $instance = (new ReflectionClass($aopClass))->newInstanceArgs($args);
51
        assert(isset($instance->bindings));
52
        $instance->bindings = $this->bind->getBindings();
53
54
        return $instance;
55
    }
56
57
    /**
58
     * @param class-string $class
0 ignored issues
show
Documentation introduced by
The doc-type class-string 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...
59
     *
60
     * @return class-string
0 ignored issues
show
Documentation introduced by
The doc-type class-string 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...
61
     */
62
    public function weave(string $class) : string
63
    {
64
        $aopClass = ($this->aopClassName)($class, $this->bindName);
65
        if (class_exists($aopClass, false)) {
66
            return $aopClass;
67
        }
68
        if ($this->loadClass($aopClass)) {
69
            assert(class_exists($aopClass));
70
71
            return $aopClass;
72
        }
73
        $this->compiler->compile($class, $this->bind);
74
        assert(class_exists($aopClass));
75
76
        return $aopClass;
77
    }
78
79
    private function loadClass(string $class) : bool
80
    {
81
        $file = sprintf('%s/%s.php', $this->classDir, str_replace('\\', '_', $class));
82
        if (file_exists($file)) {
83
            require $file;
84
85
            return true;
86
        }
87
88
        return false;
89
    }
90
}
91