PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Concerns / DependencyResolvable.php
kirki / libraries / framework / Concerns Last commit date
DeepGettable.php 3 weeks ago DependencyResolvable.php 3 weeks ago Dispatchable.php 3 weeks ago HasConstants.php 3 weeks ago
DependencyResolvable.php
94 lines
1 <?php
2
3 /**
4 * Trait that resolves method parameters via reflection and the application container.
5 * Injects typed class dependencies automatically and fills primitive arguments from a provided map or defaults.
6 * Powers controller and route action resolution without manual wiring.
7 *
8 * @package Framework
9 * @subpackage Concerns
10 * @since 1.0.0
11 */
12 namespace Kirki\Framework\Concerns;
13
14 \defined('ABSPATH') || exit;
15 use ReflectionException;
16 use ReflectionMethod;
17 use ReflectionNamedType;
18 use ReflectionParameter;
19 use function Kirki\Framework\app;
20 trait DependencyResolvable
21 {
22 /**
23 * Resolve primitive.
24 *
25 * @param ReflectionParameter $parameter The parameter.
26 *
27 * @return mixed
28 *
29 * @throws \ReflectionException
30 *
31 * @since 1.0.0
32 */
33 protected function resolve_primitive(ReflectionParameter $parameter)
34 {
35 if ($parameter->isDefaultValueAvailable()) {
36 return $parameter->getDefaultValue();
37 }
38 throw new ReflectionException(\sprintf('Unable to resolve primitive parameter "%s" in class "%s".', $parameter->getName(), $parameter->getDeclaringClass()->getName()));
39 }
40 /**
41 * Resolve the method dependencies.
42 *
43 * @param mixed $class The class.
44 * @param string $method The method name.
45 * @param array $arguments The arguments.
46 *
47 * @return array
48 *
49 * @since 1.0.0
50 */
51 protected function resolve_method_dependencies($class, string $method, array $arguments = [])
52 {
53 $dependencies = [];
54 $reflector = new ReflectionMethod($class, $method);
55 foreach ($reflector->getParameters() as $parameter) {
56 $type = $parameter->getType();
57 if ($type === null || $type->isBuiltin()) {
58 $dependencies[] = $this->resolve_primitive($parameter);
59 continue;
60 }
61 $type_name = $type instanceof ReflectionNamedType ? $type->getName() : (string) $type;
62 $argument = $this->pick($arguments, $type_name);
63 if (\is_object($argument) && \is_a($argument, $type_name)) {
64 $dependencies[] = $argument;
65 continue;
66 }
67 if (\is_string($argument) && $argument === $type_name) {
68 $dependencies[] = app()->make($argument);
69 }
70 }
71 return $dependencies;
72 }
73 /**
74 * Pick the argument from the arguments array.
75 *
76 * @param array $arguments The arguments array.
77 * @param string $type_name The type name.
78 *
79 * @return mixed
80 *
81 * @since 1.0.0
82 */
83 protected function pick(array $arguments, string $type_name)
84 {
85 foreach ($arguments as $argument) {
86 $name = \is_object($argument) ? \get_class($argument) : $argument;
87 if ($name === $type_name) {
88 return $argument;
89 }
90 }
91 return null;
92 }
93 }
94