PluginProbe
Elementor Website Builder – more than just a page builder / 3.25.4
Elementor Website Builder – more than just a page builder v3.25.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / vendor_prefixed / php-di / php-di / src / Definition / ObjectDefinition.php

ObjectDefinition.php in Elementor Website Builder – more than just a page builder 3.25.4, at vendor_prefixed/php-di/php-di/src/Definition/ObjectDefinition.php

224 lines 6.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 namespace ElementorDeps\DI\Definition;
5
6 use ElementorDeps\DI\Definition\Dumper\ObjectDefinitionDumper;
7 use ElementorDeps\DI\Definition\ObjectDefinition\MethodInjection;
8 use ElementorDeps\DI\Definition\ObjectDefinition\PropertyInjection;
9 use ElementorDeps\DI\Definition\Source\DefinitionArray;
10 use ReflectionClass;
11 /**
12 * Defines how an object can be instantiated.
13 *
14 * @author Matthieu Napoli <matthieu@mnapoli.fr>
15 */
16 class ObjectDefinition implements Definition
17 {
18 /**
19 * Entry name (most of the time, same as $classname).
20 * @var string
21 */
22 private $name;
23 /**
24 * Class name (if null, then the class name is $name).
25 * @var string|null
26 */
27 protected $className;
28 /**
29 * Constructor parameter injection.
30 * @var MethodInjection|null
31 */
32 protected $constructorInjection;
33 /**
34 * Property injections.
35 * @var PropertyInjection[]
36 */
37 protected $propertyInjections = [];
38 /**
39 * Method calls.
40 * @var MethodInjection[][]
41 */
42 protected $methodInjections = [];
43 /**
44 * @var bool|null
45 */
46 protected $lazy;
47 /**
48 * Store if the class exists. Storing it (in cache) avoids recomputing this.
49 *
50 * @var bool
51 */
52 private $classExists;
53 /**
54 * Store if the class is instantiable. Storing it (in cache) avoids recomputing this.
55 *
56 * @var bool
57 */
58 private $isInstantiable;
59 /**
60 * @param string $name Entry name
61 */
62 public function __construct(string $name, string $className = null)
63 {
64 $this->name = $name;
65 $this->setClassName($className);
66 }
67 public function getName() : string
68 {
69 return $this->name;
70 }
71 public function setName(string $name)
72 {
73 $this->name = $name;
74 }
75 public function setClassName(string $className = null)
76 {
77 $this->className = $className;
78 $this->updateCache();
79 }
80 public function getClassName() : string
81 {
82 if ($this->className !== null) {
83 return $this->className;
84 }
85 return $this->name;
86 }
87 /**
88 * @return MethodInjection|null
89 */
90 public function getConstructorInjection()
91 {
92 return $this->constructorInjection;
93 }
94 public function setConstructorInjection(MethodInjection $constructorInjection)
95 {
96 $this->constructorInjection = $constructorInjection;
97 }
98 public function completeConstructorInjection(MethodInjection $injection)
99 {
100 if ($this->constructorInjection !== null) {
101 // Merge
102 $this->constructorInjection->merge($injection);
103 } else {
104 // Set
105 $this->constructorInjection = $injection;
106 }
107 }
108 /**
109 * @return PropertyInjection[] Property injections
110 */
111 public function getPropertyInjections() : array
112 {
113 return $this->propertyInjections;
114 }
115 public function addPropertyInjection(PropertyInjection $propertyInjection)
116 {
117 $className = $propertyInjection->getClassName();
118 if ($className) {
119 // Index with the class name to avoid collisions between parent and
120 // child private properties with the same name
121 $key = $className . '::' . $propertyInjection->getPropertyName();
122 } else {
123 $key = $propertyInjection->getPropertyName();
124 }
125 $this->propertyInjections[$key] = $propertyInjection;
126 }
127 /**
128 * @return MethodInjection[] Method injections
129 */
130 public function getMethodInjections() : array
131 {
132 // Return array leafs
133 $injections = [];
134 \array_walk_recursive($this->methodInjections, function ($injection) use(&$injections) {
135 $injections[] = $injection;
136 });
137 return $injections;
138 }
139 public function addMethodInjection(MethodInjection $methodInjection)
140 {
141 $method = $methodInjection->getMethodName();
142 if (!isset($this->methodInjections[$method])) {
143 $this->methodInjections[$method] = [];
144 }
145 $this->methodInjections[$method][] = $methodInjection;
146 }
147 public function completeFirstMethodInjection(MethodInjection $injection)
148 {
149 $method = $injection->getMethodName();
150 if (isset($this->methodInjections[$method][0])) {
151 // Merge
152 $this->methodInjections[$method][0]->merge($injection);
153 } else {
154 // Set
155 $this->addMethodInjection($injection);
156 }
157 }
158 public function setLazy(bool $lazy = null)
159 {
160 $this->lazy = $lazy;
161 }
162 public function isLazy() : bool
163 {
164 if ($this->lazy !== null) {
165 return $this->lazy;
166 }
167 // Default value
168 return \false;
169 }
170 public function classExists() : bool
171 {
172 return $this->classExists;
173 }
174 public function isInstantiable() : bool
175 {
176 return $this->isInstantiable;
177 }
178 public function replaceNestedDefinitions(callable $replacer)
179 {
180 \array_walk($this->propertyInjections, function (PropertyInjection $propertyInjection) use($replacer) {
181 $propertyInjection->replaceNestedDefinition($replacer);
182 });
183 if ($this->constructorInjection) {
184 $this->constructorInjection->replaceNestedDefinitions($replacer);
185 }
186 \array_walk($this->methodInjections, function ($injectionArray) use($replacer) {
187 \array_walk($injectionArray, function (MethodInjection $methodInjection) use($replacer) {
188 $methodInjection->replaceNestedDefinitions($replacer);
189 });
190 });
191 }
192 /**
193 * Replaces all the wildcards in the string with the given replacements.
194 *
195 * @param string[] $replacements
196 */
197 public function replaceWildcards(array $replacements)
198 {
199 $className = $this->getClassName();
200 foreach ($replacements as $replacement) {
201 $pos = \strpos($className, DefinitionArray::WILDCARD);
202 if ($pos !== \false) {
203 $className = \substr_replace($className, $replacement, $pos, 1);
204 }
205 }
206 $this->setClassName($className);
207 }
208 public function __toString()
209 {
210 return (new ObjectDefinitionDumper())->dump($this);
211 }
212 private function updateCache()
213 {
214 $className = $this->getClassName();
215 $this->classExists = \class_exists($className) || \interface_exists($className);
216 if (!$this->classExists) {
217 $this->isInstantiable = \false;
218 return;
219 }
220 $class = new ReflectionClass($className);
221 $this->isInstantiable = $class->isInstantiable();
222 }
223 }
224