Skip to content

Commit

Permalink
Allow to set/force the extension of the asset
Browse files Browse the repository at this point in the history
  • Loading branch information
promatik committed Apr 7, 2024
1 parent 75eb3af commit d099226
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/BassetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/FileOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d099226

Please sign in to comment.