Completed
Push — 2.x ( 5c2340...e1da9c )
by Akihito
02:40
created

src/Dependency.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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|null
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 70
    public function __construct(NewInstance $newInstance, \ReflectionMethod $postConstruct = null)
47
    {
48 70
        $this->newInstance = $newInstance;
49 70
        $this->postConstruct = $postConstruct ? $postConstruct->name : null;
50 70
    }
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 62
    public function register(array &$container, Bind $bind)
69
    {
70 62
        $this->index = $index = (string) $bind;
71 62
        $container[$index] = $bind->getBound();
72 62
    }
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)($container);
86
87
        // @PostConstruct
88 57
        if ($this->postConstruct) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->postConstruct of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
89 8
            $this->instance->{$this->postConstruct}();
90
        }
91
92 57
        return $this->instance;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 50
    public function setScope($scope)
99
    {
100 50
        if ($scope === Scope::SINGLETON) {
101 47
            $this->isSingleton = true;
102
        }
103 50
    }
104
105 42
    public function weaveAspects(CompilerInterface $compiler, array $pointcuts)
106
    {
107 42
        $class = (string) $this->newInstance;
108 42
        $isInterceptor = (new \ReflectionClass($class))->implementsInterface(MethodInterceptor::class);
109 42
        if ($isInterceptor) {
110 41
            return;
111
        }
112 42
        $bind = new AopBind;
113 42
        $bind->bind((string) $this->newInstance, $pointcuts);
114 42
        if (! $bind->getBindings()) {
115 35
            return;
116
        }
117 12
        $class = $compiler->compile((string) $this->newInstance, $bind);
118 12
        $this->newInstance->weaveAspects($class, $bind);
119 12
    }
120
}
121