|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\RequestModel; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Yiisoft\Middleware\Dispatcher\ParametersResolverInterface; |
|
12
|
|
|
use Yiisoft\RequestModel\Attribute\HandlerParameterAttributeInterface; |
|
13
|
|
|
use Yiisoft\RequestModel\Attribute\HandlerParameterResolverInterface; |
|
14
|
|
|
use Yiisoft\RequestModel\Attribute\ValueNotFoundException; |
|
15
|
|
|
|
|
16
|
|
|
final class HandlerParametersResolver implements ParametersResolverInterface |
|
17
|
|
|
{ |
|
18
|
7 |
|
public function __construct(private RequestModelFactory $factory, private ContainerInterface $container) |
|
19
|
|
|
{ |
|
20
|
7 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritDoc} |
|
24
|
|
|
* |
|
25
|
|
|
* @throws ContainerExceptionInterface |
|
26
|
|
|
* @throws NotFoundExceptionInterface |
|
27
|
|
|
* @throws \ReflectionException |
|
28
|
|
|
*/ |
|
29
|
7 |
|
public function resolve(array $parameters, ServerRequestInterface $request): array |
|
30
|
|
|
{ |
|
31
|
7 |
|
return array_merge( |
|
32
|
7 |
|
$this->getAttributeParams($parameters, $request), |
|
33
|
7 |
|
$this->factory->createInstances($request, $parameters) |
|
34
|
7 |
|
); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param \ReflectionParameter[] $parameters |
|
39
|
|
|
* |
|
40
|
|
|
* @throws ContainerExceptionInterface |
|
41
|
|
|
* @throws NotFoundExceptionInterface |
|
42
|
|
|
*/ |
|
43
|
7 |
|
private function getAttributeParams(array $parameters, ServerRequestInterface $request): array |
|
44
|
|
|
{ |
|
45
|
7 |
|
$actionParameters = []; |
|
46
|
7 |
|
foreach ($parameters as $parameter) { |
|
47
|
7 |
|
$attributes = $parameter->getAttributes( |
|
48
|
7 |
|
HandlerParameterAttributeInterface::class, |
|
49
|
7 |
|
\ReflectionAttribute::IS_INSTANCEOF |
|
50
|
7 |
|
); |
|
51
|
7 |
|
foreach ($attributes as $attribute) { |
|
52
|
|
|
/** @var HandlerParameterAttributeInterface $attributeInstance */ |
|
53
|
5 |
|
$attributeInstance = $attribute->newInstance(); |
|
54
|
5 |
|
$resolver = $this->container->get($attributeInstance->getResolverClassName()); |
|
55
|
5 |
|
if (!($resolver instanceof HandlerParameterResolverInterface)) { |
|
56
|
2 |
|
throw new \RuntimeException( |
|
57
|
2 |
|
sprintf( |
|
58
|
2 |
|
'Resolver "%s" should implement %s.', |
|
59
|
2 |
|
$resolver::class, |
|
60
|
2 |
|
HandlerParameterResolverInterface::class |
|
61
|
2 |
|
) |
|
62
|
2 |
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
try { |
|
66
|
|
|
/** @var mixed */ |
|
67
|
3 |
|
$actionParameters[$parameter->getName()] = $resolver->resolve($attributeInstance, $request); |
|
68
|
|
|
} catch (ValueNotFoundException) { |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
5 |
|
return $actionParameters; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|