From f49e2c7a7638b769d5eb26ca058d4440301635f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Fran=C3=A7a?= <54752465+FrancaR@users.noreply.github.com> Date: Fri, 20 Sep 2019 03:18:42 -0300 Subject: [PATCH] Add method for parse path on directives to v1 (#34) * Add method for parse path on directives to v1 * Resolve issue in StyleCI * Resolve issue in StyleCI --- src/SriServiceProvider.php | 54 +++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/SriServiceProvider.php b/src/SriServiceProvider.php index 1110737..b5dd9b6 100644 --- a/src/SriServiceProvider.php +++ b/src/SriServiceProvider.php @@ -2,6 +2,7 @@ namespace Elhebert\SubresourceIntegrity; +use Illuminate\Support\HtmlString; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; @@ -28,39 +29,56 @@ public function boot() ]); Blade::directive('mixSri', function (string $path, bool $crossOrigin = false) { + $path = $this->removeQuotes($path); + if (starts_with($path, ['http', 'https', '//'])) { $href = $path; } else { $href = mix($path); } - $integrity = SriFacade::html($path, $crossOrigin); - - if (ends_with($path, 'css')) { - return ""; - } elseif (ends_with($path, 'js')) { - return ""; - } else { - throw new \Exception('Invalid file'); - } + return $this->parseAndGenerateUrl($path, $href, $crossOrigin); }); Blade::directive('assetSri', function (string $path, bool $crossOrigin = false) { + $path = $this->removeQuotes($path); + if (starts_with($path, ['http', 'https', '//'])) { $href = $path; } else { $href = asset($path); } - $integrity = SriFacade::html($path, $crossOrigin); - - if (ends_with($path, 'css')) { - return ""; - } elseif (ends_with($path, 'js')) { - return ""; - } else { - throw new \Exception('Invalid file'); - } + return $this->parseAndGenerateUrl($path, $href, $crossOrigin); }); } + + private function removeQuotes(string $path): string + { + $values = ['\'', '"']; + + return str_replace($values, '', $path); + } + + private function parseAndGenerateUrl(string $path, string $href, bool $crossOrigin): HtmlString + { + $integrity = SriFacade::html($href, $crossOrigin); + if (ends_with($path, 'css')) { + return $this->generateCssUrl($href, $integrity); + } elseif (ends_with($path, 'js')) { + return $this->generateJsUrl($href, $integrity); + } else { + throw new \Exception('Invalid file'); + } + } + + private function generateJsUrl(string $href, string $integrity): HtmlString + { + return new HtmlString(""); + } + + private function generateCssUrl(string $href, string $integrity): HtmlString + { + return new HtmlString(""); + } }