PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.2
Defender Security – Malware Scanner, Login Security & Firewall v5.7.2
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 / lib / packages / DI / Definition / Source / ReflectionBasedAutowiring.php

ReflectionBasedAutowiring.php in Defender Security – Malware Scanner, Login Security & Firewall 5.7.2, at lib/packages/DI/Definition/Source/ReflectionBasedAutowiring.php

86 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace WPMU_DEV\Defender\Vendor\DI\Definition\Source;
6
7 use WPMU_DEV\Defender\Vendor\DI\Definition\ObjectDefinition;
8 use WPMU_DEV\Defender\Vendor\DI\Definition\ObjectDefinition\MethodInjection;
9 use WPMU_DEV\Defender\Vendor\DI\Definition\Reference;
10 use ReflectionNamedType;
11
12 /**
13 * Reads WPMU_DEV\Defender\Vendor\DI class definitions using reflection.
14 *
15 * @author Matthieu Napoli <[email protected]>
16 */
17 class ReflectionBasedAutowiring implements DefinitionSource, Autowiring
18 {
19 public function autowire(string $name, ?ObjectDefinition $definition = null) : ?ObjectDefinition
20 {
21 $className = $definition ? $definition->getClassName() : $name;
22
23 if (!class_exists($className) && !interface_exists($className)) {
24 return $definition;
25 }
26
27 $definition = $definition ?: new ObjectDefinition($name);
28
29 // Constructor
30 $class = new \ReflectionClass($className);
31 $constructor = $class->getConstructor();
32 if ($constructor && $constructor->isPublic()) {
33 $constructorInjection = MethodInjection::constructor($this->getParametersDefinition($constructor));
34 $definition->completeConstructorInjection($constructorInjection);
35 }
36
37 return $definition;
38 }
39
40 public function getDefinition(string $name) : ?ObjectDefinition
41 {
42 return $this->autowire($name);
43 }
44
45 /**
46 * Autowiring cannot guess all existing definitions.
47 */
48 public function getDefinitions() : array
49 {
50 return [];
51 }
52
53 /**
54 * Read the type-hinting from the parameters of the function.
55 */
56 private function getParametersDefinition(\ReflectionFunctionAbstract $constructor) : array
57 {
58 $parameters = [];
59
60 foreach ($constructor->getParameters() as $index => $parameter) {
61 // Skip optional parameters
62 if ($parameter->isOptional()) {
63 continue;
64 }
65
66 $parameterType = $parameter->getType();
67 if (!$parameterType) {
68 // No type
69 continue;
70 }
71 if (!$parameterType instanceof ReflectionNamedType) {
72 // Union types are not supported
73 continue;
74 }
75 if ($parameterType->isBuiltin()) {
76 // Primitive types are not supported
77 continue;
78 }
79
80 $parameters[$index] = new Reference($parameterType->getName());
81 }
82
83 return $parameters;
84 }
85 }
86