1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks\RDMBundle\Mapping\Drivers; |
12
|
|
|
|
13
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
14
|
|
|
use Psr\Cache\CacheItemInterface; |
15
|
|
|
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface; |
16
|
|
|
use Addiks\RDMBundle\Mapping\EntityMappingInterface; |
17
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
18
|
|
|
|
19
|
|
|
final class CachedMappingDriver implements MappingDriverInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
const CACHE_KEY_FORMAT = "addiks_rdm_mapping__%s"; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var MappingDriverInterface |
26
|
|
|
*/ |
27
|
|
|
private $innerMappingDriver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CacheItemPoolInterface |
31
|
|
|
*/ |
32
|
|
|
private $cacheItemPool; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array<string, MappingInterface> |
36
|
|
|
*/ |
37
|
|
|
private $internalCachedMappings = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
private $internalCachedMappingLimit; |
43
|
|
|
|
44
|
9 |
|
public function __construct( |
45
|
|
|
MappingDriverInterface $innerMappingDriver, |
46
|
|
|
CacheItemPoolInterface $cacheItemPool, |
47
|
|
|
int $internalCachedMappingLimit = 100 |
48
|
|
|
) { |
49
|
9 |
|
$this->innerMappingDriver = $innerMappingDriver; |
50
|
9 |
|
$this->cacheItemPool = $cacheItemPool; |
51
|
9 |
|
$this->internalCachedMappingLimit = $internalCachedMappingLimit; |
52
|
9 |
|
} |
53
|
|
|
|
54
|
8 |
|
public function loadRDMMetadataForClass(string $className): ?EntityMappingInterface |
55
|
|
|
{ |
56
|
8 |
|
if (!array_key_exists($className, $this->internalCachedMappings)) { |
57
|
|
|
/** @var ?EntityMappingInterface $mapping */ |
|
|
|
|
58
|
8 |
|
$mapping = null; |
|
|
|
|
59
|
|
|
|
60
|
|
|
/** @var CacheItemInterface $cacheItem */ |
61
|
8 |
|
$cacheItem = $this->cacheItemPool->getItem(sprintf( |
62
|
8 |
|
self::CACHE_KEY_FORMAT, |
63
|
8 |
|
preg_replace("/[^a-zA-Z0-9]/is", "_", $className) |
64
|
|
|
)); |
65
|
|
|
|
66
|
8 |
|
if ($cacheItem->isHit()) { |
67
|
1 |
|
$mapping = unserialize($cacheItem->get()); |
68
|
|
|
|
69
|
|
|
} else { |
70
|
7 |
|
$mapping = $this->innerMappingDriver->loadRDMMetadataForClass($className); |
71
|
|
|
|
72
|
7 |
|
$cacheItem->set(serialize($mapping)); |
73
|
7 |
|
$this->cacheItemPool->saveDeferred($cacheItem); |
74
|
|
|
} |
75
|
|
|
|
76
|
8 |
|
$this->internalCachedMappings[$className] = $mapping; |
77
|
|
|
|
78
|
8 |
|
if (count($this->internalCachedMappings) > $this->internalCachedMappingLimit) { |
79
|
1 |
|
array_shift($this->internalCachedMappings); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
8 |
|
return $this->internalCachedMappings[$className]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.