PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.9.0
Defender Security – Malware Scanner, Login Security & Firewall v5.9.0
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 / Dumper / ObjectDefinitionDumper.php

ObjectDefinitionDumper.php in Defender Security – Malware Scanner, Login Security & Firewall 5.9.0, at lib/packages/DI/Definition/Dumper/ObjectDefinitionDumper.php

145 lines 4.6 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\Dumper;
6
7 use WPMU_DEV\Defender\Vendor\DI\Definition\Definition;
8 use WPMU_DEV\Defender\Vendor\DI\Definition\ObjectDefinition;
9 use WPMU_DEV\Defender\Vendor\DI\Definition\ObjectDefinition\MethodInjection;
10 use ReflectionException;
11
12 /**
13 * Dumps object definitions to string for debugging purposes.
14 *
15 * @since 4.1
16 * @author Matthieu Napoli <[email protected]>
17 */
18 class ObjectDefinitionDumper
19 {
20 /**
21 * Returns the definition as string representation.
22 */
23 public function dump(ObjectDefinition $definition) : string
24 {
25 $className = $definition->getClassName();
26 $classExist = class_exists($className) || interface_exists($className);
27
28 // Class
29 if (! $classExist) {
30 $warning = '#UNKNOWN# ';
31 } else {
32 $class = new \ReflectionClass($className);
33 $warning = $class->isInstantiable() ? '' : '#NOT INSTANTIABLE# ';
34 }
35 $str = sprintf(' class = %s%s', $warning, $className);
36
37 // Lazy
38 $str .= \PHP_EOL . ' lazy = ' . var_export($definition->isLazy(), true);
39
40 if ($classExist) {
41 // Constructor
42 $str .= $this->dumpConstructor($className, $definition);
43
44 // Properties
45 $str .= $this->dumpProperties($definition);
46
47 // Methods
48 $str .= $this->dumpMethods($className, $definition);
49 }
50
51 return sprintf('Object (' . \PHP_EOL . '%s' . \PHP_EOL . ')', $str);
52 }
53
54 /**
55 * @param class-string $className
56 */
57 private function dumpConstructor(string $className, ObjectDefinition $definition) : string
58 {
59 $str = '';
60
61 $constructorInjection = $definition->getConstructorInjection();
62
63 if ($constructorInjection !== null) {
64 $parameters = $this->dumpMethodParameters($className, $constructorInjection);
65
66 $str .= sprintf(\PHP_EOL . ' __construct(' . \PHP_EOL . ' %s' . \PHP_EOL . ' )', $parameters);
67 }
68
69 return $str;
70 }
71
72 private function dumpProperties(ObjectDefinition $definition) : string
73 {
74 $str = '';
75
76 foreach ($definition->getPropertyInjections() as $propertyInjection) {
77 $value = $propertyInjection->getValue();
78 $valueStr = $value instanceof Definition ? (string) $value : var_export($value, true);
79
80 $str .= sprintf(\PHP_EOL . ' $%s = %s', $propertyInjection->getPropertyName(), $valueStr);
81 }
82
83 return $str;
84 }
85
86 /**
87 * @param class-string $className
88 */
89 private function dumpMethods(string $className, ObjectDefinition $definition) : string
90 {
91 $str = '';
92
93 foreach ($definition->getMethodInjections() as $methodInjection) {
94 $parameters = $this->dumpMethodParameters($className, $methodInjection);
95
96 $str .= sprintf(\PHP_EOL . ' %s(' . \PHP_EOL . ' %s' . \PHP_EOL . ' )', $methodInjection->getMethodName(), $parameters);
97 }
98
99 return $str;
100 }
101
102 /**
103 * @param class-string $className
104 */
105 private function dumpMethodParameters(string $className, MethodInjection $methodInjection) : string
106 {
107 $methodReflection = new \ReflectionMethod($className, $methodInjection->getMethodName());
108
109 $args = [];
110
111 $definitionParameters = $methodInjection->getParameters();
112
113 foreach ($methodReflection->getParameters() as $index => $parameter) {
114 if (array_key_exists($index, $definitionParameters)) {
115 $value = $definitionParameters[$index];
116 $valueStr = $value instanceof Definition ? (string) $value : var_export($value, true);
117
118 $args[] = sprintf('$%s = %s', $parameter->getName(), $valueStr);
119
120 continue;
121 }
122
123 // If the parameter is optional and wasn't specified, we take its default value
124 if ($parameter->isOptional()) {
125 try {
126 $value = $parameter->getDefaultValue();
127
128 $args[] = sprintf(
129 '$%s = (default value) %s',
130 $parameter->getName(),
131 var_export($value, true)
132 );
133 continue;
134 } catch (ReflectionException) {
135 // The default value can't be read through Reflection because it is a PHP internal class
136 }
137 }
138
139 $args[] = sprintf('$%s = #UNDEFINED#', $parameter->getName());
140 }
141
142 return implode(\PHP_EOL . ' ', $args);
143 }
144 }
145