Skip to content

Commit

Permalink
Add method for parse path on directives to v1 (#34)
Browse files Browse the repository at this point in the history
* Add method for parse path on directives to v1

* Resolve issue in StyleCI

* Resolve issue in StyleCI
  • Loading branch information
FrancaR authored and Elhebert committed Sep 20, 2019
1 parent 84ec48b commit f49e2c7
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions src/SriServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Elhebert\SubresourceIntegrity;

use Illuminate\Support\HtmlString;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

Expand All @@ -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 "<link href='{$href}' rel='stylesheet' {$integrity}>";
} elseif (ends_with($path, 'js')) {
return "<script src='{$href}' {$integrity}></script>";
} 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 "<link href='{$href}' rel='stylesheet' {$integrity}>";
} elseif (ends_with($path, 'js')) {
return "<script src='{$href}' {$integrity}></script>";
} 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("<script src='{$href}' {$integrity}></script>");
}

private function generateCssUrl(string $href, string $integrity): HtmlString
{
return new HtmlString("<link href='{$href}' rel='stylesheet' {$integrity}>");
}
}

0 comments on commit f49e2c7

Please sign in to comment.