|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Interceptors\Internal; |
|
6
|
|
|
|
|
7
|
|
|
use Spiral\Interceptors\Exception\TargetCallException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @internal |
|
11
|
|
|
*/ |
|
12
|
|
|
final class ActionResolver |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @psalm-assert class-string $controller |
|
16
|
|
|
* @psalm-assert non-empty-string $action |
|
17
|
|
|
* |
|
18
|
|
|
* @throws TargetCallException |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function pathToReflection(string $controller, string $action): \ReflectionMethod |
|
21
|
|
|
{ |
|
22
|
|
|
try { |
|
23
|
|
|
/** @psalm-suppress ArgumentTypeCoercion */ |
|
24
|
|
|
$method = new \ReflectionMethod($controller, $action); |
|
25
|
|
|
} catch (\ReflectionException $e) { |
|
26
|
|
|
throw new TargetCallException( |
|
27
|
|
|
\sprintf('Invalid action `%s`->`%s`', $controller, $action), |
|
28
|
|
|
TargetCallException::BAD_ACTION, |
|
29
|
|
|
$e |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $method; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @throws TargetCallException |
|
38
|
|
|
* @psalm-assert object|null $controller |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function validateControllerMethod(\ReflectionMethod $method, mixed $controller = null): void |
|
41
|
|
|
{ |
|
42
|
|
|
if ($method->isStatic() || !$method->isPublic()) { |
|
43
|
|
|
throw new TargetCallException( |
|
44
|
|
|
\sprintf( |
|
45
|
|
|
'Invalid action `%s`->`%s`', |
|
46
|
|
|
$method->getDeclaringClass()->getName(), |
|
47
|
|
|
$method->getName(), |
|
48
|
|
|
), |
|
49
|
|
|
TargetCallException::BAD_ACTION |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($controller === null) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (!\is_object($controller) || !$method->getDeclaringClass()->isInstance($controller)) { |
|
58
|
|
|
throw new TargetCallException( |
|
59
|
|
|
\sprintf( |
|
60
|
|
|
'Invalid controller. Expected instance of `%s`, got `%s`.', |
|
61
|
|
|
$method->getDeclaringClass()->getName(), |
|
62
|
|
|
\get_debug_type($controller), |
|
63
|
|
|
), |
|
64
|
|
|
TargetCallException::INVALID_CONTROLLER, |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|