These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the slince/di package. |
||
5 | * |
||
6 | * (c) Slince <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Slince\Di; |
||
13 | |||
14 | use Slince\Di\Exception\ConfigException; |
||
15 | use Slince\Di\Exception\DependencyInjectionException; |
||
16 | use Slince\Di\Exception\NotFoundException; |
||
17 | use Interop\Container\ContainerInterface; |
||
18 | |||
19 | class Container implements ContainerInterface |
||
20 | { |
||
21 | /** |
||
22 | * Array of singletons |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $services = []; |
||
26 | |||
27 | /** |
||
28 | * Array of Definitions |
||
29 | * |
||
30 | * @var Definition[] |
||
31 | */ |
||
32 | protected $definitions = []; |
||
33 | |||
34 | /** |
||
35 | * Array of parameters |
||
36 | * @var ParameterBag |
||
37 | */ |
||
38 | protected $parameters; |
||
39 | |||
40 | /** |
||
41 | * @var DefinitionResolver |
||
42 | */ |
||
43 | protected $definitionResolver; |
||
44 | |||
45 | /** |
||
46 | * Defaults for the container. |
||
47 | * |
||
48 | * [ |
||
49 | * 'share' => true, |
||
50 | * 'autowire' => true |
||
51 | * ] |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $defaults = [ |
||
55 | 'share' => true, |
||
56 | 'autowire' => true |
||
57 | ]; |
||
58 | |||
59 | public function __construct() |
||
60 | { |
||
61 | $this->parameters = new ParameterBag(); |
||
62 | $this->definitionResolver = new DefinitionResolver($this); |
||
63 | $this->register($this); |
||
0 ignored issues
–
show
|
|||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Register a definition. |
||
68 | * |
||
69 | * @param string $id |
||
70 | * @param mixed $concrete |
||
71 | * @return Definition |
||
72 | */ |
||
73 | public function register($id, $concrete = null) |
||
74 | { |
||
75 | if (is_object($id)) { |
||
76 | $concrete = $id; |
||
77 | $id = get_class($id); |
||
78 | } |
||
79 | |||
80 | $definition = new Definition(); |
||
81 | |||
82 | if (null === $concrete) { |
||
83 | $definition->setClass($id); |
||
84 | } elseif (is_string($concrete)) { |
||
85 | $definition->setClass($concrete); |
||
86 | } elseif ($concrete instanceof \Closure || is_array($concrete)) { |
||
87 | $definition->setFactory($concrete); |
||
88 | } elseif (is_object($concrete)) { |
||
89 | $definition->setFactory(function() use ($concrete){ |
||
90 | return $concrete; |
||
91 | })->setClass(get_class($concrete)); |
||
92 | } else { |
||
93 | throw new ConfigException('expects a valid concrete'); |
||
94 | } |
||
95 | |||
96 | $definition = $this->setDefinition($id, $definition); |
||
97 | |||
98 | //Apply defaults. |
||
99 | $definition |
||
100 | ->setShared($this->defaults['share']) |
||
101 | ->setAutowired($this->defaults['autowire']); |
||
102 | return $definition; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Set a definition. |
||
107 | * |
||
108 | * @param string $id |
||
109 | * @param Definition $definition |
||
110 | * |
||
111 | * @return Definition |
||
112 | */ |
||
113 | public function setDefinition($id, Definition $definition) |
||
114 | { |
||
115 | $id = (string) $id; |
||
116 | return $this->definitions[$id] = $definition; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get a service instance by specified ID |
||
121 | * |
||
122 | * @param string $id |
||
123 | * @return object |
||
124 | */ |
||
125 | public function get($id) |
||
126 | { |
||
127 | //If service is singleton, return instance directly. |
||
128 | if (isset($this->services[$id])) { |
||
129 | return $this->services[$id]; |
||
130 | } |
||
131 | |||
132 | //If there is no matching definition, creates an definition automatically |
||
133 | if (!isset($this->definitions[$id])) { |
||
134 | if (class_exists($id)) { |
||
135 | $this->register($id); |
||
136 | } else { |
||
137 | throw new NotFoundException(sprintf('There is no definition named "%s"', $id)); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | // resolve instance. |
||
142 | $instance = $this->definitionResolver->resolve($this->definitions[$id]); |
||
143 | |||
144 | //If the service be set as singleton mode, stores its instance |
||
145 | if ($this->definitions[$id]->isShared()) { |
||
146 | $this->services[$id] = $instance; |
||
147 | } |
||
148 | return $instance; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function has($id) |
||
155 | { |
||
156 | if (isset($this->services[$id])) { |
||
157 | return true; |
||
158 | } |
||
159 | if (!isset($this->definitions[$id]) && class_exists($id)) { |
||
160 | $this->register($id); |
||
161 | } |
||
162 | return isset($this->definitions[$id]); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Gets all global parameters |
||
167 | * @return array |
||
168 | */ |
||
169 | public function getParameters() |
||
170 | { |
||
171 | return $this->parameters->toArray(); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Sets array of parameters |
||
176 | * @param array $parameterStore |
||
177 | */ |
||
178 | public function setParameters(array $parameterStore) |
||
179 | { |
||
180 | $this->parameters->setParameters($parameterStore); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Add some parameters |
||
185 | * @param array $parameters |
||
186 | */ |
||
187 | public function addParameters(array $parameters) |
||
188 | { |
||
189 | $this->parameters->addParameters($parameters); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Sets a parameter with its name and value |
||
194 | * |
||
195 | * @param $name |
||
196 | * @param mixed $value |
||
197 | */ |
||
198 | public function setParameter($name, $value) |
||
199 | { |
||
200 | $this->parameters->setParameter($name, $value); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Gets a parameter by given name |
||
205 | * @param $name |
||
206 | * @param mixed $default |
||
207 | * @return mixed |
||
208 | */ |
||
209 | public function getParameter($name, $default = null) |
||
210 | { |
||
211 | return $this->parameters->getParameter($name, $default); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Gets a default option of the container. |
||
216 | * |
||
217 | * @param string $option |
||
218 | * @return mixed|null|boolean |
||
219 | */ |
||
220 | public function getDefault($option) |
||
221 | { |
||
222 | return isset($this->defaults[$option]) ? $this->defaults[$option] : null; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Configure defaults. |
||
227 | * |
||
228 | * @param array $defaults |
||
229 | * @return array |
||
230 | */ |
||
231 | public function setDefaults(array $defaults) |
||
232 | { |
||
233 | return $this->defaults = array_merge($this->defaults, $defaults); |
||
234 | } |
||
235 | } |
||
236 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: