|
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
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Routing; |
|
13
|
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
|
15
|
|
|
use ApiPlatform\Core\Api\UrlGeneratorInterface; |
|
16
|
|
|
use ApiPlatform\Core\Exception\CacheException; |
|
17
|
|
|
use ApiPlatform\Core\Util\ClassInfoTrait; |
|
18
|
|
|
use Psr\Cache\CacheException as CacheExceptionInterface; |
|
19
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
* |
|
24
|
|
|
* @author Antoine Bluchet <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
final class CachedIriConverter implements IriConverterInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use ClassInfoTrait; |
|
29
|
|
|
|
|
30
|
|
|
const CACHE_KEY_PREFIX = 'iri_converter'; |
|
31
|
|
|
|
|
32
|
|
|
private $cacheItemPool; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(CacheItemPoolInterface $cacheItemPool, IriConverterInterface $decorated) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$this->cacheItemPool = $cacheItemPool; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getItemFromIri(string $iri, array $context = []) |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->decorated->getItemFromIri($iri, $context); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
|
45
|
|
|
{ |
|
46
|
|
|
$resourceClass = $this->getObjectClass($item); |
|
47
|
|
|
$cacheKey = self::CACHE_KEY_PREFIX.md5($resourceClass); |
|
48
|
|
|
|
|
49
|
|
|
try { |
|
50
|
|
|
$cacheItem = $this->cacheItemPool->getItem($cacheKey); |
|
51
|
|
|
|
|
52
|
|
|
if ($cacheItem->isHit()) { |
|
53
|
|
|
foreach ($cacheItem->get() as $propertyName) { |
|
54
|
|
|
$identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
if (is_object($identifiers[$propertyName])) { |
|
57
|
|
|
$relatedCacheKey = self::CACHE_KEY_PREFIX.md5(serialize($this->getObjectClass($identifiers[$propertyName]))); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$cacheItem = $this->cacheItemPool->getItem($relatedCacheKey); |
|
60
|
|
|
|
|
61
|
|
|
if (!$cacheItem->isHit()) { |
|
62
|
|
|
throw new CacheException("Can't find related identifiers"); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$identifiers[$propertyName] = $cacheItem->get()[0]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $identifiers; |
|
70
|
|
|
} |
|
71
|
|
|
} catch (CacheExceptionInterface $e) { |
|
72
|
|
|
// do nothing |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$identifiers = $this->decorated->getIriFromItem(); |
|
76
|
|
|
|
|
77
|
|
|
if (isset($cacheItem)) { |
|
78
|
|
|
try { |
|
79
|
|
|
$cacheItem->set(array_keys($identifiers)); |
|
80
|
|
|
$this->cacheItemPool->save($cacheItem); |
|
81
|
|
|
} catch (CacheExceptionInterface $e) { |
|
82
|
|
|
// do nothing |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $identifiers; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->decorated->getIriFromResourceClass($resourceClass, $referenceType); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.