PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.5.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.5.5
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 3.5.2 All 199 releases
betterdocs / includes / Dependencies / DI / Definition / Helper / CreateDefinitionHelper.php

CreateDefinitionHelper.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.5.5, at includes/Dependencies/DI/Definition/Helper/CreateDefinitionHelper.php

204 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:ignoreFile -- Bundled third-party (Mozart) dependency; exempt from plugin coding standards.
3
4 declare(strict_types=1);
5
6 namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition\Helper;
7
8 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition;
9 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Exception\InvalidDefinition;
10 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ObjectDefinition;
11 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ObjectDefinition\MethodInjection;
12 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ObjectDefinition\PropertyInjection;
13
14 /**
15 * Helps defining how to create an instance of a class.
16 *
17 * @author Matthieu Napoli <matthieu@mnapoli.fr>
18 */
19 class CreateDefinitionHelper implements DefinitionHelper
20 {
21 const DEFINITION_CLASS = ObjectDefinition::class;
22
23 /**
24 * @var string|null
25 */
26 private $className;
27
28 /**
29 * @var bool|null
30 */
31 private $lazy;
32
33 /**
34 * Array of constructor parameters.
35 * @var array
36 */
37 protected $constructor = [];
38
39 /**
40 * Array of properties and their value.
41 * @var array
42 */
43 private $properties = [];
44
45 /**
46 * Array of methods and their parameters.
47 * @var array
48 */
49 protected $methods = [];
50
51 /**
52 * Helper for defining an object.
53 *
54 * @param string|null $className Class name of the object.
55 * If null, the name of the entry (in the container) will be used as class name.
56 */
57 public function __construct(?string $className = null)
58 {
59 $this->className = $className;
60 }
61
62 /**
63 * Define the entry as lazy.
64 *
65 * A lazy entry is created only when it is used, a proxy is injected instead.
66 *
67 * @return $this
68 */
69 public function lazy()
70 {
71 $this->lazy = true;
72
73 return $this;
74 }
75
76 /**
77 * Defines the arguments to use to call the constructor.
78 *
79 * This method takes a variable number of arguments, example:
80 * ->constructor($param1, $param2, $param3)
81 *
82 * @param mixed ... Parameters to use for calling the constructor of the class.
83 *
84 * @return $this
85 */
86 public function constructor(...$parameters)
87 {
88 $this->constructor = $parameters;
89
90 return $this;
91 }
92
93 /**
94 * Defines a value to inject in a property of the object.
95 *
96 * @param string $property Entry in which to inject the value.
97 * @param mixed $value Value to inject in the property.
98 *
99 * @return $this
100 */
101 public function property(string $property, $value)
102 {
103 $this->properties[$property] = $value;
104
105 return $this;
106 }
107
108 /**
109 * Defines a method to call and the arguments to use.
110 *
111 * This method takes a variable number of arguments after the method name, example:
112 *
113 * ->method('myMethod', $param1, $param2)
114 *
115 * Can be used multiple times to declare multiple calls.
116 *
117 * @param string $method Name of the method to call.
118 * @param mixed ... Parameters to use for calling the method.
119 *
120 * @return $this
121 */
122 public function method(string $method, ...$parameters)
123 {
124 if (! isset($this->methods[$method])) {
125 $this->methods[$method] = [];
126 }
127
128 $this->methods[$method][] = $parameters;
129
130 return $this;
131 }
132
133 /**
134 * @return ObjectDefinition
135 */
136 public function getDefinition(string $entryName) : Definition
137 {
138 $class = $this::DEFINITION_CLASS;
139 /** @var ObjectDefinition $definition */
140 $definition = new $class($entryName, $this->className);
141
142 if ($this->lazy !== null) {
143 $definition->setLazy($this->lazy);
144 }
145
146 if (! empty($this->constructor)) {
147 $parameters = $this->fixParameters($definition, '__construct', $this->constructor);
148 $constructorInjection = MethodInjection::constructor($parameters);
149 $definition->setConstructorInjection($constructorInjection);
150 }
151
152 if (! empty($this->properties)) {
153 foreach ($this->properties as $property => $value) {
154 $definition->addPropertyInjection(
155 new PropertyInjection($property, $value)
156 );
157 }
158 }
159
160 if (! empty($this->methods)) {
161 foreach ($this->methods as $method => $calls) {
162 foreach ($calls as $parameters) {
163 $parameters = $this->fixParameters($definition, $method, $parameters);
164 $methodInjection = new MethodInjection($method, $parameters);
165 $definition->addMethodInjection($methodInjection);
166 }
167 }
168 }
169
170 return $definition;
171 }
172
173 /**
174 * Fixes parameters indexed by the parameter name -> reindex by position.
175 *
176 * This is necessary so that merging definitions between sources is possible.
177 *
178 * @throws InvalidDefinition
179 */
180 private function fixParameters(ObjectDefinition $definition, string $method, array $parameters) : array
181 {
182 $fixedParameters = [];
183
184 foreach ($parameters as $index => $parameter) {
185 // Parameter indexed by the parameter name, we reindex it with its position
186 if (is_string($index)) {
187 $callable = [$definition->getClassName(), $method];
188
189 try {
190 $reflectionParameter = new \ReflectionParameter($callable, $index);
191 } catch (\ReflectionException $e) {
192 throw InvalidDefinition::create($definition, sprintf("Parameter with name '%s' could not be found. %s.", $index, $e->getMessage()));
193 }
194
195 $index = $reflectionParameter->getPosition();
196 }
197
198 $fixedParameters[$index] = $parameter;
199 }
200
201 return $fixedParameters;
202 }
203 }
204