Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 48 additions & 10 deletions src/CookieSolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -184,30 +185,48 @@

public function script(): HtmlString
{
return new HtmlString(view('cookie-solution::script')->render());

Check failure on line 188 in src/CookieSolution.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $view of function view expects view-string|null, string given.
}

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;
}

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;
Expand Down Expand Up @@ -255,4 +274,23 @@

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;
}
}
Loading