Completed
Push — 2.x ( 144798...0daeb2 )
by Akihito
10s
created

Dependency::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the Ray.Di package.
6
 *
7
 * @license http://opensource.org/licenses/MIT MIT
8
 */
9
namespace Ray\Di;
10
11
use Ray\Aop\Bind as AopBind;
12
use Ray\Aop\CompilerInterface;
13
use Ray\Aop\MethodInterceptor;
14
15
final class Dependency implements DependencyInterface
16
{
17
    /**
18
     * @var NewInstance
19
     */
20
    private $newInstance;
21
22
    /**
23
     * @var string
24
     */
25
    private $postConstruct;
26
27
    /**
28
     * @var bool
29
     */
30
    private $isSingleton = false;
31
32
    /**
33
     * @var mixed
34
     */
35
    private $instance;
36
37
    /**
38
     * @var string
39
     */
40
    private $index;
41
42
    /**
43
     * @param NewInstance       $newInstance
44
     * @param \ReflectionMethod $postConstruct
45
     */
46 71
    public function __construct(NewInstance $newInstance, \ReflectionMethod $postConstruct = null)
47
    {
48 71
        $this->newInstance = $newInstance;
49 71
        $this->postConstruct = $postConstruct ? $postConstruct->name : null;
50 71
    }
51
52 4
    public function __sleep()
53
    {
54 4
        return ['newInstance', 'postConstruct', 'isSingleton'];
55
    }
56
57 1
    public function __toString()
58
    {
59 1
        return sprintf(
60 1
            '(dependency) %s',
61 1
            (string) $this->newInstance
62
        );
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 63
    public function register(array &$container, Bind $bind)
69
    {
70 63
        $this->index = $index = (string) $bind;
71 63
        $container[$index] = $bind->getBound();
72 63
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 58
    public function inject(Container $container)
78
    {
79
        // singleton ?
80 58
        if ($this->isSingleton === true && $this->instance) {
81 16
            return $this->instance;
82
        }
83
84
        // create dependency injected instance
85 58
        $this->instance = $this->newInstance->__invoke($container);
86
87
        // @PostConstruct
88 57
        if ($this->postConstruct) {
89 8
            $this->instance->{$this->postConstruct}();
90
        }
91
92 57
        return $this->instance;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 51
    public function setScope($scope)
99
    {
100 51
        if ($scope === Scope::SINGLETON) {
101 48
            $this->isSingleton = true;
102
        }
103 51
    }
104
105 43
    public function weaveAspects(CompilerInterface $compiler, array $pointcuts)
106
    {
107 43
        $class = (string) $this->newInstance;
108 43
        $isInterceptor = (new \ReflectionClass($class))->implementsInterface(MethodInterceptor::class);
109 43
        if ($isInterceptor) {
110 42
            return;
111
        }
112 43
        $bind = new AopBind;
113 43
        $bind->bind((string) $this->newInstance, $pointcuts);
114 43
        if (! $bind->getBindings()) {
115 36
            return;
116
        }
117 12
        $class = $compiler->compile((string) $this->newInstance, $bind);
118 12
        $this->newInstance->weaveAspects($class, $bind);
119 12
    }
120
}
121