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
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $bindings = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* An active key that would be altered with subsequent calls to the bindings object. |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $activeKey; |
22
|
|
|
|
23
|
5 |
|
public function setActiveKey($activeKey) |
24
|
|
|
{ |
25
|
5 |
|
$this->activeKey = $activeKey; |
26
|
5 |
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function call($method, $parameters = []) |
30
|
|
|
{ |
31
|
|
|
$this->bindings[$this->activeKey]['calls'][] = ['setter' => $method, 'parameters' => $parameters]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function setProperty($property, $binding) |
35
|
|
|
{ |
36
|
|
|
$this->bindings[$this->activeKey]['sets'][] = ['property' => $property, 'binding' => $binding]; |
37
|
|
|
} |
38
|
|
|
|
39
|
5 |
|
public function to($value) |
40
|
|
|
{ |
41
|
5 |
|
$this->bindings[$this->activeKey] = ['binding' => $value, 'calls'=>[], 'properties' => []]; |
42
|
5 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
5 |
|
public function get($key) |
46
|
|
|
{ |
47
|
5 |
|
return $this->bindings[$key]; |
48
|
|
|
} |
49
|
|
|
|
50
|
7 |
|
public function has($key) |
51
|
|
|
{ |
52
|
7 |
|
return isset($this->bindings[$key]); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function asSingleton() |
56
|
|
|
{ |
57
|
1 |
|
$this->bindings[$this->activeKey]['singleton'] = true; |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
public function merge($bindings, $replace = true) |
61
|
|
|
{ |
62
|
|
|
foreach($bindings as $key => $binding) { |
63
|
|
|
if(isset($this->bindings[$key]) && !$replace) { |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
if(is_array($binding)) { |
67
|
|
|
$this->bindings[$key] = ['binding' => $binding[0]]; |
68
|
|
|
if(isset($binding['singleton'])) { |
69
|
|
|
$this->bindings[$key]['singleton'] = $binding['singleton']; |
70
|
|
|
} |
71
|
|
|
} else { |
72
|
|
|
$this->bindings[$key] = ['binding' => $binding]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|