summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Light <sam@lightscale.co.uk>2025-03-31 22:08:20 +0100
committerSam Light <samlight1994@gmail.com>2025-03-31 22:08:20 +0100
commitd700b3b5772022902948430d076fc3c0a00f20ef (patch)
tree781311f48a250474fb8f5a5034370678245bd3cd
parent41605e2ab4b8de04cd1c05a4cfe95b822ba46ae1 (diff)
Changes to Assets class manifest function
-rw-r--r--src/Assets.php49
-rw-r--r--tests/Feature/AssetsTest.php62
2 files changed, 85 insertions, 26 deletions
diff --git a/src/Assets.php b/src/Assets.php
index a431ffb..352bb2b 100644
--- a/src/Assets.php
+++ b/src/Assets.php
@@ -2,9 +2,11 @@
namespace Lightscale\LaralightAssets;
+use InvalidArgumentException;
+
class Assets
{
- private string $defaultManifest;
+ private ?string $defaultManifest = null;
private array $manifests = [];
private array $manifestsParsers = [];
private array $files = [];
@@ -49,9 +51,50 @@ class Assets
return $this->getManifests()[$name];
}
- public function getDefaultManifest(): Manifest
+ public function setDefaultManifest(?string $name): void
+ {
+ if ($name !== null && ($this->manifests[$name] ?? false) === false) {
+ throw new InvalidArgumentException(
+ "No manifest with name {$name} exists"
+ );
+ }
+
+ $this->defaultManifest = $name;
+ }
+
+ public function getDefaultManifest(): ?Manifest
+ {
+ return (
+ $this->defaultManifest === null ?
+ null :
+ $this->getManifest($this->defaultManifest)
+ );
+ }
+
+ public function getFileManifest(FileString $file): ?Manifest
+ {
+ if ($file->manifestName !== null) {
+ return $this->getManifest($file->manifestName);
+ }
+ else {
+ return $this->getDefaultManifest();
+ }
+ }
+
+ public function queueFile(string $file, string $fileClass, bool $footer): void
+ {
+ $file = new FileString($file);
+ $manifest = $this->getFileManifest($file);
+ }
+
+ public function queueScript(string $file, bool $footer = true): void
+ {
+ $this->queueFile($file, ScriptFile::class, $footer);
+ }
+
+ public function queueStyle(string $file): void
{
- return $this->getManifest($this->defaultManifest);
+ $this->queueFile($file, StyleFile::class, false);
}
}
diff --git a/tests/Feature/AssetsTest.php b/tests/Feature/AssetsTest.php
index 7850081..f618d46 100644
--- a/tests/Feature/AssetsTest.php
+++ b/tests/Feature/AssetsTest.php
@@ -2,6 +2,7 @@
use Lightscale\LaralightAssets\Assets;
use Lightscale\LaralightAssets\Manifest;
+use Lightscale\LaralightAssets\FileString;
describe('instance', function() {
it('instantiates', function() {
@@ -17,16 +18,14 @@ describe('instance', function() {
});
});
-describe('manages manifests', function() {
- it('adds manifests', function() {
- app(Assets::class)->addManifest(
- 'main',
- new Manifest(
- public_path('dist/manifest.json'),
- '/dist',
- ),
- isDefault: true,
- );
+$newManifest = fn() => new Manifest(
+ public_path('dist/manifest.json'),
+ '/dist',
+);
+
+describe('manages manifests', function() use ($newManifest) {
+ it('adds manifests', function() use ($newManifest) {
+ app(Assets::class)->addManifest('main', $newManifest(), isDefault: true);
})->throwsNoExceptions();
it('registers manifests', function() {
@@ -46,24 +45,18 @@ describe('manages manifests', function() {
->toContainOnlyInstancesOf(Manifest::class);
});
- it('gets manifest', function() {
+ it('gets manifest', function() use ($newManifest){
$assets = app(Assets::class);
- $manifest = new Manifest(
- public_path('dist/manifest.json'),
- '/dist',
- );
+ $manifest = $newManifest();
$name = 'main';
$assets->addManifest($name, $manifest, true);
expect($assets->getManifest($name))->toBe($manifest);
});
- it('gets default manifest', function() {
+ it('gets default manifest', function() use ($newManifest) {
$assets = app(Assets::class);
- $manifest = new Manifest(
- public_path('dist/manifest.json'),
- '/dist',
- );
+ $manifest = $newManifest();
$name = 'main';
$assets->addManifest($name, $manifest, true);
@@ -75,8 +68,31 @@ describe('manages manifests', function() {
$assets->getManifest('main');
})->throws(Exception::class);
- it('throws exception without default', function() {
+ it('has null default manifest')
+ ->expect(fn() => app(Assets::class)->getDefaultManifest())
+ ->toBeNull();
+
+ it('throws exception setting default to missing manifest', function() {
$assets = app(Assets::class);
- $assets->getDefaultManifest();
- })->throws(Error::class);
+ $assets->setDefaultManifest('testing');
+ })->throws(Exception::class);
+
+ test('get manifest from FileString', function() use($newManifest) {
+ $assets = app(Assets::class);
+ $manifest = $newManifest();
+ $assets->addManifest('main', $manifest, true);
+ $file = new FileString('main::test.css');
+ $result = $assets->getFileManifest($file);
+ expect($result)->toBe($manifest);
+ });
+
+ test('default manifest from FileString', function() use($newManifest) {
+ $assets = app(Assets::class);
+ $manifest = $newManifest();
+ $assets->addManifest('main', $manifest, true);
+ $file = new FileString('test.css');
+ $result = $assets->getFileManifest($file);
+ expect($result)->toBe($manifest);
+ });
+
});