Skip to content

Commit 2c7cfd7

Browse files
authored
Merge pull request #28 from keepsuit/cache-compiled-markdown
cache compiled markdown
2 parents c5c7817 + 90aa431 commit 2c7cfd7

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

src/CookieSolution.php

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Carbon\Carbon;
66
use DateTimeInterface;
7+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
78
use Illuminate\Support\Arr;
89
use Illuminate\Support\Collection;
910
use Illuminate\Support\Facades\File;
@@ -189,25 +190,43 @@ public function script(): HtmlString
189190

190191
protected function cookiePolicyText(string $locale): ?string
191192
{
192-
if (File::exists(resource_path(sprintf('views/vendor/cookie-solution/policy/cookie-policy.%s.md', $locale)))) {
193-
return Str::markdown(File::get(resource_path(sprintf('views/vendor/cookie-solution/policy/cookie-policy.%s.md', $locale))), $this->markdownConfig);
194-
}
193+
$paths = [
194+
resource_path(sprintf('views/vendor/cookie-solution/policy/cookie-policy.%s.md', $locale)),
195+
__DIR__.sprintf('/../resources/views/policy/cookie-policy.%s.md', $locale),
196+
];
195197

196-
if (File::exists(__DIR__.sprintf('/../resources/views/policy/cookie-policy.%s.md', $locale))) {
197-
return Str::markdown(File::get(__DIR__.sprintf('/../resources/views/policy/cookie-policy.%s.md', $locale)), $this->markdownConfig);
198+
foreach ($paths as $path) {
199+
if (! File::exists($path)) {
200+
continue;
201+
}
202+
203+
try {
204+
return $this->renderMarkdownFile($path);
205+
} catch (FileNotFoundException) {
206+
continue;
207+
}
198208
}
199209

200210
return null;
201211
}
202212

203213
protected function privacyPolicyText(string $locale): ?string
204214
{
205-
if (File::exists(resource_path(sprintf('views/vendor/cookie-solution/policy/privacy-policy.%s.md', $locale)))) {
206-
return Str::markdown(File::get(resource_path(sprintf('views/vendor/cookie-solution/policy/privacy-policy.%s.md', $locale))), $this->markdownConfig);
207-
}
215+
$paths = [
216+
resource_path(sprintf('views/vendor/cookie-solution/policy/privacy-policy.%s.md', $locale)),
217+
__DIR__.sprintf('/../resources/views/policy/privacy-policy.%s.md', $locale),
218+
];
208219

209-
if (File::exists(__DIR__.sprintf('/../resources/views/policy/privacy-policy.%s.md', $locale))) {
210-
return Str::markdown(File::get(__DIR__.sprintf('/../resources/views/policy/privacy-policy.%s.md', $locale)), $this->markdownConfig);
220+
foreach ($paths as $path) {
221+
if (! File::exists($path)) {
222+
continue;
223+
}
224+
225+
try {
226+
return $this->renderMarkdownFile($path);
227+
} catch (FileNotFoundException) {
228+
continue;
229+
}
211230
}
212231

213232
return null;
@@ -255,4 +274,23 @@ protected function configDigest(): string
255274

256275
return hash('sha256', $cookies);
257276
}
277+
278+
/**
279+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
280+
*/
281+
protected function renderMarkdownFile(string $sourcePath): string
282+
{
283+
$cachePath = storage_path(sprintf('framework/views/%s.html', hash('xxh128', $sourcePath)));
284+
285+
if (File::exists($cachePath) && File::lastModified($cachePath) >= File::lastModified($sourcePath)) {
286+
return File::get($cachePath);
287+
}
288+
289+
$content = Str::markdown(File::get($sourcePath));
290+
291+
File::ensureDirectoryExists(dirname($cachePath));
292+
File::put($cachePath, $content);
293+
294+
return $content;
295+
}
258296
}

0 commit comments

Comments
 (0)