-
Notifications
You must be signed in to change notification settings - Fork 9
/
StatiCache.php
155 lines (127 loc) · 3.8 KB
/
StatiCache.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
148
149
150
151
152
153
154
155
<?php
namespace Kirby\Cache;
use Closure;
use Kirby\Cms\App;
use Kirby\Filesystem\F;
use Kirby\Filesystem\Mime;
use Kirby\Toolkit\Str;
/**
* An alternative implementation for the pages cache
* that caches full HTML files to be read directly
* by the web server.
*
* @package Kirby Staticache
* @author Bastian Allgeier <[email protected]>,
* Lukas Bestle <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class StatiCache extends FileCache
{
/**
* Internal method to retrieve the raw cache value;
* needs to return a Value object or null if not found
*/
public function retrieve(string $key): Value|null
{
$file = $this->file($key);
$value = F::read($file);
if (is_string($value) === true) {
return new Value($value, 0, filemtime($file));
}
return null;
}
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*/
public function set(string $key, $value, int $minutes = 0): bool
{
$cacheId = static::parseCacheId($key);
// body
$result = $this->appendComment($value['html'], $cacheId['contentType']);
// headers (if enabled)
if (
isset($this->options['headers']) === true &&
$this->options['headers'] === true
) {
$headers = static::headersFromResponse($value['response'], $cacheId['contentType']);
$result = $headers . "\n\n" . $result;
}
return F::write($this->file($cacheId), $result);
}
/**
* Appends a (HTML) comment to a cached body for
* identification of cached responses
*/
protected function appendComment(string $body, string $contentType): string
{
// custom string or callback
if (isset($this->options['comment']) === true) {
$comment = $this->options['comment'];
if ($comment instanceof Closure) {
return $body . $comment($contentType);
}
// use string comments for HTML bodies only
if (is_string($comment) === true && $contentType === 'html') {
return $body . $comment;
}
return $body;
}
// default implementation
if ($contentType === 'html') {
$body .= '<!-- static ' . date('c') . ' -->';
}
return $body;
}
/**
* Returns the full path to a file for a given key
*/
protected function file(string|array $key): string
{
$kirby = App::instance();
// compatibility with other cache drivers
if (is_string($key) === true) {
$key = static::parseCacheId($key);
}
$page = $kirby->page($key['id']);
$url = $page->url($key['language']);
// content representation paths of the home page contain the home slug
if ($page->isHomePage() === true && $key['contentType'] !== 'html') {
$url .= '/' . $page->uri($key['language']);
}
// we only care about the path
$root = $this->root . '/' . ltrim(Str::after($url, $kirby->url('index')), '/');
if ($key['contentType'] === 'html') {
return rtrim($root, '/') . '/index.html';
}
return $root . '.' . $key['contentType'];
}
/**
* Serializes all headers from a response array to a string of HTTP headers
*/
protected static function headersFromResponse(array $response, string $extension): string
{
$headers = [
'Status: ' . ($response['code'] ?? 200),
'Content-Type: ' . ($response['type'] ?? Mime::fromExtension($extension))
];
foreach ($response['headers'] as $key => $value) {
$headers[] = $key . ': ' . $value;
}
return implode("\n", $headers);
}
/**
* Splits a cache ID into `$id.$language.$contentType`
*/
protected static function parseCacheId(string $key): array
{
$kirby = App::instance();
$parts = explode('.', $key);
$contentType = array_pop($parts);
$language = $kirby->multilang() === true ? array_pop($parts) : null;
$id = implode('.', $parts);
return compact('id', 'language', 'contentType');
}
}