1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Router\Loader; |
3
|
|
|
|
4
|
|
|
use Psr\Container\ContainerInterface; |
5
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use ReflectionException; |
10
|
|
|
use Wandu\Router\Contracts\LoaderInterface; |
11
|
|
|
use Wandu\Router\Contracts\MiddlewareInterface; |
12
|
|
|
use Wandu\Router\Exception\HandlerNotFoundException; |
13
|
|
|
|
14
|
|
|
class PsrLoader implements LoaderInterface |
15
|
|
|
{ |
16
|
|
|
/** @var \Psr\Container\ContainerInterface */ |
17
|
|
|
protected $container; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param \Psr\Container\ContainerInterface $container |
21
|
|
|
*/ |
22
|
5 |
|
public function __construct(ContainerInterface $container) |
23
|
|
|
{ |
24
|
5 |
|
$this->container = $container; |
25
|
5 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
2 |
|
public function middleware(string $className): MiddlewareInterface |
31
|
|
|
{ |
32
|
2 |
|
if ($this->container->has($className)) { |
33
|
1 |
|
return $this->container->get($className); |
34
|
|
|
} |
35
|
1 |
|
throw new HandlerNotFoundException($className); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
3 |
|
public function execute(string $className, string $methodName, ServerRequestInterface $request) |
42
|
|
|
{ |
43
|
3 |
|
if ($this->container->has($className)) { |
44
|
3 |
|
$object = $this->container->get($className); |
45
|
|
|
try { |
46
|
3 |
|
$reflMethod = (new ReflectionClass($className))->getMethod($methodName); |
47
|
1 |
|
} catch (ReflectionException $e) { |
48
|
1 |
|
$reflMethod = null; |
49
|
|
|
} |
50
|
3 |
|
if ($reflMethod) { |
51
|
2 |
|
$arguments = $this->getArguments($reflMethod, $request); |
52
|
2 |
|
return call_user_func_array([$object, $methodName], $arguments); |
53
|
|
|
} |
54
|
1 |
|
if (method_exists($object, '__call')) { |
55
|
1 |
|
return $object->__call($methodName, [$request]); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
throw new HandlerNotFoundException($className, $methodName); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
public function getArguments(\ReflectionFunctionAbstract $refl, ServerRequestInterface $request) |
62
|
|
|
{ |
63
|
2 |
|
$arguments = []; |
64
|
|
|
// it from container Resolver.. |
65
|
2 |
|
foreach ($refl->getParameters() as $param) { |
66
|
1 |
|
$paramClass = $param->getClass(); |
67
|
1 |
|
if ($paramClass) { // #2. |
68
|
1 |
|
if ($paramClass->isInstance($request)) { |
69
|
1 |
|
$arguments[] = $request; |
70
|
1 |
|
continue; |
71
|
|
|
} |
72
|
1 |
|
$paramClassName = $paramClass->getName(); |
|
|
|
|
73
|
1 |
|
if ($attribute = $request->getAttribute($paramClassName)) { |
74
|
1 |
|
$arguments[] = $attribute; |
75
|
1 |
|
continue; |
76
|
|
|
} |
77
|
1 |
|
if ($this->container->has($paramClassName)) { |
78
|
|
|
try { |
79
|
1 |
|
$arguments[] = $this->container->get($paramClassName); |
80
|
|
|
continue; |
81
|
1 |
|
} catch (NotFoundExceptionInterface $e) { |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
1 |
|
if ($param->isDefaultValueAvailable()) { |
86
|
|
|
$arguments[] = $param->getDefaultValue(); |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
1 |
|
if ($attribute = $request->getAttribute($param->getName())) { |
|
|
|
|
90
|
|
|
$arguments[] = $attribute; |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
1 |
|
throw new RuntimeException("not found parameter named \"{$param->getName()}\"."); |
|
|
|
|
94
|
|
|
} |
95
|
2 |
|
return $arguments; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|