From d099226e216ed06cee28a99f5f590c30b846d146 Mon Sep 17 00:00:00 2001 From: Antonio Almeida Date: Sun, 7 Apr 2024 23:59:15 +0100 Subject: [PATCH] Allow to set/force the extension of the asset --- src/BassetManager.php | 16 ++++++++-------- src/Helpers/FileOutput.php | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/BassetManager.php b/src/BassetManager.php index 7f83d9b..d5e63f0 100644 --- a/src/BassetManager.php +++ b/src/BassetManager.php @@ -134,7 +134,7 @@ public function getUrl(string $asset): string * @param array $attributes * @return StatusEnum */ - public function basset(string $asset, bool|string $output = true, array $attributes = []): StatusEnum + public function basset(string $asset, bool|string $output = true, array $attributes = [], string $extension = null): StatusEnum { $this->loader->start(); @@ -150,7 +150,7 @@ public function basset(string $asset, bool|string $output = true, array $attribu // Retrieve from map $mapped = $this->cacheMap->getAsset($asset); if ($mapped && ! $this->dev) { - $output && $this->output->write($mapped, $attributes); + $output && $this->output->write($mapped, $attributes, $extension); return $this->loader->finish(StatusEnum::IN_CACHE); } @@ -160,13 +160,13 @@ public function basset(string $asset, bool|string $output = true, array $attribu // may be an internalized asset (folder or zip) if ($this->disk->exists($path)) { $asset = $this->disk->url($path); - $output && $this->output->write($asset, $attributes); + $output && $this->output->write($asset, $attributes, $extension); return $this->loader->finish(StatusEnum::IN_CACHE); } // public file (default fallback) - $output && $this->output->write($asset, $attributes); + $output && $this->output->write($asset, $attributes, $extension); return $this->loader->finish(StatusEnum::INVALID); } @@ -177,7 +177,7 @@ public function basset(string $asset, bool|string $output = true, array $attribu // Check if asset exists in basset folder // (ignores cache if in dev mode) if ($this->disk->exists($path) && ! $this->dev) { - $output && $this->output->write($url, $attributes); + $output && $this->output->write($url, $attributes, $extension); $this->cacheMap->addAsset($asset, $url); return $this->loader->finish(StatusEnum::IN_CACHE); @@ -187,7 +187,7 @@ public function basset(string $asset, bool|string $output = true, array $attribu if (Str::isUrl($asset)) { // when in dev mode, cdn should be rendered if ($this->dev) { - $output && $this->output->write($asset, $attributes); + $output && $this->output->write($asset, $attributes, $extension); return $this->loader->finish(StatusEnum::DISABLED); } @@ -209,14 +209,14 @@ public function basset(string $asset, bool|string $output = true, array $attribu $result = $this->disk->put($path, $content, 'public'); if ($result) { - $output && $this->output->write($url, $attributes); + $output && $this->output->write($url, $attributes, $extension); $this->cacheMap->addAsset($asset, $url); return $this->loader->finish(StatusEnum::INTERNALIZED); } // Fallback to the CDN/path - $output && $this->output->write($asset, $attributes); + $output && $this->output->write($asset, $attributes, $extension); return $this->loader->finish(StatusEnum::INVALID); } diff --git a/src/Helpers/FileOutput.php b/src/Helpers/FileOutput.php index 236e0ef..f231a58 100644 --- a/src/Helpers/FileOutput.php +++ b/src/Helpers/FileOutput.php @@ -34,9 +34,9 @@ public function __construct() * @param array $attributes * @return void */ - public function write(string $path, array $attributes = []): void + public function write(string $path, array $attributes = [], string $extension = null): void { - $extension = (string) Str::of($path)->afterLast('.'); + $extension ??= (string) Str::of($path)->afterLast('.'); // map extensions $file = match ($extension) {