1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Starlit App. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016 Starweb AB |
6
|
|
|
* @license BSD 3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Starlit\App\Container; |
10
|
|
|
|
11
|
|
|
use Psr\Container\ContainerInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Dependency injection container. |
15
|
|
|
* |
16
|
|
|
* @author Andreas Nilsson <http://github.com/jandreasn> |
17
|
|
|
*/ |
18
|
|
|
class Container implements ContainerInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $dicValues = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $dicObjects = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set a DIC value. |
32
|
|
|
* |
33
|
|
|
* Wrap objects provided in a closure for lazy loading. |
34
|
|
|
* |
35
|
|
|
* @param string $key |
36
|
|
|
* @param mixed $value |
37
|
|
|
* @return Container |
38
|
|
|
*/ |
39
|
20 |
|
public function set($key, $value) |
40
|
|
|
{ |
41
|
20 |
|
$this->dicValues[$key] = $value; |
42
|
20 |
|
unset($this->dicObjects[$key]); // In case an object instance was stored for sharing |
43
|
|
|
|
44
|
20 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Check if a DIC value/object exists. |
49
|
|
|
* |
50
|
|
|
* @param string $key |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
20 |
|
public function has($key) |
54
|
|
|
{ |
55
|
20 |
|
return array_key_exists($key, $this->dicValues); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $key |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
2 |
|
public function hasInstance($key) |
63
|
|
|
{ |
64
|
2 |
|
return isset($this->dicObjects[$key]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the shared instance of a DIC object, or a DIC value if it's not an object. |
69
|
|
|
* |
70
|
|
|
* @param string $key |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
20 |
|
public function get($key) |
74
|
|
|
{ |
75
|
20 |
|
if (!$this->has($key)) { |
76
|
1 |
|
throw new NotFoundException(sprintf('No application value with key "%s" is defined.', $key)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Get already instantiated object if it exist |
80
|
20 |
|
if (isset($this->dicObjects[$key])) { |
81
|
2 |
|
return $this->dicObjects[$key]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Check if it's an invokable (closure/anonymous function) |
85
|
20 |
|
if (is_object($this->dicValues[$key]) && method_exists($this->dicValues[$key], '__invoke')) { |
86
|
20 |
|
$this->dicObjects[$key] = $this->dicValues[$key]($this); |
87
|
|
|
|
88
|
20 |
|
return $this->dicObjects[$key]; |
89
|
|
|
} |
90
|
|
|
|
91
|
20 |
|
return $this->dicValues[$key]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get new instance of a DIC object. |
96
|
|
|
* |
97
|
|
|
* @param string $key |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
3 |
|
public function getNew($key) |
101
|
|
|
{ |
102
|
3 |
|
if (!array_key_exists($key, $this->dicValues)) { |
103
|
1 |
|
throw new NotFoundException(sprintf('No application value with key "%s" is defined.', $key)); |
104
|
2 |
|
} elseif (!is_object($this->dicValues[$key]) || !method_exists($this->dicValues[$key], '__invoke')) { |
105
|
1 |
|
throw new \InvalidArgumentException(sprintf('Application value "%s" is not invokable.', $key)); |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return $this->dicValues[$key]($this); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Destroy a DIC object instance. |
113
|
|
|
* |
114
|
|
|
* Will force a new object to be created on next call. |
115
|
|
|
* |
116
|
|
|
* @param string $key |
117
|
|
|
*/ |
118
|
1 |
|
public function destroyInstance($key) |
119
|
|
|
{ |
120
|
1 |
|
unset($this->dicObjects[$key]); |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Destroy all DIC object instances. |
125
|
|
|
* |
126
|
|
|
* Will force new objects to be created on next call. |
127
|
|
|
*/ |
128
|
1 |
|
public function destroyAllInstances() |
129
|
|
|
{ |
130
|
1 |
|
$this->dicObjects = []; |
131
|
|
|
|
132
|
|
|
// To make sure objects (like database connections) are destructed properly. PHP might not destruct objects |
133
|
|
|
// until the end of execution otherwise. |
134
|
1 |
|
gc_collect_cycles(); |
135
|
1 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Magic method to get or set DIC values. |
139
|
|
|
* |
140
|
|
|
* @param string $name |
141
|
|
|
* @param array $arguments |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
5 |
|
public function __call($name, $arguments) |
145
|
|
|
{ |
146
|
|
|
// getNew followed by an upper letter like getNewApple() |
147
|
5 |
|
if (preg_match('/^getNew([A-Z].*)/', $name, $matches)) { |
148
|
1 |
|
$key = lcfirst($matches[1]); |
149
|
|
|
|
150
|
1 |
|
return $this->getNew($key); |
151
|
4 |
|
} elseif (strpos($name, 'get') === 0) { |
152
|
2 |
|
$key = lcfirst(substr($name, 3)); |
153
|
|
|
|
154
|
2 |
|
return $this->get($key); |
155
|
3 |
|
} elseif (strpos($name, 'set') === 0) { |
156
|
2 |
|
$argumentCount = count($arguments); |
157
|
2 |
|
if ($argumentCount !== 1) { |
158
|
1 |
|
throw new \BadMethodCallException("Invalid argument count[{$argumentCount}] for application {$name}()"); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
$key = lcfirst(substr($name, 3)); |
162
|
|
|
|
163
|
1 |
|
return $this->set($key, $arguments[0]); |
164
|
|
|
} else { |
165
|
1 |
|
throw new \BadMethodCallException("No application method named {$name}()"); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|