This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Weew\Container; |
||
4 | |||
5 | use Weew\Container\Definitions\AliasDefinition; |
||
6 | use Weew\Container\Definitions\ClassDefinition; |
||
7 | use Weew\Container\Definitions\InterfaceDefinition; |
||
8 | use Weew\Container\Definitions\ValueDefinition; |
||
9 | use Weew\Container\Definitions\WildcardDefinition; |
||
10 | use Weew\Container\Exceptions\MissingDefinitionIdentifierException; |
||
11 | use Weew\Container\Exceptions\MissingDefinitionValueException; |
||
12 | |||
13 | class Registry { |
||
14 | /** |
||
15 | * @var IDefinition[] |
||
16 | */ |
||
17 | protected $definitions = []; |
||
18 | |||
19 | /** |
||
20 | * @param $id |
||
21 | * @param null $value |
||
22 | * |
||
23 | * @return IDefinition |
||
24 | */ |
||
25 | public function createDefinition($id, $value = null) { |
||
26 | list($id, $value) = $this->getIdAndValueFromCreateDefinitionArgs( |
||
27 | func_get_args(), $id, $value |
||
28 | ); |
||
29 | |||
30 | $definition = $this->delegateDefinitionCreation($id, $value); |
||
31 | $this->addDefinition($definition); |
||
0 ignored issues
–
show
|
|||
32 | |||
33 | return $definition; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param array $args |
||
38 | * @param $id |
||
39 | * @param $value |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | protected function getIdAndValueFromCreateDefinitionArgs(array $args, $id, $value) { |
||
44 | if (count($args) > 2) { |
||
45 | array_shift($args); |
||
46 | $value = $args; |
||
47 | } |
||
48 | |||
49 | if ( ! is_array($id) && $value === null) { |
||
50 | $value = $id; |
||
51 | |||
52 | if (is_object($id)) { |
||
53 | $id = get_class($id); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | return [$id, $value]; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param $id |
||
62 | * @param $value |
||
63 | * |
||
64 | * @return IDefinition |
||
65 | * @throws MissingDefinitionIdentifierException |
||
66 | * @throws MissingDefinitionValueException |
||
67 | */ |
||
68 | protected function delegateDefinitionCreation($id, $value) { |
||
69 | if (is_array($id)) { |
||
70 | $definition = $this->createDefinitionWithAliases($id, $value); |
||
71 | } else if ($this->isRegexPattern($id)) { |
||
72 | $definition = new WildcardDefinition($id, $value); |
||
73 | } else if (class_exists($id)) { |
||
74 | $definition = new ClassDefinition($id, $value); |
||
75 | } else if (interface_exists($id)) { |
||
76 | $definition = new InterfaceDefinition($id, $value); |
||
77 | } else { |
||
78 | $definition = new ValueDefinition($id, $value); |
||
79 | } |
||
80 | |||
81 | return $definition; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param $id |
||
86 | * |
||
87 | * @return bool |
||
88 | */ |
||
89 | public function hasDefinition($id) { |
||
90 | return $this->getDefinition($id) !== null; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param $id |
||
95 | */ |
||
96 | public function removeDefinition($id) { |
||
97 | $index = $this->getDefinitionIndex($id); |
||
98 | |||
99 | if ($index !== null) { |
||
100 | $definition = $this->definitions[$index]; |
||
101 | |||
102 | if ($definition instanceof IDefinition && |
||
103 | ! $definition instanceof WildcardDefinition) { |
||
104 | array_remove($this->definitions, $index); |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param array $ids |
||
111 | * @param $value |
||
112 | * |
||
113 | * @return IDefinition |
||
114 | * @throws MissingDefinitionIdentifierException |
||
115 | * @throws MissingDefinitionValueException |
||
116 | */ |
||
117 | protected function createDefinitionWithAliases(array $ids, $value) { |
||
118 | if ($value == null) { |
||
119 | throw new MissingDefinitionValueException( |
||
120 | s('Trying to register a class with alias without a value. Received %s.', json_encode($ids)) |
||
121 | ); |
||
122 | } else if (count($ids) == 0) { |
||
123 | throw new MissingDefinitionIdentifierException( |
||
124 | 'Trying to create a definition without an identifier.' |
||
125 | ); |
||
126 | } |
||
127 | |||
128 | $definition = null; |
||
129 | |||
130 | foreach ($ids as $id) { |
||
131 | if ( ! $definition instanceof IDefinition) { |
||
132 | $definition = $this->createDefinition($id, $value); |
||
133 | } else { |
||
134 | $alias = $this->createAliasDefinition($definition, $id); |
||
135 | $definition->addAlias($alias); |
||
136 | $this->addDefinition($alias); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | return $definition; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param IDefinition $definition |
||
145 | * @param $id |
||
146 | * |
||
147 | * @return AliasDefinition |
||
148 | */ |
||
149 | protected function createAliasDefinition(IDefinition $definition, $id) { |
||
150 | return new AliasDefinition($id, $definition); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param $id |
||
155 | * |
||
156 | * @return IDefinition |
||
157 | */ |
||
158 | public function getDefinition($id) { |
||
159 | foreach ($this->definitions as $definition) { |
||
160 | if ($definition instanceof WildcardDefinition) { |
||
161 | if ($this->matchRegexPattern($id, $definition->getId())) { |
||
162 | return $definition; |
||
163 | } |
||
164 | } else if ($definition->getId() == $id) { |
||
165 | if ($definition instanceof AliasDefinition) { |
||
166 | return $definition->getValue(); |
||
167 | } |
||
168 | |||
169 | return $definition; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | return null; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param $id |
||
178 | * |
||
179 | * @return int|null|string |
||
180 | */ |
||
181 | public function getDefinitionIndex($id) { |
||
182 | $definition = $this->getDefinition($id); |
||
183 | |||
184 | foreach ($this->definitions as $index => $item) { |
||
185 | if ($item === $definition) { |
||
186 | return $index; |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param IDefinition $definition |
||
193 | */ |
||
194 | public function addDefinition(IDefinition $definition) { |
||
195 | $this->removeDefinition($definition->getId()); |
||
196 | array_unshift($this->definitions, $definition); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param $string |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | protected function isRegexPattern($string) { |
||
205 | return str_starts_with($string, '/') && |
||
206 | str_ends_with($string, '/') || |
||
207 | str_starts_with($string, '#') && |
||
208 | str_ends_with($string, '#'); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param $string |
||
213 | * @param $pattern |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | protected function matchRegexPattern($string, $pattern) { |
||
218 | return preg_match($pattern, $string) == 1; |
||
219 | } |
||
220 | } |
||
221 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: