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