diff --git a/src/AssetHashManager.php b/src/AssetHashManager.php index d006c0c..930108c 100644 --- a/src/AssetHashManager.php +++ b/src/AssetHashManager.php @@ -2,9 +2,9 @@ namespace Backpack\Basset; -use Backpack\Basset\Contracts\AssetHashManager as AssetHashManagerContract; +use Backpack\Basset\Contracts\AssetHashManagerInterface; -final class AssetHashManager implements AssetHashManagerContract +final class AssetHashManager implements AssetHashManagerInterface { public function generateHash(string $content): string { diff --git a/src/AssetPathManager.php b/src/AssetPathManager.php index 00efa52..80eedb0 100644 --- a/src/AssetPathManager.php +++ b/src/AssetPathManager.php @@ -2,10 +2,11 @@ namespace Backpack\Basset; +use Backpack\Basset\Contracts\AssetPathManagerInterface; use Illuminate\Support\Facades\File; use Illuminate\Support\Str; -final class AssetPathManager implements \Backpack\Basset\Contracts\AssetPathManager +final class AssetPathManager implements AssetPathManagerInterface { private string $basePath; diff --git a/src/BassetManager.php b/src/BassetManager.php index 0da63d4..975a1e3 100644 --- a/src/BassetManager.php +++ b/src/BassetManager.php @@ -2,6 +2,7 @@ namespace Backpack\Basset; +use Backpack\Basset\Contracts\AssetPathManagerInterface; use Backpack\Basset\Enums\StatusEnum; use Backpack\Basset\Events\BassetCachedEvent; use Backpack\Basset\Helpers\CacheEntry; @@ -42,7 +43,7 @@ class BassetManager public FileOutput $output; - public AssetPathManager $assetPathsManager; + public AssetPathManagerInterface $assetPathsManager; public function __construct() { @@ -92,7 +93,7 @@ public function map(string $asset, string $source, array $attributes = []): void } $this->namedAssets[$asset] = [ - 'source' => $source, + 'source' => $source, 'attributes' => $attributes, ]; } @@ -224,7 +225,7 @@ public function loadAsset(CacheEntry $asset, $output) if (str_starts_with($asset->getAssetPath(), public_path())) { $output && $this->output->write($asset); - return $this->loader->finish(StatusEnum::INVALID); + return $this->loader->finish(StatusEnum::PUBLIC_FILE); } return $this->uploadAssetToDisk($asset, $content, $output); @@ -546,7 +547,7 @@ public function buildCacheEntry(CacheEntry|string $asset, $attributes = []): Cac return (new CacheEntry()) ->assetName($assetName) ->assetPath($asset['source']) - ->attributes(isset($asset['attributes']) ? array_merge($asset['attributes'], $attributes) : $attributes); + ->assetAttributes(isset($asset['asset_attributes']) ? array_merge($asset['asset_attributes'], $attributes) : (isset($asset['attributes']) ? array_merge($asset['attributes'], $attributes) : $attributes)); } private function getNamedAsset(string $asset): array diff --git a/src/BassetServiceProvider.php b/src/BassetServiceProvider.php index 7ff818f..88a1f3a 100644 --- a/src/BassetServiceProvider.php +++ b/src/BassetServiceProvider.php @@ -2,7 +2,6 @@ namespace Backpack\Basset; -use Backpack\Basset\Facades\Basset; use Illuminate\Support\Facades\Log; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; @@ -71,14 +70,17 @@ public function register(): void // Load basset disk $this->loadDisk(); + // Load public disk + $this->loadPublicDisk(); + // Register the service the package provides. $this->app->scoped('basset', fn () => new BassetManager()); // Register the asset path resolver - $this->app->singleton(Contracts\AssetPathManager::class, AssetPathManager::class); + $this->app->singleton(Contracts\AssetPathManagerInterface::class, AssetPathManager::class); // Register the asset hash manager - $this->app->singleton(Contracts\AssetHashManager::class, AssetHashManager::class); + $this->app->singleton(Contracts\AssetHashManagerInterface::class, AssetHashManager::class); // Register blade directives $this->registerBladeDirectives(); @@ -190,11 +192,33 @@ public function loadDisk(): void // add the basset disk to filesystem configuration app()->config['filesystems.disks.basset'] = [ - 'driver' => 'local', - 'root' => public_path(), - 'url' => url(''), + 'driver' => 'local', + 'root' => storage_path('app/public'), + 'url' => env('APP_URL').'/storage', + 'visibility' => 'public', + 'throw' => false, + ]; + } + + public function loadPublicDisk(): void + { + // if the basset disk already exists, don't override it + if (app()->config['filesystems.disks.public_basset']) { + return; + } + + // if the basset disk isn't being used at all, don't even bother to add it + if (app()->config['backpack.basset.disk'] !== 'public_basset') { + return; + } + + // add the basset disk to filesystem configuration + app()->config['filesystems.disks.public_basset'] = [ + 'driver' => 'local', + 'root' => public_path(), + 'url' => url(''), 'visibility' => 'public', - 'throw' => false, + 'throw' => false, ]; } diff --git a/src/Console/Commands/BassetCache.php b/src/Console/Commands/BassetCache.php index b094d0d..98e746f 100644 --- a/src/Console/Commands/BassetCache.php +++ b/src/Console/Commands/BassetCache.php @@ -30,7 +30,7 @@ class BassetCache extends Command * * @var string */ - protected $description = 'Cache all the assets under the basset blade directive and update package manifesto'; + protected $description = 'Cache all the assets using the basset blade directive and update the cache map.'; /** * Execute the console command. diff --git a/src/Console/Commands/BassetInstall.php b/src/Console/Commands/BassetInstall.php index d6ec44c..db9d282 100644 --- a/src/Console/Commands/BassetInstall.php +++ b/src/Console/Commands/BassetInstall.php @@ -18,7 +18,7 @@ class BassetInstall extends Command * * @var string */ - protected $signature = 'basset:install {--no-check} : As the name says, `basset:check` will not run.'; + protected $signature = 'basset:install {--no-check} {--git-ignore} : As the name says, `basset:check` will not run.'; /** * The console command description. @@ -41,7 +41,9 @@ public function handle(): void } // add basset folder to .gitignore - $this->addGitIgnore(); + if ($this->option('git-ignore')) { + $this->addGitIgnore(); + } $this->addComposerCommands(); diff --git a/src/Console/Commands/BassetList.php b/src/Console/Commands/BassetList.php index 2e390e0..a41a3aa 100644 --- a/src/Console/Commands/BassetList.php +++ b/src/Console/Commands/BassetList.php @@ -18,14 +18,14 @@ class BassetList extends Command * * @var string */ - protected $signature = 'basset:list {--filter=*}'; + protected $signature = 'basset:list-named {--filter=*}'; /** * The console command description. * * @var string */ - protected $description = 'List the assets that packages added to the map.'; + protected $description = 'List the named assets that packages added to the map.'; /** * Execute the console command. @@ -45,5 +45,9 @@ public function handle(): void } $this->line(' Asset Key: '.$key.' Asset Source: '.$asset['source']); } + + if (empty($assets)) { + $this->line('No named assets listed.'); + } } } diff --git a/src/Contracts/AssetHashManager.php b/src/Contracts/AssetHashManagerInterface.php similarity index 87% rename from src/Contracts/AssetHashManager.php rename to src/Contracts/AssetHashManagerInterface.php index dc814cf..023ee88 100644 --- a/src/Contracts/AssetHashManager.php +++ b/src/Contracts/AssetHashManagerInterface.php @@ -2,7 +2,7 @@ namespace Backpack\Basset\Contracts; -interface AssetHashManager +interface AssetHashManagerInterface { public function generateHash(string $content): string; diff --git a/src/Contracts/AssetPathManager.php b/src/Contracts/AssetPathManagerInterface.php similarity index 87% rename from src/Contracts/AssetPathManager.php rename to src/Contracts/AssetPathManagerInterface.php index 2f68aeb..8ddfdcc 100644 --- a/src/Contracts/AssetPathManager.php +++ b/src/Contracts/AssetPathManagerInterface.php @@ -2,7 +2,7 @@ namespace Backpack\Basset\Contracts; -interface AssetPathManager +interface AssetPathManagerInterface { public function getBasePath(): string; diff --git a/src/Enums/StatusEnum.php b/src/Enums/StatusEnum.php index f972b4f..678ff6c 100644 --- a/src/Enums/StatusEnum.php +++ b/src/Enums/StatusEnum.php @@ -8,5 +8,6 @@ enum StatusEnum: string case IN_CACHE = 'Already in cache'; case INTERNALIZED = 'Internalized'; case INVALID = 'Not in a CDN or local filesystem, falling back to provided path'; + case PUBLIC_FILE = 'Public file, no need to internalize'; case DISABLED = 'Development mode active'; } diff --git a/src/Helpers/CacheEntry.php b/src/Helpers/CacheEntry.php index 4d143b3..de80c15 100644 --- a/src/Helpers/CacheEntry.php +++ b/src/Helpers/CacheEntry.php @@ -2,8 +2,8 @@ namespace Backpack\Basset\Helpers; -use Backpack\Basset\Contracts\AssetHashManager; -use Backpack\Basset\Contracts\AssetPathManager; +use Backpack\Basset\AssetHashManager; +use Backpack\Basset\AssetPathManager; use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Facades\File; @@ -18,9 +18,9 @@ final class CacheEntry implements Arrayable, JsonSerializable private string $assetDiskPath; - private array $attributes = []; + private array $assetAttributes = []; - private string $content_hash = ''; + private string $assetContentHash = ''; private AssetPathManager $assetPathsManager; @@ -36,7 +36,7 @@ public static function from(array $asset): self { $instance = new self(); - return $instance->assetName($asset['asset_name'])->assetPath($asset['asset_path'])->assetDiskPath($asset['asset_disk_path'])->attributes($asset['attributes']); + return $instance->assetName($asset['asset_name'])->assetPath($asset['asset_path'])->assetDiskPath($asset['asset_disk_path'])->assetAttributes($asset['asset_attributes']); } public function assetName(string $assetName): self @@ -73,9 +73,9 @@ public function assetDiskPath(string $assetDiskPath): self return $this; } - public function attributes(array $attributes): self + public function assetAttributes(array $attributes): self { - $this->attributes = $attributes; + $this->assetAttributes = $attributes; return $this; } @@ -92,7 +92,7 @@ public function getAssetDiskPath(): string public function getAttributes(): array { - return $this->attributes; + return $this->assetAttributes; } public function getAssetName(): string @@ -124,11 +124,11 @@ public function isLocalAsset() public function toArray(): array { return [ - 'asset_name' => $this->assetName, - 'asset_path' => $this->assetPath, - 'asset_disk_path' => isset($this->assetDiskPath) ? $this->assetDiskPath : $this->getPathOnDisk($this->assetPath), - 'attributes' => $this->attributes, - 'content_hash' => $this->content_hash, + 'asset_name' => $this->assetName, + 'asset_path' => $this->assetPath, + 'asset_disk_path' => isset($this->assetDiskPath) ? $this->assetDiskPath : $this->getPathOnDisk($this->assetPath), + 'asset_attributes' => $this->assetAttributes, + 'asset_content_hash' => $this->assetContentHash, ]; } @@ -158,7 +158,7 @@ public function getContentAndGenerateHash(): string public function getContentHash(): string { - return $this->content_hash; + return $this->assetContentHash; } public function getPathOnDiskHashed(string $content): string @@ -168,7 +168,7 @@ public function getPathOnDiskHashed(string $content): string // get the hash for the content $hash = $this->assetHashManager->generateHash($content); - $this->content_hash = $this->assetHashManager->appendHashToPath($content, $hash); + $this->assetContentHash = $this->assetHashManager->appendHashToPath($content, $hash); return $this->assetHashManager->appendHashToPath($path, $hash); } @@ -177,7 +177,7 @@ public function generateContentHash(string $content = null): string { $content = $content ?? $this->getContent(); - return $this->content_hash = $this->assetHashManager->generateHash($content); + return $this->assetContentHash = $this->assetHashManager->generateHash($content); } private function getPathOnDisk(string $asset): string diff --git a/src/Helpers/FileOutput.php b/src/Helpers/FileOutput.php index 0a30795..d919403 100644 --- a/src/Helpers/FileOutput.php +++ b/src/Helpers/FileOutput.php @@ -44,8 +44,8 @@ public function write(CacheEntry $asset): void $file = match ($extension) { 'jpg', 'jpeg', 'png', 'webp', 'gif', 'svg' => 'img', 'mp3', 'ogg', 'wav', 'mp4', 'webm', 'avi' => 'source', - 'pdf' => 'object', - 'vtt' => 'track', + 'pdf' => 'object', + 'vtt' => 'track', default => $extension }; @@ -56,7 +56,7 @@ public function write(CacheEntry $asset): void } echo Blade::render($template, [ - 'src' => $this->assetPath($filePath), + 'src' => $this->assetPath($filePath), 'args' => $this->prepareAttributes($asset->getAttributes()), ]); } diff --git a/tests/Feature/OutputTest.php b/tests/Feature/OutputTest.php index dfcd604..f78fcf5 100644 --- a/tests/Feature/OutputTest.php +++ b/tests/Feature/OutputTest.php @@ -9,7 +9,7 @@ it('echoes the attributes', function ($asset) { bassetInstance($asset, true, [ 'async' => true, - 'type' => 'module', + 'type' => 'module', ]); $this->expectOutputRegex('/