|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of php-cache\cache-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Cache\CacheBundle\Routing; |
|
13
|
|
|
|
|
14
|
|
|
use Cache\CacheBundle\KeyNormalizer; |
|
15
|
|
|
use Cache\TagInterop\TaggableCacheItemInterface; |
|
16
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
17
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
18
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class CachingRouter implements RouterInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @type CacheItemPoolInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $cache; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @type int |
|
32
|
|
|
*/ |
|
33
|
|
|
private $ttl; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @type RouterInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $router; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param CacheItemPoolInterface $cache |
|
42
|
|
|
* @param RouterInterface $router |
|
43
|
|
|
* @param array $config |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(CacheItemPoolInterface $cache, RouterInterface $router, array $config) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->cache = $cache; |
|
48
|
|
|
$this->ttl = $config['ttl']; |
|
49
|
|
|
$this->router = $router; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function match($pathinfo) |
|
56
|
|
|
{ |
|
57
|
|
|
$cacheItem = $this->getCacheItemMatch($pathinfo); |
|
58
|
|
|
if ($cacheItem->isHit()) { |
|
59
|
|
|
return $cacheItem->get(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Get the result form the router |
|
63
|
|
|
$result = $this->router->match($pathinfo); |
|
64
|
|
|
|
|
65
|
|
|
// Save the result |
|
66
|
|
|
$cacheItem->set($result) |
|
67
|
|
|
->expiresAfter($this->ttl); |
|
68
|
|
|
$this->cache->save($cacheItem); |
|
69
|
|
|
|
|
70
|
|
|
return $result; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH) |
|
77
|
|
|
{ |
|
78
|
|
|
$cacheItem = $this->getCacheItemGenerate($name, $parameters, $referenceType); |
|
79
|
|
|
if ($cacheItem->isHit()) { |
|
80
|
|
|
return $cacheItem->get(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Get the result form the router |
|
84
|
|
|
$result = $this->router->generate($name, $parameters, $referenceType); |
|
85
|
|
|
|
|
86
|
|
|
// Save the result |
|
87
|
|
|
$cacheItem->set($result) |
|
88
|
|
|
->expiresAfter($this->ttl); |
|
89
|
|
|
$this->cache->save($cacheItem); |
|
90
|
|
|
|
|
91
|
|
|
return $result; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getRouteCollection() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->router->getRouteCollection(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setContext(RequestContext $context) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->router->setContext($context); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getContext() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->router->getContext(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get a cache item for a call to match(). |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $pathinfo |
|
122
|
|
|
* |
|
123
|
|
|
* @return \Psr\Cache\CacheItemInterface |
|
124
|
|
|
*/ |
|
125
|
|
|
private function getCacheItemMatch($pathinfo) |
|
126
|
|
|
{ |
|
127
|
|
|
/** @type RequestContext $c */ |
|
128
|
|
|
$c = $this->getContext(); |
|
129
|
|
|
$key = sprintf('%s__%s__%s__%s', $c->getHost(), $pathinfo, $c->getMethod(), $c->getQueryString()); |
|
130
|
|
|
|
|
131
|
|
|
return $this->getCacheItemFromKey($key, 'match'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get a cache item for a call to generate(). |
|
136
|
|
|
* |
|
137
|
|
|
* @param $name |
|
138
|
|
|
* @param array $parameters |
|
139
|
|
|
* @param $referenceType |
|
140
|
|
|
* |
|
141
|
|
|
* @return \Psr\Cache\CacheItemInterface |
|
142
|
|
|
*/ |
|
143
|
|
|
private function getCacheItemGenerate($name, array $parameters, $referenceType) |
|
144
|
|
|
{ |
|
145
|
|
|
asort($parameters); |
|
146
|
|
|
$key = sprintf('%s.%s.%s', $name, $referenceType ? 'true' : 'false', http_build_query($parameters)); |
|
147
|
|
|
|
|
148
|
|
|
return $this->getCacheItemFromKey($key, 'generate'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Passes through all unknown calls onto the router object. |
|
153
|
|
|
*/ |
|
154
|
|
|
public function __call($method, $args) |
|
155
|
|
|
{ |
|
156
|
|
|
return call_user_func_array([$this->router, $method], $args); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string $key |
|
161
|
|
|
* @param string $tag |
|
162
|
|
|
* |
|
163
|
|
|
* @return \Psr\Cache\CacheItemInterface |
|
164
|
|
|
*/ |
|
165
|
|
|
private function getCacheItemFromKey($key, $tag) |
|
166
|
|
|
{ |
|
167
|
|
|
$item = $this->cache->getItem(KeyNormalizer::noInvalid($key)); |
|
168
|
|
|
|
|
169
|
|
|
if ($item instanceof TaggableCacheItemInterface) { |
|
170
|
|
|
$item->setTags(['router', $tag]); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $item; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|