1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WyriHaximus\TwigView\Lib; |
5
|
|
|
|
6
|
|
|
use Asm89\Twig\CacheExtension\CacheProviderInterface; |
7
|
|
|
use Cake\Cache\Cache as CakeCache; |
8
|
|
|
|
9
|
|
|
final class Cache implements CacheProviderInterface |
10
|
|
|
{ |
11
|
|
|
public const CACHE_PREFIX = 'twig-view-in-template-item-'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Retrieve data from the cache. |
15
|
|
|
* |
16
|
|
|
* @param string $identifier Identifier for this bit of data to read. |
17
|
|
|
* |
18
|
|
|
* @return mixed The cached data, or false if the data doesn't exist, has expired, or on error while fetching. |
19
|
|
|
*/ |
20
|
|
|
public function fetch($identifier) |
21
|
|
|
{ |
22
|
|
|
[$config, $key] = $this->configSplit($identifier); |
|
|
|
|
23
|
|
|
|
24
|
|
|
return CakeCache::read(static::CACHE_PREFIX . $key, $config); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Save data to the cache. |
29
|
|
|
* |
30
|
|
|
* @param string $identifier Identifier for this bit of data to write. |
31
|
|
|
* @param string $data Data to cache. |
32
|
|
|
* @param int $lifeTime Time to life inside the cache. |
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
public function save($identifier, $data, $lifeTime = 0): bool |
37
|
|
|
{ |
38
|
|
|
[$config, $key] = $this->configSplit($identifier); |
|
|
|
|
39
|
|
|
|
40
|
|
|
return CakeCache::write(static::CACHE_PREFIX . $key, $data, $config); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Extract $configName and $key from $name and $config. |
45
|
|
|
* |
46
|
|
|
* @param string $name Name. |
47
|
|
|
* @param string $config Cache configuration name to used. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
protected function configSplit($name, $config = 'default'): array |
52
|
|
|
{ |
53
|
|
|
if (strpos($name, ':') !== false) { |
54
|
|
|
$parts = explode(':', $name, 2); |
55
|
|
|
|
56
|
|
|
return $parts; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return [$config, $name]; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.