From 207a312c27594361bab23dcd16b2b9a1349aa0da Mon Sep 17 00:00:00 2001 From: "Pedro X." Date: Mon, 8 Apr 2024 14:07:35 +0100 Subject: [PATCH 1/3] store the file with the provided extension --- src/BassetManager.php | 15 ++++++++++++--- src/Helpers/FileOutput.php | 3 ++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/BassetManager.php b/src/BassetManager.php index d5e63f0..2709fa6 100644 --- a/src/BassetManager.php +++ b/src/BassetManager.php @@ -21,13 +21,19 @@ class BassetManager use Traits\ViewPathsTrait; private FilesystemAdapter $disk; + private array $loaded; + private string $basePath; + private bool $dev = false; public CacheMap $cacheMap; + public LoadingTime $loader; + public Unarchiver $unarchiver; + public FileOutput $output; public function __construct() @@ -90,12 +96,13 @@ public function loaded(): array * @param string $asset * @return string */ - public function getPath(string $asset): string + public function getPath(string $asset, string $extension = null): string { return Str::of($this->basePath) ->append(str_replace([base_path().'/', base_path(), 'http://', 'https://', '://', '<', '>', ':', '"', '|', "\0", '*', '`', ';', "'", '+'], '', $asset)) ->before('?') - ->replace('/\\', '/'); + ->replace('/\\', '/') + ->finish($extension ?? ''); } /** @@ -138,8 +145,10 @@ public function basset(string $asset, bool|string $output = true, array $attribu { $this->loader->start(); + $extension = empty($extension) ? $extension : Str::start($extension, '.'); + // Get asset path - $path = $this->getPath(is_string($output) ? $output : $asset); + $path = $this->getPath(is_string($output) ? $output : $asset, $extension); if ($this->isLoaded($path)) { return $this->loader->finish(StatusEnum::LOADED); diff --git a/src/Helpers/FileOutput.php b/src/Helpers/FileOutput.php index f231a58..1250db7 100644 --- a/src/Helpers/FileOutput.php +++ b/src/Helpers/FileOutput.php @@ -37,7 +37,7 @@ public function __construct() public function write(string $path, array $attributes = [], string $extension = null): void { $extension ??= (string) Str::of($path)->afterLast('.'); - + $extension = Str::afterLast($extension, '.'); // map extensions $file = match ($extension) { 'jpg', 'jpeg', 'png', 'webp', 'gif', 'svg' => 'img', @@ -53,6 +53,7 @@ public function write(string $path, array $attributes = [], string $extension = return; } + $path = Str::of($path)->finish('.'.$extension); echo Blade::render($template, [ 'src' => $this->assetPath($path), 'args' => $this->prepareAttributes($attributes), From ed14dd013b7f21d7904a425ac57c0c4cfa6f091a Mon Sep 17 00:00:00 2001 From: Antonio Almeida Date: Mon, 29 Apr 2024 02:08:13 +0100 Subject: [PATCH 2/3] Fix formatting issues in BassetManager, CacheMap, and FileOutput classes --- src/BassetManager.php | 10 +++------- src/Helpers/CacheMap.php | 2 +- src/Helpers/FileOutput.php | 6 +++--- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/BassetManager.php b/src/BassetManager.php index 2709fa6..7597ff4 100644 --- a/src/BassetManager.php +++ b/src/BassetManager.php @@ -21,19 +21,13 @@ class BassetManager use Traits\ViewPathsTrait; private FilesystemAdapter $disk; - private array $loaded; - private string $basePath; - private bool $dev = false; public CacheMap $cacheMap; - public LoadingTime $loader; - public Unarchiver $unarchiver; - public FileOutput $output; public function __construct() @@ -145,7 +139,9 @@ public function basset(string $asset, bool|string $output = true, array $attribu { $this->loader->start(); - $extension = empty($extension) ? $extension : Str::start($extension, '.'); + if ($extension) { + $extension = Str::start($extension, '.'); + } // Get asset path $path = $this->getPath(is_string($output) ? $output : $asset, $extension); diff --git a/src/Helpers/CacheMap.php b/src/Helpers/CacheMap.php index 4e7477f..d443589 100644 --- a/src/Helpers/CacheMap.php +++ b/src/Helpers/CacheMap.php @@ -28,7 +28,7 @@ public function __construct(FilesystemAdapter $disk, string $basePath) // Load map if (File::exists($this->filePath)) { - $this->map = json_decode(File::get($this->filePath), true); + $this->map = json_decode(File::get($this->filePath), true) ?? []; } } diff --git a/src/Helpers/FileOutput.php b/src/Helpers/FileOutput.php index 1250db7..0fa945f 100644 --- a/src/Helpers/FileOutput.php +++ b/src/Helpers/FileOutput.php @@ -36,8 +36,8 @@ public function __construct() */ public function write(string $path, array $attributes = [], string $extension = null): void { - $extension ??= (string) Str::of($path)->afterLast('.'); - $extension = Str::afterLast($extension, '.'); + $extension ??= (string) Str::afterLast($extension, '.'); + // map extensions $file = match ($extension) { 'jpg', 'jpeg', 'png', 'webp', 'gif', 'svg' => 'img', @@ -53,7 +53,7 @@ public function write(string $path, array $attributes = [], string $extension = return; } - $path = Str::of($path)->finish('.'.$extension); + $path = (string) Str::finish($path, '.'.$extension); echo Blade::render($template, [ 'src' => $this->assetPath($path), 'args' => $this->prepareAttributes($attributes), From c83369cac8edec1c8a85ba6a627fd0dbfe748823 Mon Sep 17 00:00:00 2001 From: Antonio Almeida Date: Mon, 29 Apr 2024 02:10:48 +0100 Subject: [PATCH 3/3] Refactor file extension handling in FileOutput class --- src/Helpers/FileOutput.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Helpers/FileOutput.php b/src/Helpers/FileOutput.php index 0fa945f..2137bbb 100644 --- a/src/Helpers/FileOutput.php +++ b/src/Helpers/FileOutput.php @@ -36,7 +36,7 @@ public function __construct() */ public function write(string $path, array $attributes = [], string $extension = null): void { - $extension ??= (string) Str::afterLast($extension, '.'); + $extension ??= (string) Str::of($path)->afterLast('.'); // map extensions $file = match ($extension) { @@ -53,7 +53,7 @@ public function write(string $path, array $attributes = [], string $extension = return; } - $path = (string) Str::finish($path, '.'.$extension); + $path = (string) Str::of($path)->finish('.'.$extension); echo Blade::render($template, [ 'src' => $this->assetPath($path), 'args' => $this->prepareAttributes($attributes),