1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the API Platform project. |
7
|
|
|
* |
8
|
|
|
* (c) Kévin Dunglas <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Api; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Util\ClassInfoTrait; |
17
|
|
|
use Psr\Cache\CacheException; |
18
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
19
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
20
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
* |
25
|
|
|
* @author Antoine Bluchet <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class CachedIdentifiersExtractor implements IdentifiersExtractorInterface |
28
|
|
|
{ |
29
|
|
|
use ClassInfoTrait; |
30
|
|
|
|
31
|
|
|
const CACHE_KEY_PREFIX = 'iri_identifiers'; |
32
|
|
|
|
33
|
|
|
private $cacheItemPool; |
34
|
|
|
private $propertyAccessor; |
35
|
|
|
private $decorated; |
36
|
|
|
|
37
|
|
|
public function __construct(CacheItemPoolInterface $cacheItemPool, IdentifiersExtractorInterface $decorated, PropertyAccessorInterface $propertyAccessor = null) |
38
|
|
|
{ |
39
|
|
|
$this->cacheItemPool = $cacheItemPool; |
40
|
|
|
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor(); |
41
|
|
|
$this->decorated = $decorated; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function getIdentifiersFromItem($item): array |
48
|
|
|
{ |
49
|
|
|
$identifiers = []; |
50
|
|
|
$resourceClass = $this->getObjectClass($item); |
51
|
|
|
|
52
|
|
|
$cacheKey = self::CACHE_KEY_PREFIX.md5($resourceClass); |
53
|
|
|
|
54
|
|
|
// This is to avoid setting the cache twice in the case where the related item cache doesn't exist |
55
|
|
|
$cacheIsHit = false; |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
$cacheItem = $this->cacheItemPool->getItem($cacheKey); |
59
|
|
|
$isRelationCached = true; |
60
|
|
|
|
61
|
|
|
if ($cacheIsHit = $cacheItem->isHit()) { |
62
|
|
|
foreach ($cacheItem->get() as $propertyName) { |
63
|
|
|
$identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName); |
64
|
|
|
|
65
|
|
|
if (!is_object($identifiers[$propertyName])) { |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$relatedItem = $identifiers[$propertyName]; |
70
|
|
|
$relatedCacheKey = self::CACHE_KEY_PREFIX.md5($this->getObjectClass($relatedItem)); |
71
|
|
|
|
72
|
|
|
$relatedCacheItem = $this->cacheItemPool->getItem($relatedCacheKey); |
73
|
|
|
|
74
|
|
|
if (!$relatedCacheItem->isHit()) { |
75
|
|
|
$isRelationCached = false; |
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
unset($identifiers[$propertyName]); |
80
|
|
|
|
81
|
|
|
$identifiers[$propertyName] = $this->propertyAccessor->getValue($relatedItem, $relatedCacheItem->get()[0]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (true === $isRelationCached) { |
85
|
|
|
return $identifiers; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} catch (CacheException $e) { |
89
|
|
|
// do nothing |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$identifiers = $this->decorated->getIdentifiersFromItem($item); |
93
|
|
|
|
94
|
|
|
if (isset($cacheItem) && false === $cacheIsHit) { |
95
|
|
|
try { |
96
|
|
|
$cacheItem->set(array_keys($identifiers)); |
97
|
|
|
$this->cacheItemPool->save($cacheItem); |
98
|
|
|
} catch (CacheException $e) { |
99
|
|
|
// do nothing |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $identifiers; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|