1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/container |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\Container; |
7
|
|
|
|
8
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
9
|
|
|
use Zend\ServiceManager\ConfigInterface; |
10
|
|
|
use Nnx\EntryNameResolver\EntryNameResolverInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Container |
14
|
|
|
* |
15
|
|
|
* @package Nnx\Container |
16
|
|
|
*/ |
17
|
|
|
class Container extends AbstractPluginManager implements ContainerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Имя секции в конфиги приложения отвечающей за настройки контейнера |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
const CONFIG_KEY = 'nnx_container'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Флаг определяющий нужно ли производить поиск по $id в связанных ServiceManager'ах |
28
|
|
|
* |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
protected $flagUsePeeringServiceManagers = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Флаг определяет, нужно ли задействовать абстрактные фабрики, при поиске по $id |
35
|
|
|
* |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
protected $flagCheckAbstractFactories = true; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Резолвер для получения имени создаваемой сущщности |
42
|
|
|
* |
43
|
|
|
* @var EntryNameResolverInterface |
44
|
|
|
*/ |
45
|
|
|
protected $entryNameResolver; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Container constructor. |
49
|
|
|
* |
50
|
|
|
* @param EntryNameResolverInterface $entryNameResolver |
51
|
|
|
* @param ConfigInterface|null $configuration |
52
|
|
|
*/ |
53
|
|
|
public function __construct(EntryNameResolverInterface $entryNameResolver, ConfigInterface $configuration = null) |
54
|
|
|
{ |
55
|
|
|
$this->setEntryNameResolver($entryNameResolver); |
56
|
|
|
parent::__construct($configuration); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
* |
63
|
|
|
* @param string $id |
64
|
|
|
* @param array|null $options |
65
|
|
|
* @param null $context |
66
|
|
|
* |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function get($id, array $options = [], $usePeeringServiceManagers = true, $context = null) |
70
|
|
|
{ |
71
|
|
|
$resolvedId = $this->getEntryNameByContext($id, $context); |
72
|
|
|
return parent::get($resolvedId, $options, $usePeeringServiceManagers); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
* |
78
|
|
|
* @param array|string $id |
|
|
|
|
79
|
|
|
* @param null $context |
80
|
|
|
* |
81
|
|
|
* @return bool|void |
82
|
|
|
* |
83
|
|
|
*/ |
84
|
|
|
public function has($name, $checkAbstractFactories = true, $usePeeringServiceManagers = true, $context = null) |
85
|
|
|
{ |
86
|
|
|
if (is_array($name)) { |
87
|
|
|
return parent::has($name, $checkAbstractFactories, $usePeeringServiceManagers); |
88
|
|
|
} |
89
|
|
|
$resolvedId = $this->getEntryNameByContext($name, $context); |
90
|
|
|
return parent::has($resolvedId, $checkAbstractFactories, $usePeeringServiceManagers); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Проверить есть ли служба исходя из контекста вызова |
95
|
|
|
* |
96
|
|
|
* @param $name |
97
|
|
|
* @param $context |
98
|
|
|
* |
99
|
|
|
* @return bool|void |
100
|
|
|
*/ |
101
|
|
|
public function hasByContext($name, $context) |
102
|
|
|
{ |
103
|
|
|
$resolvedId = $this->getEntryNameByContext($name, $context); |
104
|
|
|
$checkAbstractFactories = $this->getFlagCheckAbstractFactories(); |
105
|
|
|
$usePeeringServiceManagers = $this->getFlagUsePeeringServiceManagers(); |
106
|
|
|
|
107
|
|
|
return $this->has($resolvedId, $checkAbstractFactories, $usePeeringServiceManagers, $context); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Получить службу исходя из контекста вызова |
112
|
|
|
* |
113
|
|
|
* @param $name |
114
|
|
|
* @param array $options |
115
|
|
|
* @param $context |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
* |
119
|
|
|
* @throws \Zend\ServiceManager\Exception\RuntimeException |
120
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException |
121
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
122
|
|
|
*/ |
123
|
|
|
public function getByContext($name, array $options = [], $context) |
124
|
|
|
{ |
125
|
|
|
$usePeeringServiceManagers = $this->getFlagUsePeeringServiceManagers(); |
126
|
|
|
return $this->get($name, $options, $usePeeringServiceManagers, $context); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
* |
132
|
|
|
* @param string $id |
133
|
|
|
* @param null $context |
134
|
|
|
* |
135
|
|
|
* @return string|void |
136
|
|
|
*/ |
137
|
|
|
public function getEntryNameByContext($id, $context = null) |
138
|
|
|
{ |
139
|
|
|
return $this->getEntryNameResolver()->resolveEntryNameByContext($id, $context); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritdoc |
144
|
|
|
* |
145
|
|
|
* @param mixed $plugin |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function validatePlugin($plugin) |
150
|
|
|
{ |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Возвращает флаг определяющий нужно ли производить поиск по $id в связанных ServiceManager'ах |
156
|
|
|
* |
157
|
|
|
* @return boolean |
158
|
|
|
*/ |
159
|
|
|
public function getFlagUsePeeringServiceManagers() |
160
|
|
|
{ |
161
|
|
|
return $this->flagUsePeeringServiceManagers; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Устанавливает флаг определяющий нужно ли производить поиск по $id в связанных ServiceManager'ах |
166
|
|
|
* |
167
|
|
|
* @param boolean $flagUsePeeringServiceManagers |
168
|
|
|
* |
169
|
|
|
* @return $this |
170
|
|
|
*/ |
171
|
|
|
public function setFlagUsePeeringServiceManagers($flagUsePeeringServiceManagers) |
172
|
|
|
{ |
173
|
|
|
$this->flagUsePeeringServiceManagers = (bool)$flagUsePeeringServiceManagers; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Возвращает флаг определяяющий, нужно ли задействовать абстрактные фабрики, при поиске по $id |
180
|
|
|
* |
181
|
|
|
* @return boolean |
182
|
|
|
*/ |
183
|
|
|
public function getFlagCheckAbstractFactories() |
184
|
|
|
{ |
185
|
|
|
return $this->flagCheckAbstractFactories; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Устанавливает флаг определяяющий, нужно ли задействовать абстрактные фабрики, при поиске по $id |
190
|
|
|
* |
191
|
|
|
* @param boolean $flagCheckAbstractFactories |
192
|
|
|
* |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
|
|
public function setFlagCheckAbstractFactories($flagCheckAbstractFactories) |
196
|
|
|
{ |
197
|
|
|
$this->flagCheckAbstractFactories = (bool)$flagCheckAbstractFactories; |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Возвращает резолвер для получения имени создаваемой сущщности |
204
|
|
|
* |
205
|
|
|
* @return EntryNameResolverInterface |
206
|
|
|
*/ |
207
|
|
|
public function getEntryNameResolver() |
208
|
|
|
{ |
209
|
|
|
return $this->entryNameResolver; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Устанавливает резолвер для получения имени создаваемой сущщности |
214
|
|
|
* |
215
|
|
|
* @param EntryNameResolverInterface $entryNameResolver |
216
|
|
|
* |
217
|
|
|
* @return $this |
218
|
|
|
*/ |
219
|
|
|
public function setEntryNameResolver($entryNameResolver) |
220
|
|
|
{ |
221
|
|
|
$this->entryNameResolver = $entryNameResolver; |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.