1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Interceptors\Internal; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerExceptionInterface; |
8
|
|
|
use Spiral\Core\Exception\Resolver\ArgumentResolvingException; |
9
|
|
|
use Spiral\Core\Exception\Resolver\InvalidArgumentException; |
10
|
|
|
use Spiral\Core\ResolverInterface; |
11
|
|
|
use Spiral\Interceptors\Exception\TargetCallException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
final class ActionResolver |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @psalm-assert class-string $controller |
20
|
|
|
* @psalm-assert non-empty-string $action |
21
|
|
|
* |
22
|
|
|
* @throws TargetCallException |
23
|
|
|
*/ |
24
|
81 |
|
public static function pathToReflection(string $controller, string $action): \ReflectionMethod |
25
|
|
|
{ |
26
|
|
|
try { |
27
|
|
|
/** @psalm-suppress ArgumentTypeCoercion */ |
28
|
81 |
|
$method = new \ReflectionMethod($controller, $action); |
29
|
11 |
|
} catch (\ReflectionException $e) { |
30
|
11 |
|
throw new TargetCallException( |
31
|
11 |
|
\sprintf('Invalid action `%s`->`%s`', $controller, $action), |
32
|
11 |
|
TargetCallException::BAD_ACTION, |
33
|
11 |
|
$e |
34
|
11 |
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
70 |
|
return $method; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws TargetCallException |
42
|
|
|
* @psalm-assert object|null $controller |
43
|
|
|
*/ |
44
|
1 |
|
public static function validateControllerMethod(\ReflectionMethod $method, mixed $controller = null): void |
45
|
|
|
{ |
46
|
1 |
|
if ($method->isStatic() || !$method->isPublic()) { |
47
|
|
|
throw new TargetCallException( |
48
|
|
|
\sprintf( |
49
|
|
|
'Invalid action `%s`->`%s`', |
50
|
|
|
$method->getDeclaringClass()->getName(), |
51
|
|
|
$method->getName(), |
52
|
|
|
), |
53
|
|
|
TargetCallException::BAD_ACTION |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
if ($controller === null) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
if (!\is_object($controller) || !$method->getDeclaringClass()->isInstance($controller)) { |
62
|
|
|
throw new TargetCallException( |
63
|
|
|
\sprintf( |
64
|
|
|
'Invalid controller. Expected instance of `%s`, got `%s`.', |
65
|
|
|
$method->getDeclaringClass()->getName(), |
66
|
|
|
\get_debug_type($controller), |
67
|
|
|
), |
68
|
|
|
TargetCallException::INVALID_CONTROLLER, |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @throws TargetCallException |
75
|
|
|
* @throws \Throwable |
76
|
|
|
*/ |
77
|
|
|
public static function resolveArguments( |
78
|
|
|
ResolverInterface $resolver, |
79
|
|
|
\ReflectionMethod $method, |
80
|
|
|
array $arguments, |
81
|
|
|
): array { |
82
|
|
|
try { |
83
|
|
|
return $resolver->resolveArguments($method, $arguments); |
84
|
|
|
} catch (ArgumentResolvingException|InvalidArgumentException $e) { |
85
|
|
|
throw new TargetCallException( |
86
|
|
|
\sprintf( |
87
|
|
|
'Missing/invalid parameter %s of `%s`->`%s`.', |
88
|
|
|
$e->getParameter(), |
89
|
|
|
$method->getDeclaringClass()->getName(), |
90
|
|
|
$method->getName(), |
91
|
|
|
), |
92
|
|
|
TargetCallException::BAD_ARGUMENT, |
93
|
|
|
$e, |
94
|
|
|
); |
95
|
|
|
} catch (ContainerExceptionInterface $e) { |
96
|
|
|
throw new TargetCallException( |
97
|
|
|
$e->getMessage(), |
98
|
|
|
TargetCallException::ERROR, |
99
|
|
|
$e, |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|