Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- `AssetFile` (application), `AssetRegistrationService`, `MissingBuilds`, `AssetException`, `ViteManager`, `AssetContainerManager`, `InMemoryAssetRepository` and `AssetEnqueuer` are now `final` (CONV-08). This is a restriction of the API for anyone who extended them: a class that is not an extension point can no longer be subclassed; compose them instead
- **BREAKING**: `AssetManager`, `AssetRetrievalService` and `AssetContainer` are now `final` (CONV-08), the second one also `readonly`. A class that extends one of them no longer loads; compose them instead. Code that mocked `AssetManager` or `AssetRetrievalService` builds the real object (`new AssetManager(new AssetRetrievalService(new AssetContainerManager(new InMemoryAssetRepository)))`) and registers its containers with `addContainer()`. `Asset` stays open on purpose: `AssetRegistrationService::register()` instantiates the class it is given as `$assetClass`
- `AssetContainer` moves from `Infrastructure\Repositories` to `Domain\Models` and `AssetFile` from `Infrastructure\Services` to `Application\Services`, so that the domain and application layers no longer depend on the infrastructure layer. Each former class name stays available as a deprecated alias, removed in the next major version
- `AssetManager::add()` returns the new `AssetEnqueuerInterface` contract, which the `AssetEnqueuer` implements, instead of the concrete `AssetEnqueuer`. The fluent methods are the same
- An unknown asset container, and `useVite()` called before a container is chosen, throw the named `AssetContainerNotFoundException` exception instead of a bare `\RuntimeException`. It extends `\RuntimeException` and the messages are unchanged
Expand Down
6 changes: 3 additions & 3 deletions src/Application/Services/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
* This class provides methods to add, retrieve, and manage asset containers,
* as well as to generate asset URLs through the infrastructure layer (Vite, manifest, etc).
*/
class AssetManager
final class AssetManager
{
/**
* List of registered asset containers.
*
* @var array<string, AssetContainer>
*/
protected array $containers = [];
private array $containers = [];

/**
* The default asset container name.
*/
protected ?string $defaultContainer = null;
private ?string $defaultContainer = null;

/**
* Initializes the asset manager with the retrieval service.
Expand Down
4 changes: 2 additions & 2 deletions src/Application/Services/AssetRetrievalService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
* This service acts as a use case boundary for asset retrieval, providing methods
* to fetch individual assets or all assets registered in the container manager.
*/
class AssetRetrievalService
final readonly class AssetRetrievalService
{
/**
* Initializes the retrieval service with the container manager.
*
* @param AssetContainerManager $manager The asset container manager
*/
public function __construct(private readonly AssetContainerManager $manager) {}
public function __construct(private AssetContainerManager $manager) {}

/**
* Retrieves a single asset by name.
Expand Down
14 changes: 7 additions & 7 deletions src/Domain/Models/AssetContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* build directories, manifest files, and base asset paths. Used by infrastructure services
* to resolve asset locations and integration with tools like Vite.
*/
class AssetContainer
final class AssetContainer
{
/**
* Array of asset directory paths.
Expand All @@ -26,35 +26,35 @@ class AssetContainer
/**
* The path to the hot reload file used by Vite.
*/
protected string $hotFile;
private readonly string $hotFile;

/**
* The directory where built assets are stored.
*/
protected string $buildDirectory;
private readonly string $buildDirectory;

/**
* The path to the Vite manifest file.
*/
protected string $manifestPath;
private readonly string $manifestPath;

/**
* The directory `build_directory` is relative to, when it is the container's own and not one shared by all.
*/
protected ?string $publicPath;
private readonly ?string $publicPath;

/**
* The URL of that directory.
*/
protected ?string $publicUrl;
private readonly ?string $publicUrl;

/**
* Creates a new asset container instance.
*
* @param string $name The unique identifier for this container
* @param array $config Configuration options for the container
*/
public function __construct(protected string $name, protected array $config = [])
public function __construct(private readonly string $name, array $config = [])
{
$this->hotFile = $config['hot_file'] ?? $this->name.'.hot';
$this->buildDirectory = $config['build_directory'] ?? 'build/'.$this->name;
Expand Down
13 changes: 13 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
declare(strict_types=1);

use Brain\Monkey;
use Pollora\Asset\Application\Services\AssetRetrievalService;
use Pollora\Asset\Domain\Services\AssetContainerManager;
use Pollora\Asset\Infrastructure\Repositories\InMemoryAssetRepository;

/*
|--------------------------------------------------------------------------
Expand All @@ -23,3 +26,13 @@

Monkey\tearDown();
})->in('Unit');

/**
* A real retrieval service over an empty in-memory repository.
*
* The services are final, so a test builds them instead of mocking them.
*/
function emptyRetrievalService(): AssetRetrievalService
{
return new AssetRetrievalService(new AssetContainerManager(new InMemoryAssetRepository));
}
14 changes: 6 additions & 8 deletions tests/Unit/AssetEnqueuerBehaviourTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
Functions\when('sanitize_title')->alias(fn (string $title): string => strtolower(str_replace('.', '-', $title)));
Functions\when('esc_url')->returnArg();

$this->assetManager = Mockery::mock(AssetManager::class);
$this->assetManager = new AssetManager(emptyRetrievalService());

$this->hookAction = Mockery::mock(HookAction::class);
$this->hookAction->shouldReceive('add')->andReturnUsing(function (...$arguments): HookAction {
Expand Down Expand Up @@ -86,8 +86,8 @@ public function log($level, string|Stringable $message, array $context = []): vo

describe('the containers the enqueuer reads', function (): void {
it('takes the container of the manager, without a runtime context', function (): void {
$container = new AssetContainer('theme');
$this->assetManager->shouldReceive('getContainer')->with('theme')->once()->andReturn($container);
$this->assetManager->addContainer('theme', []);
$container = $this->assetManager->getContainer('theme');

$enqueuer = new AssetEnqueuer($this->assetManager, $this->hookAction, $this->hookFilter);
$enqueuer->container('theme');
Expand All @@ -96,8 +96,6 @@ public function log($level, string|Stringable $message, array $context = []): vo
});

it('names the container nobody registered', function (): void {
$this->assetManager->shouldReceive('getContainer')->with('ghost')->andReturn(null);

expect(fn () => ($this->make)()->container('ghost'))
->toThrow(AssetContainerNotFoundException::class, "Asset container 'ghost' not found. Make sure you have added it via AssetManager::addContainer().");
});
Expand All @@ -113,8 +111,8 @@ public function log($level, string|Stringable $message, array $context = []): vo
});

it('resolves Vite for its container, without a runtime context', function (): void {
$container = new AssetContainer('theme');
$this->assetManager->shouldReceive('getContainer')->andReturn($container);
$this->assetManager->addContainer('theme', []);
$container = $this->assetManager->getContainer('theme');
$factory = Mockery::mock(ViteManagerFactory::class);
$factory->shouldReceive('make')->with($container)->once()->andReturn($this->viteManager);

Expand All @@ -125,7 +123,7 @@ public function log($level, string|Stringable $message, array $context = []): vo
});

it('turns the flag of useVite() on before it asks for a resolver', function (): void {
$this->assetManager->shouldReceive('getContainer')->andReturn(new AssetContainer('theme'));
$this->assetManager->addContainer('theme', []);
$enqueuer = ($this->make)()->container('theme');

try {
Expand Down
7 changes: 3 additions & 4 deletions tests/Unit/AssetEnqueuerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
use Pollora\Asset\Domain\Contracts\ViteManagerFactory;
use Pollora\Asset\Domain\Contracts\ViteManagerInterface;
use Pollora\Asset\Domain\Exceptions\AssetException;
use Pollora\Asset\Domain\Models\AssetContainer;
use Pollora\Asset\Infrastructure\Services\AssetEnqueuer;
use Pollora\Hook\Domain\Contract\Action as HookAction;
use Pollora\Hook\Domain\Contract\Filter as HookFilter;

beforeEach(function (): void {
$this->assetManager = Mockery::mock(AssetManager::class);
$this->assetManager = new AssetManager(emptyRetrievalService());

// Hook registration is mocked, so the deferred enqueue of a destroyed builder cannot fail
$this->hookAction = Mockery::mock(HookAction::class);
Expand Down Expand Up @@ -248,8 +247,8 @@ public function isCommandLine(): bool
// Not the command line: useVite() acts
$this->commandLine = false;

$this->container = new AssetContainer('theme', ['hot_file' => '/tmp/none.hot', 'build_directory' => 'build/theme/x']);
$this->assetManager->shouldReceive('getContainer')->with('theme')->andReturn($this->container);
$this->assetManager->addContainer('theme', ['hot_file' => '/tmp/none.hot', 'build_directory' => 'build/theme/x']);
$this->container = $this->assetManager->getContainer('theme');
});

it('asks the factory for the resolver of the container it was given', function (): void {
Expand Down
15 changes: 7 additions & 8 deletions tests/Unit/AssetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Pollora\Asset\Application\Services\AssetManager;
use Pollora\Asset\Application\Services\AssetRetrievalService;
use Pollora\Asset\Domain\Contracts\AssetEnqueuerInterface;
use Pollora\Asset\Domain\Contracts\PublicPath;
use Pollora\Asset\Domain\Exceptions\AssetException;
Expand All @@ -25,7 +24,7 @@ public function path(string $to = ''): string

describe('AssetManager containers', function (): void {
it('lists every container, keyed by name', function (): void {
$manager = new AssetManager(Mockery::mock(AssetRetrievalService::class));
$manager = new AssetManager(emptyRetrievalService());
$manager->addContainer('theme', ['hot_file' => 'theme.hot']);
$manager->addContainer('acme-plugin', []);

Expand All @@ -37,11 +36,11 @@ public function path(string $to = ''): string
});

it('lists nothing before a container is added', function (): void {
expect((new AssetManager(Mockery::mock(AssetRetrievalService::class)))->containers())->toBe([]);
expect((new AssetManager(emptyRetrievalService()))->containers())->toBe([]);
});

it('puts the hot file of a container that gives none in the public directory', function (): void {
$manager = new AssetManager(Mockery::mock(AssetRetrievalService::class), publicPathAt('/var/www/public'));
$manager = new AssetManager(emptyRetrievalService(), publicPathAt('/var/www/public'));
$manager->addContainer('acme', []);
$manager->addContainer('theme', ['hot_file' => '/elsewhere/theme.hot']);

Expand All @@ -50,14 +49,14 @@ public function path(string $to = ''): string
});

it('leaves the hot file as the container names it when it has no public directory to ask', function (): void {
$manager = new AssetManager(Mockery::mock(AssetRetrievalService::class));
$manager = new AssetManager(emptyRetrievalService());
$manager->addContainer('acme', []);

expect($manager->getContainer('acme')->getHotFile())->toBe('acme.hot');
});

it('keeps the default container apart from the others', function (): void {
$manager = new AssetManager(Mockery::mock(AssetRetrievalService::class));
$manager = new AssetManager(emptyRetrievalService());
$manager->addContainer('root', ['hot_file' => 'hot']);
$manager->addContainer('theme', ['hot_file' => 'theme.hot']);

Expand All @@ -76,13 +75,13 @@ public function path(string $to = ''): string
$enqueuer->shouldReceive('handle')->once()->with('theme/app')->andReturnSelf();
$enqueuer->shouldReceive('path')->once()->with('app.js')->andReturnSelf();

$manager = new AssetManager(Mockery::mock(AssetRetrievalService::class), null, fn (): AssetEnqueuerInterface => $enqueuer);
$manager = new AssetManager(emptyRetrievalService(), null, fn (): AssetEnqueuerInterface => $enqueuer);

expect($manager->add('theme/app', 'app.js'))->toBe($enqueuer);
});

it('says so when the manager was built without a way to make an enqueuer', function (): void {
$manager = new AssetManager(Mockery::mock(AssetRetrievalService::class));
$manager = new AssetManager(emptyRetrievalService());

expect(fn () => $manager->add('theme/app', 'app.js'))->toThrow(AssetException::class, 'No asset enqueuer is set');
});
Expand Down
18 changes: 9 additions & 9 deletions tests/Unit/AssetResolutionBehaviourTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Brain\Monkey\Functions;
use Pollora\Asset\Application\Services\AssetFile;
use Pollora\Asset\Application\Services\AssetManager;
use Pollora\Asset\Application\Services\AssetRetrievalService;
use Pollora\Asset\Application\Services\MissingBuilds;
use Pollora\Asset\Asset;
use Pollora\Asset\Domain\Contracts\ViteManagerFactory;
Expand Down Expand Up @@ -188,11 +187,10 @@ public function log($level, string|Stringable $message, array $context = []): vo
$this->records[] = [(string) $level, (string) $message, $context];
}
};
$this->assets = Mockery::mock(AssetManager::class);
$this->assets = new AssetManager(emptyRetrievalService());
});

it('is the empty string, and says nothing, for a container nobody registered', function (): void {
$this->assets->shouldReceive('getContainer')->with('ghost')->andReturn(null);
$factory = Mockery::mock(ViteManagerFactory::class);
$factory->shouldReceive('make')->never();

Expand All @@ -201,8 +199,8 @@ public function log($level, string|Stringable $message, array $context = []): vo
});

it('is what the resolver of the container says of the file', function (): void {
$container = new AssetContainer('theme');
$this->assets->shouldReceive('getContainer')->with('theme')->andReturn($container);
$this->assets->addContainer('theme', []);
$container = $this->assets->getContainer('theme');
$vite = Mockery::mock(ViteManagerInterface::class);
$vite->shouldReceive('asset')->with('logo.svg')->andReturn('https://site.test/logo.svg');
$factory = Mockery::mock(ViteManagerFactory::class);
Expand All @@ -212,7 +210,7 @@ public function log($level, string|Stringable $message, array $context = []): vo
});

it('is the empty string and a line in the log when the resolver fails', function (): void {
$this->assets->shouldReceive('getContainer')->andReturn(new AssetContainer('theme'));
$this->assets->addContainer('theme', []);
$vite = Mockery::mock(ViteManagerInterface::class);
$vite->shouldReceive('asset')->andThrow(new AssetException('not in the manifest'));
$factory = Mockery::mock(ViteManagerFactory::class);
Expand All @@ -223,15 +221,17 @@ public function log($level, string|Stringable $message, array $context = []): vo
});

it('is the empty string when the resolver fails and nobody listens', function (): void {
$this->assets->shouldReceive('getContainer')->andThrow(new RuntimeException('boom'));
$this->assets->addContainer('theme', []);
$factory = Mockery::mock(ViteManagerFactory::class);
$factory->shouldReceive('make')->andThrow(new RuntimeException('boom'));

expect((new StandaloneAssetFileResolver($this->assets, Mockery::mock(ViteManagerFactory::class)))('logo.svg', 'theme'))->toBe('');
expect((new StandaloneAssetFileResolver($this->assets, $factory))('logo.svg', 'theme'))->toBe('');
});
});

describe('the default container of the manager', function (): void {
beforeEach(function (): void {
$this->manager = new AssetManager(Mockery::mock(AssetRetrievalService::class));
$this->manager = new AssetManager(emptyRetrievalService());
$this->manager->addContainer('', ['base_path' => 'empty']);
$this->manager->addContainer('0', ['base_path' => 'zero']);
$this->manager->addContainer('theme', ['base_path' => 'theme']);
Expand Down
Loading