PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / php-di / php-di / src / DI / Definition / Helper / ObjectDefinitionHelper.php

ObjectDefinitionHelper.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at vendor/php-di/php-di/src/DI/Definition/Helper/ObjectDefinitionHelper.php

279 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace DI\Definition\Helper;
4
5 use DI\Definition\Exception\DefinitionException;
6 use DI\Definition\ObjectDefinition;
7 use DI\Definition\ObjectDefinition\MethodInjection;
8 use DI\Definition\ObjectDefinition\PropertyInjection;
9
10 /**
11 * Helps defining how to create an instance of a class.
12 *
13 * @author Matthieu Napoli <[email protected]>
14 */
15 class ObjectDefinitionHelper implements DefinitionHelper
16 {
17 /**
18 * @var string|null
19 */
20 private $className;
21
22 /**
23 * @var bool|null
24 */
25 private $lazy;
26
27 /**
28 * @var string|null
29 */
30 private $scope;
31
32 /**
33 * Array of constructor parameters.
34 * @var array
35 */
36 private $constructor = [];
37
38 /**
39 * Array of properties and their value.
40 * @var array
41 */
42 private $properties = [];
43
44 /**
45 * Array of methods and their parameters.
46 * @var array
47 */
48 private $methods = [];
49
50 /**
51 * Helper for defining an object.
52 *
53 * @param string|null $className Class name of the object.
54 * If null, the name of the entry (in the container) will be used as class name.
55 */
56 public function __construct($className = null)
57 {
58 $this->className = $className;
59 }
60
61 /**
62 * Define the entry as lazy.
63 *
64 * A lazy entry is created only when it is used, a proxy is injected instead.
65 *
66 * @return ObjectDefinitionHelper
67 */
68 public function lazy()
69 {
70 $this->lazy = true;
71
72 return $this;
73 }
74
75 /**
76 * Defines the scope of the entry.
77 *
78 * @param string $scope
79 *
80 * @return ObjectDefinitionHelper
81 */
82 public function scope($scope)
83 {
84 $this->scope = $scope;
85
86 return $this;
87 }
88
89 /**
90 * Defines the arguments to use to call the constructor.
91 *
92 * This method takes a variable number of arguments, example:
93 * ->constructor($param1, $param2, $param3)
94 *
95 * @param mixed ... Parameters to use for calling the constructor of the class.
96 *
97 * @return ObjectDefinitionHelper
98 */
99 public function constructor()
100 {
101 $this->constructor = func_get_args();
102
103 return $this;
104 }
105
106 /**
107 * Defines a value for a specific argument of the constructor.
108 *
109 * This method is usually used together with annotations or autowiring, when a parameter
110 * is not (or cannot be) type-hinted. Using this method instead of constructor() allows to
111 * avoid defining all the parameters (letting them being resolved using annotations or autowiring)
112 * and only define one.
113 *
114 * @param string $parameter Parameter for which the value will be given.
115 * @param mixed $value Value to give to this parameter.
116 *
117 * @return ObjectDefinitionHelper
118 */
119 public function constructorParameter($parameter, $value)
120 {
121 $this->constructor[$parameter] = $value;
122
123 return $this;
124 }
125
126 /**
127 * Defines a value to inject in a property of the object.
128 *
129 * @param string $property Entry in which to inject the value.
130 * @param mixed $value Value to inject in the property.
131 *
132 * @return ObjectDefinitionHelper
133 */
134 public function property($property, $value)
135 {
136 $this->properties[$property] = $value;
137
138 return $this;
139 }
140
141 /**
142 * Defines a method to call and the arguments to use.
143 *
144 * This method takes a variable number of arguments after the method name, example:
145 *
146 * ->method('myMethod', $param1, $param2)
147 *
148 * Can be used multiple times to declare multiple calls.
149 *
150 * @param string $method Name of the method to call.
151 * @param mixed ... Parameters to use for calling the method.
152 *
153 * @return ObjectDefinitionHelper
154 */
155 public function method($method)
156 {
157 $args = func_get_args();
158 array_shift($args);
159
160 if (! isset($this->methods[$method])) {
161 $this->methods[$method] = [];
162 }
163
164 $this->methods[$method][] = $args;
165
166 return $this;
167 }
168
169 /**
170 * Defines a method to call and a value for a specific argument.
171 *
172 * This method is usually used together with annotations or autowiring, when a parameter
173 * is not (or cannot be) type-hinted. Using this method instead of method() allows to
174 * avoid defining all the parameters (letting them being resolved using annotations or
175 * autowiring) and only define one.
176 *
177 * If multiple calls to the method have been configured already (e.g. in a previous definition)
178 * then this method only overrides the parameter for the *first* call.
179 *
180 * @param string $method Name of the method to call.
181 * @param string $parameter Name or index of the parameter for which the value will be given.
182 * @param mixed $value Value to give to this parameter.
183 *
184 * @return ObjectDefinitionHelper
185 */
186 public function methodParameter($method, $parameter, $value)
187 {
188 // Special case for the constructor
189 if ($method === '__construct') {
190 $this->constructor[$parameter] = $value;
191
192 return $this;
193 }
194
195 if (! isset($this->methods[$method])) {
196 $this->methods[$method] = [0 => []];
197 }
198
199 $this->methods[$method][0][$parameter] = $value;
200
201 return $this;
202 }
203
204 /**
205 * {@inheritdoc}
206 */
207 public function getDefinition($entryName)
208 {
209 $definition = new ObjectDefinition($entryName, $this->className);
210
211 if ($this->lazy !== null) {
212 $definition->setLazy($this->lazy);
213 }
214 if ($this->scope !== null) {
215 $definition->setScope($this->scope);
216 }
217
218 if (! empty($this->constructor)) {
219 $parameters = $this->fixParameters($definition, '__construct', $this->constructor);
220 $constructorInjection = MethodInjection::constructor($parameters);
221 $definition->setConstructorInjection($constructorInjection);
222 }
223
224 if (! empty($this->properties)) {
225 foreach ($this->properties as $property => $value) {
226 $definition->addPropertyInjection(
227 new PropertyInjection($property, $value)
228 );
229 }
230 }
231
232 if (! empty($this->methods)) {
233 foreach ($this->methods as $method => $calls) {
234 foreach ($calls as $parameters) {
235 $parameters = $this->fixParameters($definition, $method, $parameters);
236 $methodInjection = new MethodInjection($method, $parameters);
237 $definition->addMethodInjection($methodInjection);
238 }
239 }
240 }
241
242 return $definition;
243 }
244
245 /**
246 * Fixes parameters indexed by the parameter name -> reindex by position.
247 *
248 * This is necessary so that merging definitions between sources is possible.
249 *
250 * @param ObjectDefinition $definition
251 * @param string $method
252 * @param array $parameters
253 * @throws DefinitionException
254 * @return array
255 */
256 private function fixParameters(ObjectDefinition $definition, $method, $parameters)
257 {
258 $fixedParameters = [];
259
260 foreach ($parameters as $index => $parameter) {
261 // Parameter indexed by the parameter name, we reindex it with its position
262 if (is_string($index)) {
263 $callable = [$definition->getClassName(), $method];
264 try {
265 $reflectionParameter = new \ReflectionParameter($callable, $index);
266 } catch (\ReflectionException $e) {
267 throw DefinitionException::create($definition, sprintf("Parameter with name '%s' could not be found. %s.", $index, $e->getMessage()));
268 }
269
270 $index = $reflectionParameter->getPosition();
271 }
272
273 $fixedParameters[$index] = $parameter;
274 }
275
276 return $fixedParameters;
277 }
278 }
279