-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.php
executable file
·147 lines (119 loc) · 3.75 KB
/
Cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Codememory\Components\Big;
use Codememory\Components\Caching\Interfaces\CacheInterface;
use Codememory\FileSystem\Interfaces\FileInterface;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Pure;
/**
* Class Cache
* @package Codememory\Components\Big
*
* @author Codememory
*/
class Cache
{
public const TEMPLATE_CACHE_NAME_PREFIX = '__cdm-engine-big=%s';
public const TYPE_CACHE = 'template';
/**
* @var CacheInterface
*/
private CacheInterface $cache;
/**
* @var string|null
*/
private ?string $templateText;
/**
* Cache constructor.
*
* @param CacheInterface $cache
*/
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}
/**
* =>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>
* Set the compiled template text to be added to the cache
* <=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=
*
* @param string|null $text
*
* @return $this
*/
public function setTemplateText(?string $text): Cache
{
$this->templateText = $text;
return $this;
}
/**
* =>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>
* Returns the full name of the template cache
* <=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=
*
* @param string $templateName
*
* @return string
*/
public function getFullNameTemplateCache(string $templateName): string
{
return sprintf(self::TEMPLATE_CACHE_NAME_PREFIX, $templateName);
}
/**
* =>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>
* If the cache of the open template does not exist, it will be created,
* otherwise it will simply be overwritten, an additional cache file with
* the php extension is also created
* <=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=
*
* @param string $templateName
*
* @return Cache
*/
public function save(string $templateName): Cache
{
$this->cache->create(
self::TYPE_CACHE,
$this->getFullNameTemplateCache($templateName),
$this->templateText,
function (FileInterface $filesystem, string $fullPath, mixed $data, array &$history) use ($templateName) {
$phpTemplateFilename = $fullPath . '.php';
$filesystem->writer->open($phpTemplateFilename, 'r', true)->put($data);
$history = $this->additionalHistoryData($templateName);
}
);
return $this;
}
/**
* =>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>
* Get a template from the cache by name
* <=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=
*
* @param string $templateName
*
* @return string|null
*/
public function getTemplateFromCache(string $templateName): ?string
{
return $this->cache->get(self::TYPE_CACHE, $this->getFullNameTemplateCache($templateName)) ?: null;
}
/**
* =>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>
* Returns an additional array of data that will be written to
* the cache history for the generated template caches
* <=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=<=
*
* @param string $templateName
*
* @return array
*/
#[Pure]
#[ArrayShape(['cacheName' => "string", 'name' => "string", 'otherExtensions' => "string[]"])]
private function additionalHistoryData(string $templateName): array
{
return [
'cacheName' => $this->getFullNameTemplateCache($templateName),
'name' => $templateName,
'otherExtensions' => ['php']
];
}
}