From 4c1cac084c475a02192fc51ed91d9b2212229a33 Mon Sep 17 00:00:00 2001 From: Pollora maintainer Date: Mon, 23 Nov 2026 09:40:36 +0100 Subject: [PATCH] refactor!: make AssetManager, AssetRetrievalService and AssetContainer final (CONV-08) --- CHANGELOG.md | 1 + src/Application/Services/AssetManager.php | 6 +++--- .../Services/AssetRetrievalService.php | 4 ++-- src/Domain/Models/AssetContainer.php | 14 +++++++------- tests/Pest.php | 13 +++++++++++++ tests/Unit/AssetEnqueuerBehaviourTest.php | 14 ++++++-------- tests/Unit/AssetEnqueuerTest.php | 7 +++---- tests/Unit/AssetManagerTest.php | 15 +++++++-------- tests/Unit/AssetResolutionBehaviourTest.php | 18 +++++++++--------- 9 files changed, 51 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab0e4f3..eba546e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Application/Services/AssetManager.php b/src/Application/Services/AssetManager.php index c8f8234..bceeaa4 100755 --- a/src/Application/Services/AssetManager.php +++ b/src/Application/Services/AssetManager.php @@ -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 */ - 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. diff --git a/src/Application/Services/AssetRetrievalService.php b/src/Application/Services/AssetRetrievalService.php index 1e72aec..da1a5cf 100755 --- a/src/Application/Services/AssetRetrievalService.php +++ b/src/Application/Services/AssetRetrievalService.php @@ -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. diff --git a/src/Domain/Models/AssetContainer.php b/src/Domain/Models/AssetContainer.php index 2e12f04..87538bb 100755 --- a/src/Domain/Models/AssetContainer.php +++ b/src/Domain/Models/AssetContainer.php @@ -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. @@ -26,27 +26,27 @@ 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. @@ -54,7 +54,7 @@ class AssetContainer * @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; diff --git a/tests/Pest.php b/tests/Pest.php index 7862f1b..5035e73 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -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; /* |-------------------------------------------------------------------------- @@ -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)); +} diff --git a/tests/Unit/AssetEnqueuerBehaviourTest.php b/tests/Unit/AssetEnqueuerBehaviourTest.php index 0990e5d..270299c 100644 --- a/tests/Unit/AssetEnqueuerBehaviourTest.php +++ b/tests/Unit/AssetEnqueuerBehaviourTest.php @@ -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 { @@ -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'); @@ -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()."); }); @@ -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); @@ -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 { diff --git a/tests/Unit/AssetEnqueuerTest.php b/tests/Unit/AssetEnqueuerTest.php index 8c0972f..56cc114 100644 --- a/tests/Unit/AssetEnqueuerTest.php +++ b/tests/Unit/AssetEnqueuerTest.php @@ -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); @@ -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 { diff --git a/tests/Unit/AssetManagerTest.php b/tests/Unit/AssetManagerTest.php index 85d8145..aef00fb 100644 --- a/tests/Unit/AssetManagerTest.php +++ b/tests/Unit/AssetManagerTest.php @@ -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; @@ -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', []); @@ -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']); @@ -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']); @@ -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'); }); diff --git a/tests/Unit/AssetResolutionBehaviourTest.php b/tests/Unit/AssetResolutionBehaviourTest.php index 59a45fd..b92ad4a 100644 --- a/tests/Unit/AssetResolutionBehaviourTest.php +++ b/tests/Unit/AssetResolutionBehaviourTest.php @@ -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; @@ -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(); @@ -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); @@ -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); @@ -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']);