1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\panie; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Holds all the bindings for the dependency injection. |
7
|
|
|
*/ |
8
|
|
|
class Bindings |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* An array of all the bindings. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $bindings = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Key of the binding that would be altered with subsequent calls to a bindings object. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $activeKey; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set the key of the binding to be modified or accessed on subsequent calls. |
27
|
|
|
* |
28
|
|
|
* @param $activeKey |
29
|
|
|
* @return self |
30
|
|
|
*/ |
31
|
10 |
|
public function setActiveKey(string $activeKey) : self |
32
|
|
|
{ |
33
|
10 |
|
$this->activeKey = $activeKey; |
34
|
10 |
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Registers a method to be called when an instance is created for the current active binding. |
39
|
|
|
* This method could be a setter or some other optional initialization method. Parameters for methods registered |
40
|
|
|
* with call that have type hints are also injected. |
41
|
|
|
* |
42
|
|
|
* @param string $method |
43
|
|
|
* @param array $parameters |
44
|
|
|
* @return self |
45
|
|
|
*/ |
46
|
3 |
|
public function call(string $method, array $parameters = []) : self |
47
|
|
|
{ |
48
|
3 |
|
$this->bindings[$this->activeKey]['calls'][] = [$method, $parameters]; |
49
|
3 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Provides a concrete class or factory function to which the currently selected binding should be linked. |
54
|
|
|
* |
55
|
|
|
* @param mixed $value |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
12 |
|
public function to($value) |
59
|
|
|
{ |
60
|
12 |
|
if(isset($this->bindings[$this->activeKey])) { |
61
|
1 |
|
$this->bindings[$this->activeKey]['binding'] = $value; |
62
|
|
|
} else { |
63
|
12 |
|
$this->bindings[$this->activeKey] = ['binding' => $value, 'calls' => [], 'properties' => []]; |
64
|
|
|
} |
65
|
12 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function withArgs(array $args) |
69
|
|
|
{ |
70
|
|
|
$this->bindings[$this->activeKey]['args'] = $args; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the configuration of a binding. |
75
|
|
|
* |
76
|
|
|
* @param string $key |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
12 |
|
public function get(string $key) |
80
|
|
|
{ |
81
|
12 |
|
$binding = $this->bindings[$key]; |
82
|
12 |
|
if(!isset($binding['binding'])) { |
83
|
|
|
$binding['binding'] = $key; |
84
|
|
|
} |
85
|
12 |
|
return $binding; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Checks if a binding exists. |
90
|
|
|
* |
91
|
|
|
* @param string $key |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
19 |
|
public function has($key) |
95
|
|
|
{ |
96
|
19 |
|
return isset($this->bindings[$key]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Register the current active binding as a singleton. |
101
|
|
|
* |
102
|
|
|
* @param bool $singleton |
103
|
|
|
*/ |
104
|
3 |
|
public function asSingleton($singleton = true) |
105
|
|
|
{ |
106
|
3 |
|
$this->bindings[$this->activeKey]['singleton'] = $singleton; |
107
|
3 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Setup bindings from an array. |
111
|
|
|
* |
112
|
|
|
* @param array $binding |
113
|
|
|
*/ |
114
|
2 |
|
private function setArrayBinding(array $binding) |
115
|
|
|
{ |
116
|
2 |
|
if(isset($binding[0])) { |
117
|
2 |
|
$this->to($binding[0]); |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
if(isset($binding['calls'])){ |
121
|
1 |
|
foreach($binding['calls'] as $key => $call) { |
122
|
1 |
|
if(is_string($call)) { |
123
|
1 |
|
$method = $call; |
124
|
1 |
|
$parameters = []; |
125
|
1 |
|
} else if(is_array($call)) { |
126
|
1 |
|
$method = $key; |
127
|
1 |
|
$parameters = $call; |
128
|
|
|
} |
129
|
1 |
|
$this->call($method, $parameters); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
} else { |
132
|
1 |
|
$this->bindings[$this->activeKey]['calls'] = []; |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
$this->asSingleton($binding['singleton'] ?? false); |
136
|
2 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Merge current bindings with another configuration overwriting conflicting values with the new bindings. |
140
|
|
|
* |
141
|
|
|
* @param array $bindings |
142
|
|
|
*/ |
143
|
5 |
|
public function merge(array $bindings) |
144
|
|
|
{ |
145
|
5 |
|
foreach($bindings as $key => $binding) { |
146
|
5 |
|
$this->activeKey = $key; |
147
|
5 |
|
if(is_array($binding)) { |
148
|
2 |
|
$this->setArrayBinding($binding); |
149
|
|
|
} else { |
150
|
5 |
|
$this->bindings[$key] = ['binding' => $binding, 'calls' => [], 'properties' => []]; |
151
|
|
|
} |
152
|
|
|
} |
153
|
5 |
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: