summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Light <sam@lightscale.co.uk>2025-03-31 23:28:21 +0100
committerSam Light <samlight1994@gmail.com>2025-03-31 23:28:21 +0100
commit138ff4bea020350f6b067624dea328d88ceb74b0 (patch)
tree73b550350f0aaaf2470ddc6d80c9bba9a0f60cb0
parent292c0eb477a8ffb7d3389a44ff1d41ccdc2ee790 (diff)
Queuing files
-rw-r--r--src/Assets.php19
-rw-r--r--tests/Feature/AssetsTest.php19
2 files changed, 38 insertions, 0 deletions
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);
+ });
});