1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 26.01.16 at 15:11 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\di; |
7
|
|
|
use samsonframework\container\ContainerInterface; |
8
|
|
|
use samsonframework\di\exception\ClassNotFoundException; |
9
|
|
|
use samsonframework\di\exception\ContainerException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Dependency container. |
13
|
|
|
* |
14
|
|
|
* @author Vitaly Iegorov <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Container implements ContainerInterface |
17
|
|
|
{ |
18
|
|
|
/** @var array Collection of instantiated service instances */ |
19
|
|
|
protected $serviceInstances = []; |
20
|
|
|
/** @var array[string] Collection of loaded services */ |
21
|
|
|
protected $services = []; |
22
|
|
|
/** @var array[string] Collection of alias => class name for alias resolving */ |
23
|
|
|
protected $aliases = []; |
24
|
|
|
/** @var array[string] Collection of class name dependencies trees */ |
25
|
|
|
protected $dependencies = []; |
26
|
|
|
/** @var ContainerInterface[] Collection of delegated containers */ |
27
|
|
|
protected $delegates = []; |
28
|
|
|
/** @var callable Dependency resolving function callable */ |
29
|
|
|
protected $logicCallable; |
30
|
|
|
/** @var array Collection of scope => [alias => class_name] */ |
31
|
|
|
protected $scopes = []; |
32
|
|
|
/** |
33
|
|
|
* Wrapper for calling dependency resolving function. |
34
|
|
|
* |
35
|
|
|
* @param string $dependency Dependency name |
36
|
|
|
* |
37
|
|
|
* @return mixed Created instance or null |
38
|
|
|
* @throws ContainerException |
39
|
|
|
*/ |
40
|
5 |
|
protected function logic($dependency) |
41
|
|
|
{ |
42
|
5 |
|
if (!is_callable($this->logicCallable)) { |
43
|
2 |
|
throw new ContainerException('Logic function is not callable'); |
44
|
|
|
} |
45
|
4 |
|
return call_user_func($this->logicCallable, $dependency); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get parameter |
50
|
|
|
* |
51
|
|
|
* @param $name |
52
|
|
|
*/ |
53
|
|
|
public function getParameter($name) |
54
|
|
|
{ |
55
|
|
|
return $this->parameter($name); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
* |
61
|
|
|
* @throws \samsonframework\di\exception\ContainerException |
62
|
|
|
* @throws \samsonframework\di\exception\ClassNotFoundException |
63
|
|
|
*/ |
64
|
5 |
|
public function get($dependency) |
65
|
|
|
{ |
66
|
|
|
// Get pointer from logic |
67
|
5 |
|
$module = $this->logic($dependency) ?? $this->getFromDelegate($dependency); |
68
|
4 |
|
if (null === $module) { |
69
|
1 |
|
throw new ClassNotFoundException($dependency); |
70
|
|
|
} else { |
71
|
3 |
|
return $module; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
/** |
75
|
|
|
* Try to find dependency in delegate container. |
76
|
|
|
* |
77
|
|
|
* @param string $dependency Dependency identifier |
78
|
|
|
* |
79
|
|
|
* @return mixed Delegate found dependency |
80
|
|
|
* |
81
|
|
|
* @throws \Interop\Container\Exception\ContainerException |
82
|
|
|
*/ |
83
|
2 |
|
protected function getFromDelegate(string $dependency) |
84
|
|
|
{ |
85
|
|
|
// Try delegate lookup |
86
|
2 |
|
foreach ($this->delegates as $delegate) { |
87
|
|
|
try { |
88
|
2 |
|
return $delegate->get($dependency); |
89
|
1 |
|
} catch (ContainerException $e) { |
90
|
|
|
// Catch all delegated exceptions |
91
|
1 |
|
} catch (ClassNotFoundException $e) { |
92
|
|
|
// Catch all delegated exceptions |
93
|
|
|
} |
94
|
|
|
} |
95
|
1 |
|
return null; |
96
|
|
|
} |
97
|
|
|
/** |
98
|
|
|
* Implementing delegate lookup feature. |
99
|
|
|
* If current container cannot resolve entity dependency |
100
|
|
|
* resolving process is passed to delegated container. |
101
|
|
|
* |
102
|
|
|
* @param ContainerInterface $container Container for delegate lookup |
103
|
|
|
*/ |
104
|
3 |
|
public function delegate(ContainerInterface $container) |
105
|
|
|
{ |
106
|
3 |
|
$this->delegates[] = $container; |
107
|
3 |
|
} |
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
2 |
|
public function has($dependency) : bool |
112
|
|
|
{ |
113
|
2 |
|
$found = array_key_exists($dependency, $this->dependencies) |
114
|
2 |
|
|| in_array($dependency, $this->aliases, true); |
115
|
|
|
// Return true if found or try delegate containers |
116
|
2 |
|
return $found ?: $this->hasDelegate($dependency); |
117
|
|
|
} |
118
|
|
|
/** |
119
|
|
|
* Define if delegate containers have dependency. |
120
|
|
|
* |
121
|
|
|
* @param string $dependency Dependency identifier |
122
|
|
|
* |
123
|
|
|
* @return bool True if delegate containers have dependency |
124
|
|
|
*/ |
125
|
2 |
|
protected function hasDelegate(string $dependency) : bool |
126
|
|
|
{ |
127
|
2 |
|
foreach ($this->delegates as $delegate) { |
128
|
1 |
|
if ($delegate->has($dependency)) { |
129
|
1 |
|
return true; |
130
|
|
|
} |
131
|
|
|
} |
132
|
2 |
|
return false; |
133
|
|
|
} |
134
|
|
|
/** |
135
|
|
|
* Set service dependency. Upon first creation of this class instance |
136
|
|
|
* it would be used everywhere where this dependency is needed. |
137
|
|
|
* |
138
|
|
|
* @param string $className Fully qualified class name |
139
|
|
|
* @param array $parameters Collection of parameters needed for dependency creation |
140
|
|
|
* @param string $alias Dependency name |
141
|
|
|
* |
142
|
|
|
* @return ContainerInterface Chaining |
143
|
|
|
*/ |
144
|
9 |
|
public function service($className, array $parameters = [], string $alias = null) : ContainerInterface |
145
|
|
|
{ |
146
|
9 |
|
$this->services[$className] = $className; |
147
|
9 |
|
return $this->set($className, $parameters, $alias); |
148
|
|
|
} |
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
9 |
|
public function set($className, array $dependencies = [], string $alias = null) : ContainerInterface |
153
|
|
|
{ |
154
|
|
|
// Create dependencies collection for class name |
155
|
9 |
|
if (!array_key_exists($className, $this->dependencies)) { |
156
|
9 |
|
$this->dependencies[$className] = []; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// Merge other class constructor parameters |
160
|
9 |
|
$this->dependencies[$className] = array_merge($this->dependencies[$className], $dependencies); |
161
|
|
|
// Store alias for this class name |
162
|
9 |
|
$this->aliases[$className] = $alias; |
163
|
9 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
1 |
|
public function getServices(string $filterScope = null) : array |
169
|
|
|
{ |
170
|
1 |
|
$filtered = []; |
171
|
1 |
|
if ($filterScope !== null && array_key_exists($filterScope, $this->scopes)) { |
172
|
|
|
foreach ($this->scopes[$filterScope] as $alias => $className) { |
173
|
|
|
$filtered[] = $this->get($className); |
174
|
|
|
} |
175
|
|
|
} |
176
|
1 |
|
return $filtered; |
177
|
|
|
} |
178
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.