Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pxpm committed Dec 31, 2024
1 parent fc00c75 commit aac27a7
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/AssetHashManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
3 changes: 2 additions & 1 deletion src/AssetPathManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 5 additions & 4 deletions src/BassetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,7 +43,7 @@ class BassetManager

public FileOutput $output;

public AssetPathManager $assetPathsManager;
public AssetPathManagerInterface $assetPathsManager;

public function __construct()
{
Expand Down Expand Up @@ -92,7 +93,7 @@ public function map(string $asset, string $source, array $attributes = []): void
}

$this->namedAssets[$asset] = [
'source' => $source,
'source' => $source,
'attributes' => $attributes,
];
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
38 changes: 31 additions & 7 deletions src/BassetServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/BassetCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions src/Console/Commands/BassetInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -41,7 +41,9 @@ public function handle(): void
}

// add basset folder to .gitignore
$this->addGitIgnore();
if ($this->option('git-ignore')) {
$this->addGitIgnore();
}

$this->addComposerCommands();

Expand Down
8 changes: 6 additions & 2 deletions src/Console/Commands/BassetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -45,5 +45,9 @@ public function handle(): void
}
$this->line('<fg=blue> Asset Key: </>'.$key.'</><fg=blue> Asset Source: </>'.$asset['source']);
}

if (empty($assets)) {
$this->line('No named assets listed.');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Backpack\Basset\Contracts;

interface AssetHashManager
interface AssetHashManagerInterface
{
public function generateHash(string $content): string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Backpack\Basset\Contracts;

interface AssetPathManager
interface AssetPathManagerInterface
{
public function getBasePath(): string;

Expand Down
1 change: 1 addition & 0 deletions src/Enums/StatusEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
32 changes: 16 additions & 16 deletions src/Helpers/CacheEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -92,7 +92,7 @@ public function getAssetDiskPath(): string

public function getAttributes(): array
{
return $this->attributes;
return $this->assetAttributes;
}

public function getAssetName(): string
Expand Down Expand Up @@ -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,
];
}

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

Expand All @@ -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()),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
it('echoes the attributes', function ($asset) {
bassetInstance($asset, true, [
'async' => true,
'type' => 'module',
'type' => 'module',
]);

$this->expectOutputRegex('/<script .+ async type="module"/');
Expand Down
Empty file.
1 change: 1 addition & 0 deletions tests/Helpers/bootstrap.min.js.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

14 changes: 10 additions & 4 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,24 @@

// setup fake links
Http::fake([
'https://unpkg.com/vue@3/dist/vue.global.prod.js' => Http::response(getStub('vue.global.prod.js')),
'https://unpkg.com/vue@3.1/dist/vue31.global.prod.js' => Http::response(getStub('vue31.global.prod.js')),
'https://unpkg.com/react@18/umd/react.production.min.js' => Http::response(getStub('react.production.min.js')),
'https://unpkg.com/vue@3/dist/vue.global.prod.js' => Http::response(getStub('vue.global.prod.js')),
'https://unpkg.com/vue@3.1/dist/vue31.global.prod.js' => Http::response(getStub('vue31.global.prod.js')),
'https://unpkg.com/react@18/umd/react.production.min.js' => Http::response(getStub('react.production.min.js')),
'https://unpkg.com/react@3/dist/reactscript.production.min.js' => Http::response(getStub('reactscript.production.min.js')),
'https://unpkg.com/backpack@5/dist/script.production.min.js' => Http::response(getStub('backpack.production.min.js')),
'https://unpkg.com/backpack@5/dist/script.production.min.js' => Http::response(getStub('backpack.production.min.js')),
]);

// setup config
config([
'backpack.basset.disk' => 'basset',
'backpack.basset.path', 'basset',
]);
// add fake files to public_path() for testing
File::ensureDirectoryExists(public_path('bootstrap/js'), 0755, true);
File::ensureDirectoryExists(public_path('bootstrap/css'), 0755, true);

File::put(public_path('bootstrap/js/bootstrap.min.js'), getStub('bootstrap.min.js'));
File::put(public_path('bootstrap/css/bootstrap.min.css'), getStub('bootstrap.min.css'));
})
->in(__DIR__);

Expand Down

0 comments on commit aac27a7

Please sign in to comment.