1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace League\Container; |
6
|
|
|
|
7
|
|
|
use League\Container\Definition\{DefinitionAggregate, DefinitionInterface, DefinitionAggregateInterface}; |
8
|
|
|
use League\Container\Exception\{NotFoundException, ContainerException}; |
9
|
|
|
use League\Container\Inflector\{InflectorAggregate, InflectorInterface, InflectorAggregateInterface}; |
10
|
|
|
use League\Container\ServiceProvider\{ServiceProviderAggregate, |
11
|
|
|
ServiceProviderAggregateInterface, |
12
|
|
|
ServiceProviderInterface}; |
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
|
15
|
|
|
class Container implements DefinitionContainerInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var boolean |
19
|
|
|
*/ |
20
|
|
|
protected $defaultToShared = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var DefinitionAggregateInterface |
24
|
|
|
*/ |
25
|
|
|
protected $definitions; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ServiceProviderAggregateInterface |
29
|
|
|
*/ |
30
|
|
|
protected $providers; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var InflectorAggregateInterface |
34
|
|
|
*/ |
35
|
|
|
protected $inflectors; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ContainerInterface[] |
39
|
|
|
*/ |
40
|
|
|
protected $delegates = []; |
41
|
|
|
|
42
|
114 |
|
public function __construct( |
43
|
|
|
DefinitionAggregateInterface $definitions = null, |
44
|
|
|
ServiceProviderAggregateInterface $providers = null, |
45
|
|
|
InflectorAggregateInterface $inflectors = null |
46
|
|
|
) { |
47
|
114 |
|
$this->definitions = $definitions ?? new DefinitionAggregate(); |
48
|
114 |
|
$this->providers = $providers ?? new ServiceProviderAggregate(); |
49
|
114 |
|
$this->inflectors = $inflectors ?? new InflectorAggregate(); |
50
|
|
|
|
51
|
114 |
|
if ($this->definitions instanceof ContainerAwareInterface) { |
52
|
114 |
|
$this->definitions->setContainer($this); |
53
|
|
|
} |
54
|
|
|
|
55
|
114 |
|
if ($this->providers instanceof ContainerAwareInterface) { |
56
|
114 |
|
$this->providers->setContainer($this); |
57
|
|
|
} |
58
|
|
|
|
59
|
114 |
|
if ($this->inflectors instanceof ContainerAwareInterface) { |
60
|
114 |
|
$this->inflectors->setContainer($this); |
61
|
|
|
} |
62
|
114 |
|
} |
63
|
|
|
|
64
|
27 |
|
public function add(string $id, $concrete = null): DefinitionInterface |
65
|
|
|
{ |
66
|
27 |
|
$concrete = $concrete ?? $id; |
67
|
|
|
|
68
|
27 |
|
if (true === $this->defaultToShared) { |
69
|
3 |
|
return $this->addShared($id, $concrete); |
70
|
|
|
} |
71
|
|
|
|
72
|
24 |
|
return $this->definitions->add($id, $concrete); |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
public function addShared(string $id, $concrete = null): DefinitionInterface |
76
|
|
|
{ |
77
|
6 |
|
$concrete = $concrete ?? $id; |
78
|
6 |
|
return $this->definitions->addShared($id, $concrete); |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
public function defaultToShared(bool $shared = true): ContainerInterface |
82
|
|
|
{ |
83
|
3 |
|
$this->defaultToShared = $shared; |
84
|
3 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
9 |
|
public function extend(string $id): DefinitionInterface |
88
|
|
|
{ |
89
|
9 |
|
if ($this->providers->provides($id)) { |
90
|
3 |
|
$this->providers->register($id); |
91
|
|
|
} |
92
|
|
|
|
93
|
9 |
|
if ($this->definitions->has($id)) { |
94
|
6 |
|
return $this->definitions->getDefinition($id); |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
throw new NotFoundException(sprintf( |
98
|
3 |
|
'Unable to extend alias (%s) as it is not being managed as a definition', |
99
|
1 |
|
$id |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
9 |
|
public function addServiceProvider(ServiceProviderInterface $provider): DefinitionContainerInterface |
104
|
|
|
{ |
105
|
9 |
|
$this->providers->add($provider); |
106
|
9 |
|
return $this; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
33 |
|
public function get($id) |
110
|
|
|
{ |
111
|
33 |
|
return $this->resolve($id); |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
public function getNew($id) |
115
|
|
|
{ |
116
|
3 |
|
return $this->resolve($id, true); |
117
|
|
|
} |
118
|
|
|
|
119
|
36 |
|
public function has($id): bool |
120
|
|
|
{ |
121
|
36 |
|
if ($this->definitions->has($id)) { |
122
|
21 |
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
21 |
|
if ($this->definitions->hasTag($id)) { |
126
|
6 |
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
15 |
|
if ($this->providers->provides($id)) { |
130
|
6 |
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
9 |
|
foreach ($this->delegates as $delegate) { |
134
|
3 |
|
if ($delegate->has($id)) { |
135
|
3 |
|
return true; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
6 |
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
public function inflector(string $type, callable $callback = null): InflectorInterface |
143
|
|
|
{ |
144
|
3 |
|
return $this->inflectors->add($type, $callback); |
145
|
|
|
} |
146
|
|
|
|
147
|
3 |
|
public function delegate(ContainerInterface $container): self |
148
|
|
|
{ |
149
|
3 |
|
$this->delegates[] = $container; |
150
|
|
|
|
151
|
3 |
|
if ($container instanceof ContainerAwareInterface) { |
152
|
3 |
|
$container->setContainer($this); |
153
|
|
|
} |
154
|
|
|
|
155
|
3 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
33 |
|
protected function resolve($id, bool $new = false) |
159
|
|
|
{ |
160
|
33 |
|
if ($this->definitions->has($id)) { |
161
|
18 |
|
$resolved = (true === $new) ? $this->definitions->resolveNew($id) : $this->definitions->resolve($id); |
162
|
18 |
|
return $this->inflectors->inflect($resolved); |
163
|
|
|
} |
164
|
|
|
|
165
|
18 |
|
if ($this->definitions->hasTag($id)) { |
166
|
6 |
|
$arrayOf = (true === $new) |
167
|
3 |
|
? $this->definitions->resolveTaggedNew($id) |
168
|
6 |
|
: $this->definitions->resolveTagged($id); |
169
|
|
|
|
170
|
|
|
array_walk($arrayOf, function (&$resolved) { |
171
|
6 |
|
$resolved = $this->inflectors->inflect($resolved); |
172
|
6 |
|
}); |
173
|
|
|
|
174
|
6 |
|
return $arrayOf; |
175
|
|
|
} |
176
|
|
|
|
177
|
12 |
|
if ($this->providers->provides($id)) { |
178
|
6 |
|
$this->providers->register($id); |
179
|
|
|
|
180
|
6 |
|
if (!$this->definitions->has($id) && !$this->definitions->hasTag($id)) { |
181
|
3 |
|
throw new ContainerException(sprintf('Service provider lied about providing (%s) service', $id)); |
182
|
|
|
} |
183
|
|
|
|
184
|
3 |
|
return $this->resolve($id, $new); |
185
|
|
|
} |
186
|
|
|
|
187
|
6 |
|
foreach ($this->delegates as $delegate) { |
188
|
3 |
|
if ($delegate->has($id)) { |
189
|
3 |
|
$resolved = $delegate->get($id); |
190
|
3 |
|
return $this->inflectors->inflect($resolved); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
3 |
|
throw new NotFoundException(sprintf('Alias (%s) is not being managed by the container or delegates', $id)); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.