From 90aa431e39c42a4ee1673e61792145d0de3de6bc Mon Sep 17 00:00:00 2001 From: Fabio Capucci Date: Wed, 6 Aug 2025 16:31:11 +0200 Subject: [PATCH] cache compiled markdown --- src/CookieSolution.php | 58 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/src/CookieSolution.php b/src/CookieSolution.php index 2a0ce95..c2c26bd 100755 --- a/src/CookieSolution.php +++ b/src/CookieSolution.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use DateTimeInterface; +use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; @@ -189,12 +190,21 @@ public function script(): HtmlString protected function cookiePolicyText(string $locale): ?string { - if (File::exists(resource_path(sprintf('views/vendor/cookie-solution/policy/cookie-policy.%s.md', $locale)))) { - return Str::markdown(File::get(resource_path(sprintf('views/vendor/cookie-solution/policy/cookie-policy.%s.md', $locale))), $this->markdownConfig); - } + $paths = [ + resource_path(sprintf('views/vendor/cookie-solution/policy/cookie-policy.%s.md', $locale)), + __DIR__.sprintf('/../resources/views/policy/cookie-policy.%s.md', $locale), + ]; - if (File::exists(__DIR__.sprintf('/../resources/views/policy/cookie-policy.%s.md', $locale))) { - return Str::markdown(File::get(__DIR__.sprintf('/../resources/views/policy/cookie-policy.%s.md', $locale)), $this->markdownConfig); + foreach ($paths as $path) { + if (! File::exists($path)) { + continue; + } + + try { + return $this->renderMarkdownFile($path); + } catch (FileNotFoundException) { + continue; + } } return null; @@ -202,12 +212,21 @@ protected function cookiePolicyText(string $locale): ?string protected function privacyPolicyText(string $locale): ?string { - if (File::exists(resource_path(sprintf('views/vendor/cookie-solution/policy/privacy-policy.%s.md', $locale)))) { - return Str::markdown(File::get(resource_path(sprintf('views/vendor/cookie-solution/policy/privacy-policy.%s.md', $locale))), $this->markdownConfig); - } + $paths = [ + resource_path(sprintf('views/vendor/cookie-solution/policy/privacy-policy.%s.md', $locale)), + __DIR__.sprintf('/../resources/views/policy/privacy-policy.%s.md', $locale), + ]; - if (File::exists(__DIR__.sprintf('/../resources/views/policy/privacy-policy.%s.md', $locale))) { - return Str::markdown(File::get(__DIR__.sprintf('/../resources/views/policy/privacy-policy.%s.md', $locale)), $this->markdownConfig); + foreach ($paths as $path) { + if (! File::exists($path)) { + continue; + } + + try { + return $this->renderMarkdownFile($path); + } catch (FileNotFoundException) { + continue; + } } return null; @@ -255,4 +274,23 @@ protected function configDigest(): string return hash('sha256', $cookies); } + + /** + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + protected function renderMarkdownFile(string $sourcePath): string + { + $cachePath = storage_path(sprintf('framework/views/%s.html', hash('xxh128', $sourcePath))); + + if (File::exists($cachePath) && File::lastModified($cachePath) >= File::lastModified($sourcePath)) { + return File::get($cachePath); + } + + $content = Str::markdown(File::get($sourcePath)); + + File::ensureDirectoryExists(dirname($cachePath)); + File::put($cachePath, $content); + + return $content; + } }