|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the API Platform project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Routing; |
|
15
|
|
|
|
|
16
|
|
|
use Psr\Cache\CacheException; |
|
17
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
* |
|
22
|
|
|
* @author Teoh Han Hui <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
final class CachedRouteNameResolver implements RouteNameResolverInterface |
|
25
|
|
|
{ |
|
26
|
|
|
const CACHE_KEY_PREFIX = 'route_name_'; |
|
27
|
|
|
|
|
28
|
|
|
private $cacheItemPool; |
|
29
|
|
|
private $decorated; |
|
30
|
|
|
private $localCache = []; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(CacheItemPoolInterface $cacheItemPool, RouteNameResolverInterface $decorated) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->cacheItemPool = $cacheItemPool; |
|
35
|
|
|
$this->decorated = $decorated; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getRouteName(string $resourceClass, $operationType /**, array $context = []**/): string |
|
42
|
|
|
{ |
|
43
|
|
|
$context = \func_num_args() > 2 ? func_get_arg(2) : []; |
|
44
|
|
|
$cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $operationType, $context['subresource_resources'] ?? null])); |
|
45
|
|
|
|
|
46
|
|
|
if (isset($this->localCache[$cacheKey])) { |
|
47
|
|
|
return $this->localCache[$cacheKey]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$cacheItem = $this->cacheItemPool->getItem($cacheKey); |
|
52
|
|
|
|
|
53
|
|
|
if ($cacheItem->isHit()) { |
|
54
|
|
|
return $this->localCache[$cacheKey] = $cacheItem->get(); |
|
55
|
|
|
} |
|
56
|
|
|
} catch (CacheException $e) { |
|
57
|
|
|
//do nothing |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$routeName = $this->decorated->getRouteName($resourceClass, $operationType, $context); |
|
61
|
|
|
|
|
62
|
|
|
if (!isset($cacheItem)) { |
|
63
|
|
|
return $this->localCache[$cacheKey] = $routeName; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
try { |
|
67
|
|
|
$cacheItem->set($routeName); |
|
68
|
|
|
$this->cacheItemPool->save($cacheItem); |
|
69
|
|
|
} catch (CacheException $e) { |
|
70
|
|
|
// do nothing |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->localCache[$cacheKey] = $routeName; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|