Completed
Push — master ( 06cb54...79190c )
by Tomáš
08:10
created

DefaultCache::getClassFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Portiny\Doctrine\Cache;
5
6
use Doctrine\Common\Cache\CacheProvider;
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Nette\Caching\Cache;
9
use Nette\Caching\IStorage;
10
use Nette\Utils\Strings;
11
use ReflectionClass;
12
13
final class DefaultCache extends CacheProvider
14
{
15
	/**
16
	 * @var string
17
	 */
18
	public const CACHE_NS = 'Doctrine';
19
20
	/**
21
	 * @var Cache
22
	 */
23
	private $cache;
24
25
	/**
26
	 * @var bool
27
	 */
28
	private $debugMode = FALSE;
29
30 6
	public function __construct(IStorage $storage, string $namespace = self::CACHE_NS, bool $debugMode = FALSE)
31
	{
32 6
		$this->cache = new Cache($storage, $namespace);
33 6
		$this->debugMode = $debugMode;
34 6
	}
35
36
	/**
37
	 * {@inheritdoc}
38
	 */
39
	protected function doFetch($id)
40
	{
41
		$cached = $this->cache->load($id);
42
		return $cached !== NULL ? $cached : FALSE;
43
	}
44
45
	/**
46
	 * {@inheritdoc}
47
	 */
48
	protected function doContains($id)
49
	{
50
		return $this->cache->load($id) !== NULL;
51
	}
52
53
	/**
54
	 * @param string $id
55
	 * @param string|object $data
56
	 * @param int $lifeTime
57
	 * @return bool
58
	 */
59
	protected function doSave($id, $data, $lifeTime = 0)
60
	{
61
		if ($this->debugMode !== TRUE) {
62
			return $this->doSaveDependingOnFiles($id, $data, [], $lifeTime);
63
		}
64
65
		$files = [];
66
		if ($data instanceof ClassMetadata) {
67
			$files[] = self::getClassFilename($data->name);
68
			foreach ($data->parentClasses as $class) {
69
				$files[] = self::getClassFilename($class);
70
			}
71
		}
72
73
		if (! empty($data)) {
74
			$m = Strings::match($id, '~(?P<class>[^@$[\].]+)(?:\$(?P<prop>[^@$[\].]+))?\@\[Annot\]~i');
75
			if ($m !== NULL && class_exists($m['class'])) {
76
				$files[] = self::getClassFilename($m['class']);
77
			}
78
		}
79
80
		return $this->doSaveDependingOnFiles($id, $data, $files, $lifeTime);
81
	}
82
83
	/**
84
	 * @param string $id
85
	 * @param mixed $data
86
	 * @param int $lifeTime
87
	 * @return bool
88
	 */
89
	protected function doSaveDependingOnFiles($id, $data, array $files, $lifeTime = 0)
90
	{
91
		$dependencies = [
92
			Cache::TAGS => ['doctrine'],
93
			Cache::FILES => $files,
94
		];
95
		if ($lifeTime > 0) {
96
			$dependencies[Cache::EXPIRE] = time() + $lifeTime;
97
		}
98
99
		$this->cache->save($id, $data, $dependencies);
100
101
		return TRUE;
102
	}
103
104
	/**
105
	 * {@inheritdoc}
106
	 */
107
	protected function doDelete($id)
108
	{
109
		$this->cache->save($id, NULL);
110
111
		return TRUE;
112
	}
113
114
	/**
115
	 * {@inheritdoc}
116
	 */
117
	protected function doFlush()
118
	{
119
		$this->cache->clean([
120
			Cache::TAGS => ['doctrine'],
121
		]);
122
123
		return TRUE;
124
	}
125
126
	/**
127
	 * {@inheritdoc}
128
	 */
129
	protected function doGetStats()
130
	{
131
		return [
132
			self::STATS_HITS => NULL,
133
			self::STATS_MISSES => NULL,
134
			self::STATS_UPTIME => NULL,
135
			self::STATS_MEMORY_USAGE => NULL,
136
			self::STATS_MEMORY_AVAILABLE => NULL,
137
		];
138
	}
139
140
	private static function getClassFilename(string $className): string
141
	{
142
		$reflection = new ReflectionClass($className);
143
		return $reflection->getFileName();
144
	}
145
}
146