1 | <?php |
||||
2 | /* |
||||
3 | * @copyright (c) 2018 Mendel <[email protected]> |
||||
4 | * @license see license.txt |
||||
5 | */ |
||||
6 | namespace drycart\di; |
||||
7 | |||||
8 | /** |
||||
9 | * Full container functionality DI |
||||
10 | * |
||||
11 | * @author Mendel <[email protected]> |
||||
12 | */ |
||||
13 | class Container extends AbstractContainer |
||||
14 | { |
||||
15 | /** |
||||
16 | * Set config |
||||
17 | * @param array $config |
||||
18 | */ |
||||
19 | public function setConfig(array $config) : void |
||||
20 | { |
||||
21 | $this->config = $config; |
||||
22 | } |
||||
23 | |||||
24 | /** |
||||
25 | * Add config for one class |
||||
26 | * @param string $id |
||||
27 | * @param array $config |
||||
28 | * @return void |
||||
29 | */ |
||||
30 | public function addClass(string $id, array $config) : void |
||||
31 | { |
||||
32 | $this->config[$id] = $config; |
||||
33 | } |
||||
34 | |||||
35 | /** |
||||
36 | * Add transformer closure for transform parameters |
||||
37 | * @param callable $transformer |
||||
38 | * @return void |
||||
39 | */ |
||||
40 | public function addTransformer(callable $transformer) : void |
||||
41 | { |
||||
42 | $this->transformers[] = $transformer; |
||||
43 | } |
||||
44 | |||||
45 | /** |
||||
46 | * Call object method using parameter and add some parameters if need |
||||
47 | * @param callable $callable |
||||
48 | * @param array $parameters |
||||
49 | * @return mixed |
||||
50 | */ |
||||
51 | public function call(callable $callable, array $parameters) |
||||
52 | { |
||||
53 | $preparedParameters = $this->callableParameters($callable, $parameters); |
||||
54 | return call_user_func_array($callable, $preparedParameters); |
||||
55 | } |
||||
56 | |||||
57 | /** |
||||
58 | * Finds an entry of the container by its identifier and returns it. |
||||
59 | * |
||||
60 | * @param string $id class name |
||||
61 | * |
||||
62 | * @throws NotFoundException No entry was found for **this** identifier. |
||||
63 | * @throws ContainerException Error while retrieving the entry. |
||||
64 | * |
||||
65 | * @return mixed |
||||
66 | */ |
||||
67 | public function get($id) |
||||
68 | { |
||||
69 | $config = $this->classConfig($id, true); |
||||
70 | // |
||||
71 | if ($config['#singleton']) { |
||||
72 | return $this->internalSingleton($id, $config); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
73 | } else { |
||||
74 | return $this->internalMake($id, $config); |
||||
0 ignored issues
–
show
It seems like
$config can also be of type null ; however, parameter $config of drycart\di\AbstractContainer::internalMake() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
75 | } |
||||
76 | } |
||||
77 | |||||
78 | /** |
||||
79 | * Create new object, throw if singleton |
||||
80 | * @param string $id class name |
||||
81 | * @param array $parameters parameters from request |
||||
82 | * @return mixed |
||||
83 | * @throws ContainerException |
||||
84 | */ |
||||
85 | public function make(string $id, array $parameters = []) |
||||
86 | { |
||||
87 | $config = $this->classConfig($id, true); |
||||
88 | // |
||||
89 | if ($config['#singleton']) { |
||||
90 | throw new ContainerException($id.' is singlton!'); |
||||
91 | } |
||||
92 | // |
||||
93 | return $this->internalMake($id, $config, $parameters); |
||||
0 ignored issues
–
show
It seems like
$config can also be of type null ; however, parameter $config of drycart\di\AbstractContainer::internalMake() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
94 | } |
||||
95 | |||||
96 | /** |
||||
97 | * Get singleton, throw if not singleton |
||||
98 | * @param string $id class name |
||||
99 | * @return mixed |
||||
100 | * @throws ContainerException |
||||
101 | */ |
||||
102 | public function singleton(string $id) |
||||
103 | { |
||||
104 | $config = $this->classConfig($id, true); |
||||
105 | // |
||||
106 | if (!$config['#singleton']) { |
||||
107 | throw new ContainerException($id.' NOT singlton!'); |
||||
108 | } |
||||
109 | // |
||||
110 | return $this->internalSingleton($id, $config); |
||||
0 ignored issues
–
show
It seems like
$config can also be of type null ; however, parameter $config of drycart\di\AbstractContainer::internalSingleton() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
111 | } |
||||
112 | |||||
113 | /** |
||||
114 | * Returns true if the container can return an entry for the given identifier. |
||||
115 | * Returns false otherwise. |
||||
116 | * *Implement* container interface method |
||||
117 | * |
||||
118 | * @param string $id Identifier of the entry to look for. |
||||
119 | * |
||||
120 | * @return bool |
||||
121 | */ |
||||
122 | public function has($id): bool |
||||
123 | { |
||||
124 | return !empty($this->classConfig($id)); |
||||
125 | } |
||||
126 | |||||
127 | } |
||||
128 |