Issues (28)

src/Cache/DefaultCache.php (4 issues)

1
<?php declare(strict_types = 1);
2
3
namespace Portiny\Doctrine\Cache;
4
5
use Doctrine\Common\Cache\CacheProvider;
6
use Doctrine\ORM\Mapping\ClassMetadata;
7
use Nette\Caching\Cache;
8
use Nette\Caching\IStorage;
9
use Nette\Utils\Strings;
10
use ReflectionClass;
11
12
final class DefaultCache extends CacheProvider
13
{
14
	public const CACHE_NS = 'Doctrine';
15
16
	/**
17
	 * @var bool
18
	 */
19
	private $debugMode = false;
20
21
	/**
22
	 * @var Cache
23
	 */
24
	private $cache;
25
26
27 6
	public function __construct(IStorage $storage, string $namespace = self::CACHE_NS, bool $debugMode = false)
28
	{
29 6
		$this->cache = new Cache($storage, $namespace);
30 6
		$this->debugMode = $debugMode;
31 6
	}
32
33
34
	/**
35
	 * {@inheritdoc}
36
	 */
37
	protected function doFetch($id)
38
	{
39
		$cached = $this->cache->load($id);
40
		return $cached !== null ? $cached : false;
41
	}
42
43
44
	/**
45
	 * {@inheritdoc}
46
	 */
47
	protected function doContains($id)
48
	{
49
		return $this->cache->load($id) !== null;
50
	}
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
	/**
85
	 * @param string $id
86
	 * @param mixed $data
87
	 * @param int $lifeTime
88
	 * @return bool
89
	 */
90
	protected function doSaveDependingOnFiles($id, $data, array $files, $lifeTime = 0)
91
	{
92
		$dependencies = [
93
			Cache::TAGS => ['doctrine'],
0 ignored issues
show
Deprecated Code introduced by
The constant Nette\Caching\Cache::TAGS has been deprecated: use Cache::Tags ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

93
			/** @scrutinizer ignore-deprecated */ Cache::TAGS => ['doctrine'],

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
94
			Cache::FILES => $files,
0 ignored issues
show
Deprecated Code introduced by
The constant Nette\Caching\Cache::FILES has been deprecated: use Cache::Files ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

94
			/** @scrutinizer ignore-deprecated */ Cache::FILES => $files,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
95
		];
96
		if ($lifeTime > 0) {
97
			$dependencies[Cache::EXPIRE] = time() + $lifeTime;
0 ignored issues
show
Deprecated Code introduced by
The constant Nette\Caching\Cache::EXPIRE has been deprecated: use Cache::Expire ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

97
			$dependencies[/** @scrutinizer ignore-deprecated */ Cache::EXPIRE] = time() + $lifeTime;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
98
		}
99
100
		$this->cache->save($id, $data, $dependencies);
101
102
		return true;
103
	}
104
105
106
	/**
107
	 * {@inheritdoc}
108
	 */
109
	protected function doDelete($id)
110
	{
111
		$this->cache->save($id, null);
112
113
		return true;
114
	}
115
116
117
	/**
118
	 * {@inheritdoc}
119
	 */
120
	protected function doFlush()
121
	{
122
		$this->cache->clean([
123
			Cache::TAGS => ['doctrine'],
0 ignored issues
show
Deprecated Code introduced by
The constant Nette\Caching\Cache::TAGS has been deprecated: use Cache::Tags ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

123
			/** @scrutinizer ignore-deprecated */ Cache::TAGS => ['doctrine'],

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
124
		]);
125
126
		return true;
127
	}
128
129
130
	/**
131
	 * {@inheritdoc}
132
	 */
133
	protected function doGetStats()
134
	{
135
		return [
136
			self::STATS_HITS => null,
137
			self::STATS_MISSES => null,
138
			self::STATS_UPTIME => null,
139
			self::STATS_MEMORY_USAGE => null,
140
			self::STATS_MEMORY_AVAILABLE => null,
141
		];
142
	}
143
144
145
	/**
146
	 * @param mixed $className
147
	 */
148
	private static function getClassFilename($className): string
149
	{
150
		$reflection = new ReflectionClass($className);
151
		return (string) $reflection->getFileName();
152
	}
153
154
}
155