|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Rareloop\Lumberjack\Http\Resolvers; |
| 4 | + |
| 5 | +use Illuminate\Support\Arr; |
| 6 | +use Invoker\ParameterResolver\ParameterResolver; |
| 7 | +use Rareloop\Lumberjack\Exceptions\MismatchedContextException; |
| 8 | +use Rareloop\Lumberjack\Exceptions\MissingContextException; |
| 9 | +use ReflectionFunctionAbstract; |
| 10 | + |
| 11 | +abstract class AbstractContextResolver implements ParameterResolver |
| 12 | +{ |
| 13 | + public function getParameters( |
| 14 | + ReflectionFunctionAbstract $reflection, |
| 15 | + array $providedParameters, |
| 16 | + array $resolvedParameters |
| 17 | + ): array { |
| 18 | + foreach ($reflection->getParameters() as $parameter) { |
| 19 | + if (Arr::has($resolvedParameters, $parameter->getPosition())) { |
| 20 | + continue; |
| 21 | + } |
| 22 | + |
| 23 | + $type = $parameter->getType(); |
| 24 | + |
| 25 | + if (!$type || $type->isBuiltin()) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + $className = $type->getName(); |
| 30 | + |
| 31 | + if (!$this->canResolveClass($className)) { |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + $context = $this->getContext(); |
| 37 | + |
| 38 | + if (is_null($context)) { |
| 39 | + throw MissingContextException::forType($className, $context); |
| 40 | + } |
| 41 | + |
| 42 | + if (!$this->isValidContext($context, $className)) { |
| 43 | + throw MismatchedContextException::forIncorrectClass($className, $context); |
| 44 | + } |
| 45 | + |
| 46 | + $resolvedObject = $this->resolveObject($className, $context); |
| 47 | + |
| 48 | + if (!is_null($resolvedObject) && !$resolvedObject instanceof $className) { |
| 49 | + throw MismatchedContextException::forIncorrectClass($className, $resolvedObject); |
| 50 | + } |
| 51 | + |
| 52 | + $resolvedParameters[$parameter->getPosition()] = $resolvedObject; |
| 53 | + } catch (MissingContextException | MismatchedContextException $e) { |
| 54 | + // If the context is entirely missing or mismatched, we allow null if the typehint supports it |
| 55 | + if (!$parameter->allowsNull()) { |
| 56 | + throw $e; |
| 57 | + } |
| 58 | + |
| 59 | + $resolvedParameters[$parameter->getPosition()] = null; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return $resolvedParameters; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get the raw context object to resolve from (e.g. WP_Post, WP_Term, WP_Query). |
| 68 | + * Defaults to the current WordPress queried object. |
| 69 | + * |
| 70 | + * @return mixed |
| 71 | + */ |
| 72 | + protected function getContext(): mixed |
| 73 | + { |
| 74 | + return get_queried_object(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Determine if this resolver can handle the given class type-hint. |
| 79 | + * |
| 80 | + * @param string $className |
| 81 | + * @return bool |
| 82 | + */ |
| 83 | + abstract protected function canResolveClass(string $className): bool; |
| 84 | + |
| 85 | + /** |
| 86 | + * Determine if the current context is valid for this resolver. |
| 87 | + * |
| 88 | + * @param mixed $context |
| 89 | + * @param string $className |
| 90 | + * @return bool |
| 91 | + */ |
| 92 | + abstract protected function isValidContext(mixed $context, string $className): bool; |
| 93 | + |
| 94 | + /** |
| 95 | + * Build the concrete object instance from the raw context. |
| 96 | + * |
| 97 | + * @param string $className |
| 98 | + * @param mixed $context |
| 99 | + * @return mixed |
| 100 | + */ |
| 101 | + abstract protected function resolveObject(string $className, mixed $context): mixed; |
| 102 | +} |
0 commit comments