|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sunrise\Http\Router; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
18
|
|
|
use ReflectionClass; |
|
19
|
|
|
use ReflectionException; |
|
20
|
|
|
use ReflectionMethod; |
|
21
|
|
|
|
|
22
|
|
|
use function is_array; |
|
23
|
|
|
use function is_callable; |
|
24
|
|
|
use function is_string; |
|
25
|
|
|
use function is_subclass_of; |
|
26
|
|
|
use function sprintf; |
|
27
|
|
|
|
|
28
|
|
|
use const PHP_VERSION_ID; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @since 3.0.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
final class RequestHandlerReflector implements RequestHandlerReflectorInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @inheritDoc |
|
37
|
|
|
* |
|
38
|
|
|
* @return ReflectionClass<RequestHandlerInterface>|ReflectionMethod |
|
39
|
|
|
* |
|
40
|
|
|
* @throws InvalidArgumentException |
|
41
|
|
|
* @throws ReflectionException |
|
42
|
|
|
*/ |
|
43
|
10 |
|
public function reflectRequestHandler(mixed $reference): ReflectionClass|ReflectionMethod |
|
44
|
|
|
{ |
|
45
|
10 |
|
if ($reference instanceof RequestHandlerInterface) { |
|
46
|
1 |
|
return new ReflectionClass($reference); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
9 |
|
if (is_string($reference) && is_subclass_of($reference, RequestHandlerInterface::class)) { |
|
50
|
4 |
|
return new ReflectionClass($reference); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// https://github.com/php/php-src/blob/3ed526441400060aa4e618b91b3352371fcd02a8/Zend/zend_API.c#L3884-L3932 |
|
54
|
8 |
|
if (is_array($reference) && is_callable($reference, true, $referenceName)) { |
|
55
|
|
|
try { |
|
56
|
|
|
// @codeCoverageIgnoreStart |
|
57
|
|
|
if (PHP_VERSION_ID < 80300) { |
|
58
|
|
|
return new ReflectionMethod($referenceName); |
|
|
|
|
|
|
59
|
|
|
} // @codeCoverageIgnoreEnd |
|
60
|
|
|
|
|
61
|
|
|
/** @psalm-var ReflectionMethod */ |
|
62
|
7 |
|
return ReflectionMethod::createFromMethodName($referenceName); |
|
|
|
|
|
|
63
|
2 |
|
} catch (ReflectionException) { |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
throw new InvalidArgumentException(sprintf( |
|
68
|
3 |
|
'The request handler reference "%s" could not be reflected.', |
|
69
|
3 |
|
ReferenceResolver::stringifyReference($reference), |
|
70
|
3 |
|
)); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|