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 |