-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgumentResolver.php
More file actions
127 lines (104 loc) · 4.36 KB
/
ArgumentResolver.php
File metadata and controls
127 lines (104 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
*
* (c) Yaroslav Honcharuk <yaroslav.xs@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Yarhon\RouteGuardBundle\Controller;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Yarhon\RouteGuardBundle\Cache\CacheFactory;
use Yarhon\RouteGuardBundle\Routing\RequestAttributesFactoryInterface;
use Yarhon\RouteGuardBundle\Routing\RouteContextInterface;
use Yarhon\RouteGuardBundle\Controller\ArgumentResolver\ArgumentResolverContext;
use Yarhon\RouteGuardBundle\Controller\ArgumentResolver\ArgumentValueResolverInterface;
use Yarhon\RouteGuardBundle\Exception\RuntimeException;
/**
* Responsible for resolving the argument passed to an action.
*
* @author Yaroslav Honcharuk <yaroslav.xs@gmail.com>
*/
class ArgumentResolver implements ArgumentResolverInterface
{
/**
* @var CacheItemPoolInterface
*/
private $controllerMetadataCache;
/**
* @var RequestAttributesFactoryInterface
*/
private $requestAttributesFactory;
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var \Traversable|ArgumentValueResolverInterface[]
*/
private $argumentValueResolvers;
/**
* @var array
*/
private $internalCache = [];
/**
* @param CacheItemPoolInterface $controllerMetadataCache
* @param RequestAttributesFactoryInterface $requestAttributesFactory
* @param RequestStack $requestStack
* @param \Traversable|ArgumentValueResolverInterface[] $argumentValueResolvers
*/
public function __construct(CacheItemPoolInterface $controllerMetadataCache, RequestAttributesFactoryInterface $requestAttributesFactory, RequestStack $requestStack, $argumentValueResolvers = [])
{
$this->controllerMetadataCache = $controllerMetadataCache;
$this->requestAttributesFactory = $requestAttributesFactory;
$this->requestStack = $requestStack;
$this->argumentValueResolvers = $argumentValueResolvers;
}
/**
* {@inheritdoc}
*/
public function getArgument(RouteContextInterface $routeContext, $name)
{
$cacheKey = spl_object_hash($routeContext).'#'.$name;
if (array_key_exists($cacheKey, $this->internalCache)) {
return $this->internalCache[$cacheKey];
}
$controllerMetadata = $this->getControllerMetadata($routeContext->getName());
if (null === $controllerMetadata) {
$message = 'Route "%s" does not have controller or controller name is unresolvable.';
throw new RuntimeException(sprintf($message, $routeContext->getName()));
}
if (!$controllerMetadata->hasArgument($name)) {
$message = 'Route "%s" controller "%s" does not have argument "$%s".';
throw new RuntimeException(sprintf($message, $routeContext->getName(), $controllerMetadata->getName(), $name));
}
$requestAttributes = $this->requestAttributesFactory->createAttributes($routeContext);
$resolverContext = new ArgumentResolverContext($requestAttributes, $controllerMetadata->getName(), $this->requestStack->getCurrentRequest());
$argumentMetadata = $controllerMetadata->getArgument($name);
foreach ($this->argumentValueResolvers as $resolver) {
if (!$resolver->supports($resolverContext, $argumentMetadata)) {
continue;
}
return $this->internalCache[$cacheKey] = $resolver->resolve($resolverContext, $argumentMetadata);
}
$message = 'Route "%s" controller "%s" requires that you provide a value for the "$%s" argument.';
throw new RuntimeException(sprintf($message, $routeContext->getName(), $controllerMetadata->getName(), $name));
}
/**
* @param string $routeName
*
* @return ControllerMetadata
*
* @throws RuntimeException
*/
private function getControllerMetadata($routeName)
{
$cacheKey = CacheFactory::getValidCacheKey($routeName);
$cacheItem = $this->controllerMetadataCache->getItem($cacheKey);
if (!$cacheItem->isHit()) {
throw new RuntimeException(sprintf('Cannot get ControllerMetadata for route "%s".', $routeName));
}
return $cacheItem->get();
}
}