ObjectDefinition::setConstructorInjection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of the Stack package.
4
 *
5
 * (c) Andrzej Kostrzewa <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Stack\DI\Definition;
12
13
/**
14
 * Defines how an object can be instantiated.
15
 *
16
 * @author Andrzej Kostrzewa <[email protected]>
17
 */
18
class ObjectDefinition extends \stdClass
19
{
20
    /**
21
     * Entry name (most of the time, same as $className).
22
     *
23
     * @var string
24
     */
25
    private $name;
26
27
    /**
28
     * Class name (if null, then the class name is $name).
29
     *
30
     * @var string|null
31
     */
32
    private $className;
33
34
    /**
35
     * @var array
36
     */
37
    private $constructorInjection = [];
38
39
    /**
40
     * @var array
41
     */
42
    private $methodInjections = [];
43
44
    /**
45
     * ObjectDefinition constructor.
46
     *
47
     * @param string $name      Entry name
48
     * @param null   $className Class name
49
     */
50
    public function __construct($name, $className = null)
51
    {
52
        $this->name      = $name;
53
        $this->className = $className;
54
    }
55
56
    /**
57
     * @return string Class name
58
     */
59
    public function getClassName()
60
    {
61
        if ($this->className !== null) {
62
            return $this->className;
63
        }
64
65
        return $this->name;
66
    }
67
68
    /**
69
     * @return string Entry name
70
     */
71
    public function getName()
72
    {
73
        return $this->name;
74
    }
75
76
    /**
77
     * Add method parameters.
78
     *
79
     * @param $name
80
     * @param array $methodInjection
81
     */
82
    public function addMethodInjection($name, array $methodInjection)
83
    {
84
        if (!isset($this->methodInjections[$name])) {
85
            $this->methodInjections[$name] = [];
86
        }
87
88
        $this->methodInjections[$name][] = $methodInjection;
89
    }
90
91
    /**
92
     * Set property definition.
93
     *
94
     * @param \ReflectionProperty $property
95
     * @param $target
96
     */
97
    public function setPropertyInjection(\ReflectionProperty $property, $target)
98
    {
99
        if (!$property->isPublic()) {
100
            $property->setAccessible(true);
101
        }
102
        $property->setValue($this, $target);
103
    }
104
105
    /**
106
     * Set constructor parameters.
107
     *
108
     * @param array $methodInjection
109
     */
110
    public function setConstructorInjection(array $methodInjection)
111
    {
112
        $this->constructorInjection = $methodInjection;
113
    }
114
115
    /**
116
     * Get method parameters.
117
     *
118
     * @param $name
119
     *
120
     * @return array
121
     */
122
    public function getMethodParameters($name)
123
    {
124
        if (isset($this->methodInjections[$name])) {
125
            return $this->methodInjections[$name][0];
126
        }
127
128
        return [];
129
    }
130
131
    /**
132
     * New Instance of defined class.
133
     *
134
     * @param bool $withConstructor
135
     *
136
     * @return object|\ReflectionClass
137
     */
138
    public function getNewInstance($withConstructor = true)
139
    {
140
        $object = new \ReflectionClass($this->name);
141
142
        return $withConstructor ?
143
            $object->newInstanceArgs($this->constructorInjection)
144
            : $object->newInstanceWithoutConstructor();
145
    }
146
}
147