PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.6.0
Defender Security – Malware Scanner, Login Security & Firewall v5.6.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.6.0, at lib/packages/DI/Definition/Dumper/ObjectDefinitionDumper.php

136 lines 4.4 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 private function dumpConstructor(string $className, ObjectDefinition $definition) : string
55 {
56 $str = '';
57
58 $constructorInjection = $definition->getConstructorInjection();
59
60 if ($constructorInjection !== null) {
61 $parameters = $this->dumpMethodParameters($className, $constructorInjection);
62
63 $str .= sprintf(\PHP_EOL . ' __construct(' . \PHP_EOL . ' %s' . \PHP_EOL . ' )', $parameters);
64 }
65
66 return $str;
67 }
68
69 private function dumpProperties(ObjectDefinition $definition) : string
70 {
71 $str = '';
72
73 foreach ($definition->getPropertyInjections() as $propertyInjection) {
74 $value = $propertyInjection->getValue();
75 $valueStr = $value instanceof Definition ? (string) $value : var_export($value, true);
76
77 $str .= sprintf(\PHP_EOL . ' $%s = %s', $propertyInjection->getPropertyName(), $valueStr);
78 }
79
80 return $str;
81 }
82
83 private function dumpMethods(string $className, ObjectDefinition $definition) : string
84 {
85 $str = '';
86
87 foreach ($definition->getMethodInjections() as $methodInjection) {
88 $parameters = $this->dumpMethodParameters($className, $methodInjection);
89
90 $str .= sprintf(\PHP_EOL . ' %s(' . \PHP_EOL . ' %s' . \PHP_EOL . ' )', $methodInjection->getMethodName(), $parameters);
91 }
92
93 return $str;
94 }
95
96 private function dumpMethodParameters(string $className, MethodInjection $methodInjection) : string
97 {
98 $methodReflection = new \ReflectionMethod($className, $methodInjection->getMethodName());
99
100 $args = [];
101
102 $definitionParameters = $methodInjection->getParameters();
103
104 foreach ($methodReflection->getParameters() as $index => $parameter) {
105 if (array_key_exists($index, $definitionParameters)) {
106 $value = $definitionParameters[$index];
107 $valueStr = $value instanceof Definition ? (string) $value : var_export($value, true);
108
109 $args[] = sprintf('$%s = %s', $parameter->getName(), $valueStr);
110
111 continue;
112 }
113
114 // If the parameter is optional and wasn't specified, we take its default value
115 if ($parameter->isOptional()) {
116 try {
117 $value = $parameter->getDefaultValue();
118
119 $args[] = sprintf(
120 '$%s = (default value) %s',
121 $parameter->getName(),
122 var_export($value, true)
123 );
124 continue;
125 } catch (ReflectionException $e) {
126 // The default value can't be read through Reflection because it is a PHP internal class
127 }
128 }
129
130 $args[] = sprintf('$%s = #UNDEFINED#', $parameter->getName());
131 }
132
133 return implode(\PHP_EOL . ' ', $args);
134 }
135 }
136