From 138ff4bea020350f6b067624dea328d88ceb74b0 Mon Sep 17 00:00:00 2001 From: Sam Light Date: Mon, 31 Mar 2025 23:28:21 +0100 Subject: Queuing files --- src/Assets.php | 19 +++++++++++++++++++ tests/Feature/AssetsTest.php | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Assets.php b/src/Assets.php index 981ac76..962041f 100644 --- a/src/Assets.php +++ b/src/Assets.php @@ -12,6 +12,7 @@ class Assets private array $files = []; private array $queuedHeadFiles = []; private array $queuedFooterFiles = []; + private array $queuedFileHashs = []; public function addManifest( string $name, @@ -81,10 +82,28 @@ class Assets } } + private function pushFile(File $file, bool $footer): void + { + $hash = $file->hash(); + + if ($this->queuedFileHashes[$hash] ?? false) { + throw new Exception("File {$file} is already queued"); + } + else { + $this->queuedFileHashes[$hash] = true; + $queue = $footer ? 'queuedFooterFiles' : 'queuedHeadFiles'; + $this->{$queue}[] = $file; + } + } + public function queueFile(string $file, string $fileClass, bool $footer): void { $file = new FileString($file); $manifest = $this->getFileManifest($file); + $url = $manifest->getUrl($file->fileUri) ?? $file->fileUri; + + $file = new $fileClass($url); + $this->pushFile($file, $footer); } public function queueScript(string $file, bool $footer = true): void diff --git a/tests/Feature/AssetsTest.php b/tests/Feature/AssetsTest.php index f618d46..9614eee 100644 --- a/tests/Feature/AssetsTest.php +++ b/tests/Feature/AssetsTest.php @@ -94,5 +94,24 @@ describe('manages manifests', function() use ($newManifest) { $result = $assets->getFileManifest($file); expect($result)->toBe($manifest); }); +}); + +describe('queuing files', function() use ($newManifest) { + test('queue script', function() use ($newManifest) { + $assets = app(Assets::class); + $manifest = $newManifest(); + $assets->addManifest('main', $manifest, true); + $assets->queueScript('main::main.js'); + + expect($assets->getQueuedFooterFiles())->toBeArray()->toHaveLength(1); + }); + + test('queue style', function() use ($newManifest) { + $assets = app(Assets::class); + $manifest = $newManifest(); + $assets->addManifest('main', $manifest, true); + $assets->queueStyle('main::main.css'); + expect($assets->getQueuedHeadFiles())->toBeArray()->toHaveLength(1); + }); }); -- cgit v1.2.3