-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestAttributesFactory.php
More file actions
126 lines (101 loc) · 3.81 KB
/
RequestAttributesFactory.php
File metadata and controls
126 lines (101 loc) · 3.81 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
<?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\Routing;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\Routing\RequestContext;
use Yarhon\RouteGuardBundle\Cache\CacheFactory;
use Yarhon\RouteGuardBundle\Exception\RuntimeException;
/**
* @author Yaroslav Honcharuk <yaroslav.xs@gmail.com>
*/
class RequestAttributesFactory implements RequestAttributesFactoryInterface
{
/**
* @var CacheItemPoolInterface
*/
private $routeMetadataCache;
/**
* @var RequestContext
*/
private $generatorContext;
/**
* @var array
*/
private $internalCache = [];
/**
* @param CacheItemPoolInterface $routeMetadataCache
* @param UrlGeneratorInterface $urlGenerator
*/
public function __construct(CacheItemPoolInterface $routeMetadataCache, UrlGeneratorInterface $urlGenerator)
{
$this->routeMetadataCache = $routeMetadataCache;
$this->generatorContext = $urlGenerator->getContext();
}
/**
* @see \Symfony\Component\Routing\Matcher\UrlMatcher::getAttributes
* @see \Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest
*
* {@inheritdoc}
*/
public function createAttributes(RouteContextInterface $routeContext)
{
$cacheKey = spl_object_hash($routeContext);
if (isset($this->internalCache[$cacheKey])) {
return $this->internalCache[$cacheKey];
}
$routeMetadata = $this->getRouteMetadata($routeContext->getName());
$parameters = $routeContext->getParameters();
$defaults = $routeMetadata->getDefaults();
// Special default parameters returned (if present): _format, _fragment, _locale
// See \Symfony\Component\Routing\Matcher\UrlMatcher::mergeDefaults
foreach ($parameters as $key => $value) {
if (is_int($key) || null === $value) {
unset($parameters[$key]);
}
}
$variables = array_flip($routeMetadata->getVariables());
$parameters = array_replace($this->generatorContext->getParameters(), $parameters);
// We should add only parameters being used as route variables, others wouldn't be presented in generated url,
// and therefore wouldn't be returned by the UrlMatcher as request attributes.
$parameters = array_intersect_key($parameters, $variables);
$attributes = array_replace($defaults, $parameters);
if ($diff = array_diff_key($variables, $attributes)) {
$missing = implode('", "', array_keys($diff));
throw new RuntimeException(sprintf('Some mandatory parameters are missing ("%s") to get attributes for route.', $missing));
}
return $this->internalCache[$cacheKey] = new ParameterBag($attributes);
}
/**
* {@inheritdoc}
*/
public function getAttributeNames(RouteMetadata $routeMetadata)
{
$names = array_merge($routeMetadata->getVariables(), array_keys($routeMetadata->getDefaults()));
$names = array_unique($names);
return $names;
}
/**
* @param string $routeName
*
* @return RouteMetadata
*
* @throws RuntimeException
*/
private function getRouteMetadata($routeName)
{
$cacheKey = CacheFactory::getValidCacheKey($routeName);
$cacheItem = $this->routeMetadataCache->getItem($cacheKey);
if (!$cacheItem->isHit()) {
throw new RuntimeException(sprintf('Cannot get RouteMetadata for route "%s".', $routeName));
}
return $cacheItem->get();
}
}