PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.8.1
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.8.1
4.9.2 4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 All 200 releases
betterdocs / includes / Dependencies / DI / Definition / Dumper / ObjectDefinitionDumper.php

ObjectDefinitionDumper.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 3.8.1, at includes/Dependencies/DI/Definition/Dumper/ObjectDefinitionDumper.php

144 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 WPDeveloper\BetterDocs\Dependencies\DI\Definition\Dumper;
6
7 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition;
8 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ObjectDefinition;
9 use WPDeveloper\BetterDocs\Dependencies\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 <matthieu@mnapoli.fr>
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 if ($value instanceof Definition) {
76 $valueStr = (string) $value;
77 } else {
78 $valueStr = var_export($value, true);
79 }
80
81 $str .= sprintf(PHP_EOL . ' $%s = %s', $propertyInjection->getPropertyName(), $valueStr);
82 }
83
84 return $str;
85 }
86
87 private function dumpMethods(string $className, ObjectDefinition $definition) : string
88 {
89 $str = '';
90
91 foreach ($definition->getMethodInjections() as $methodInjection) {
92 $parameters = $this->dumpMethodParameters($className, $methodInjection);
93
94 $str .= sprintf(PHP_EOL . ' %s(' . PHP_EOL . ' %s' . PHP_EOL . ' )', $methodInjection->getMethodName(), $parameters);
95 }
96
97 return $str;
98 }
99
100 private function dumpMethodParameters(string $className, MethodInjection $methodInjection) : string
101 {
102 $methodReflection = new \ReflectionMethod($className, $methodInjection->getMethodName());
103
104 $args = [];
105
106 $definitionParameters = $methodInjection->getParameters();
107
108 foreach ($methodReflection->getParameters() as $index => $parameter) {
109 if (array_key_exists($index, $definitionParameters)) {
110 $value = $definitionParameters[$index];
111
112 if ($value instanceof Definition) {
113 $valueStr = (string) $value;
114 } else {
115 $valueStr = var_export($value, true);
116 }
117 $args[] = sprintf('$%s = %s', $parameter->getName(), $valueStr);
118
119 continue;
120 }
121
122 // If the parameter is optional and wasn't specified, we take its default value
123 if ($parameter->isOptional()) {
124 try {
125 $value = $parameter->getDefaultValue();
126
127 $args[] = sprintf(
128 '$%s = (default value) %s',
129 $parameter->getName(),
130 var_export($value, true)
131 );
132 continue;
133 } catch (ReflectionException $e) {
134 // The default value can't be read through Reflection because it is a PHP internal class
135 }
136 }
137
138 $args[] = sprintf('$%s = #UNDEFINED#', $parameter->getName());
139 }
140
141 return implode(PHP_EOL . ' ', $args);
142 }
143 }
144