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 / Helper / CreateDefinitionHelper.php

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

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