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 @@ -9,6 +9,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
- `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
2 changes: 1 addition & 1 deletion src/Application/Services/AssetFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* This class extends the domain AssetFile and implements the AssetFileInterface contract.
* It resolves the public URL of an asset using the configured container and Vite integration.
*/
class AssetFile extends DomainAssetFile implements AssetFileInterface
final class AssetFile extends DomainAssetFile implements AssetFileInterface
{
/**
* What turns a file and the name of its container into a URL, set by the environment.
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Services/AssetRegistrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* This service acts as a use case boundary for asset registration, ensuring
* that assets are properly instantiated and delegated to the container manager.
*/
class AssetRegistrationService
final class AssetRegistrationService
{
/**
* Initializes the registration service with the container manager.
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Services/MissingBuilds.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* reachable: each missing build is logged once per request and listed in
* wp-admin by `MissingBuildNotice`.
*/
class MissingBuilds
final class MissingBuilds
{
public function __construct(private readonly ?LoggerInterface $logger = null) {}

Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Exceptions/AssetException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
* during asset management, such as invalid configuration, missing files,
* or unsupported asset types.
*/
class AssetException extends Exception {}
final class AssetException extends Exception {}
2 changes: 1 addition & 1 deletion src/Domain/Models/ViteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* Use for testing or pure domain logic.
*/
class ViteManager implements ViteManagerInterface
final class ViteManager implements ViteManagerInterface
{
/**
* Returns a stub asset container instance.
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Services/AssetContainerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* This service provides methods to register assets, retrieve a single asset by name,
* and fetch all registered assets via the configured repository implementation.
*/
class AssetContainerManager
final class AssetContainerManager
{
/**
* Initializes the container manager with a repository implementation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* prototyping, or scenarios where persistence is not required. Assets are
* stored in a simple associative array keyed by their name/handle.
*/
class InMemoryAssetRepository implements AssetRepositoryInterface
final class InMemoryAssetRepository implements AssetRepositoryInterface
{
/**
* Internal array of assets, keyed by asset name/handle.
Expand Down
72 changes: 36 additions & 36 deletions src/Infrastructure/Services/AssetEnqueuer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* WordPress enqueuing and Vite.js integration. It handles various asset loading scenarios
* including dependencies, versioning, and conditional loading.
*/
class AssetEnqueuer implements AssetEnqueuerInterface
final class AssetEnqueuer implements AssetEnqueuerInterface
{
/**
* Hooks on whose pages WordPress prints script modules, after its import map.
Expand All @@ -46,83 +46,83 @@ class AssetEnqueuer implements AssetEnqueuerInterface
*
* @var string|array<string>
*/
protected string|array $path;
private string|array $path;

/**
* The type of asset ('css' or 'js').
*/
protected string $type;
private string $type;

/**
* Array of asset handles that this asset depends on.
*
* @var array<string>
*/
protected array $dependencies = [];
private array $dependencies = [];

/**
* The asset container instance.
*/
protected ?AssetContainer $container = null;
private ?AssetContainer $container = null;

/**
* The Vite manager instance.
*/
protected ?ViteManagerInterface $viteManager = null;
private ?ViteManagerInterface $viteManager = null;

/**
* Whether to use Vite.js for asset handling.
*/
protected bool $useVite = false;
private bool $useVite = false;

/**
* The version string for cache busting.
*/
protected ?string $version = null;
private ?string $version = null;

/**
* The media query for stylesheet (e.g., 'all', 'print', 'screen').
*/
protected string $media = 'all';
private string $media = 'all';

/**
* Whether to load the script in the footer.
*/
protected bool $loadInFooter = false;
private bool $loadInFooter = false;

/**
* The loading strategy for scripts (e.g., 'defer', 'async').
*/
protected ?string $loadStrategy = null;
private ?string $loadStrategy = null;

/**
* The content to be added inline with the asset.
*/
protected ?string $inlineContent = null;
private ?string $inlineContent = null;

/**
* The position for inline content ('before' or 'after').
*/
protected ?string $inlinePosition = null;
private ?string $inlinePosition = null;

/**
* Array of WordPress hooks where the asset should be enqueued.
*
* @var array<string>
*/
protected array $hooks = [];
private array $hooks = [];

/**
* The asset handle (unique identifier).
*/
protected string $handle;
private string $handle;

/**
* Stored localization data to apply after enqueuing.
*
* @var array<string, array>
*/
protected array $localizationData = [];
private array $localizationData = [];

/**
* @param AssetManager $assetManager Resolves the containers by name
Expand All @@ -133,20 +133,20 @@ class AssetEnqueuer implements AssetEnqueuerInterface
* @param LoggerInterface|null $logger Where a failure of the deferred enqueue is reported
*/
public function __construct(
protected AssetManager $assetManager,
protected ?HookAction $action = null,
protected ?HookFilter $filter = null,
protected ?ViteManagerFactory $viteManagers = null,
protected ?RuntimeContext $runtime = null,
protected ?LoggerInterface $logger = null,
private readonly AssetManager $assetManager,
private readonly ?HookAction $action = null,
private readonly ?HookFilter $filter = null,
private readonly ?ViteManagerFactory $viteManagers = null,
private readonly ?RuntimeContext $runtime = null,
private readonly ?LoggerInterface $logger = null,
) {}

/**
* The hook service the enqueuer registers on.
*
* @throws AssetException When the enqueuer was built without one
*/
protected function hookAction(): HookAction
private function hookAction(): HookAction
{
return $this->action ?? throw new AssetException('No hook service is set: the enqueuer was built without an Action.');
}
Expand All @@ -156,7 +156,7 @@ protected function hookAction(): HookAction
*
* @throws AssetException When the enqueuer was built without one
*/
protected function hookFilter(): HookFilter
private function hookFilter(): HookFilter
{
return $this->filter ?? throw new AssetException('No filter service is set: the enqueuer was built without a Filter.');
}
Expand Down Expand Up @@ -418,7 +418,7 @@ public function enqueueStyleOrScript(?string $hook = null): void
* Adds a WordPress hook for asset enqueuing.
*
* @param string $hook WordPress hook name\n */
protected function addHook(string $hook): self
private function addHook(string $hook): self
{
$this->hooks[] = $hook;

Expand All @@ -430,7 +430,7 @@ protected function addHook(string $hook): self
*
* @param string $hook WordPress hook to attach the client script
*/
protected function loadViteClient(string $hook): void
private function loadViteClient(string $hook): void
{
if ($this->printsScriptModules($hook)) {
// The client is a module too: printed ahead of the import map, it would void it.
Expand Down Expand Up @@ -476,7 +476,7 @@ public function configureViteAssets(): void
*
* @return bool True if Vite client should be loaded
*/
protected function needToLoadViteClient(): bool
private function needToLoadViteClient(): bool
{
return $this->useVite && $this->viteManager instanceof ViteManagerInterface && $this->viteManager->isRunningHot();
}
Expand All @@ -486,7 +486,7 @@ protected function needToLoadViteClient(): bool
*
* @return array Array of asset paths grouped by type
*/
protected function getAssetPaths(): array
private function getAssetPaths(): array
{
if ($this->useVite && ! $this->viteManager->isRunningHot()) {

Expand All @@ -504,7 +504,7 @@ protected function getAssetPaths(): array
*
* @throws \InvalidArgumentException When asset type is not supported
*/
protected function enqueueAsset(string $type, string $path, ?string $hook = null): void
private function enqueueAsset(string $type, string $path, ?string $hook = null): void
{
$handle = $this->useVite && ! $this->viteManager->isRunningHot() ? $this->handle.'/'.sanitize_title(basename($path)) : $this->handle;
match ($type) {
Expand All @@ -521,7 +521,7 @@ protected function enqueueAsset(string $type, string $path, ?string $hook = null
*
* @param string $path Path to the JavaScript file
*/
protected function enqueueScript(string $path, string $handle): void
private function enqueueScript(string $path, string $handle): void
{
wp_enqueue_script($handle, $path, $this->dependencies, $this->version, $this->loadInFooter);

Expand All @@ -545,7 +545,7 @@ protected function enqueueScript(string $path, string $handle): void
/**
* Whether a Vite entry enqueued on this hook is printed by WordPress as a script module.
*/
protected function printsScriptModules(?string $hook): bool
private function printsScriptModules(?string $hook): bool
{
return in_array($hook, self::SCRIPT_MODULE_HOOKS, true) && function_exists('wp_enqueue_script_module');
}
Expand All @@ -558,7 +558,7 @@ protected function printsScriptModules(?string $hook): bool
* `{handle}-data`, printed in the head: it runs before the module, which the
* browser defers.
*/
protected function enqueueScriptModule(string $path, string $handle): void
private function enqueueScriptModule(string $path, string $handle): void
{
// null, not false: false appends WordPress's version, and a module is identified by its
// exact URL — a chunk importing the entry back would load a second copy of it.
Expand Down Expand Up @@ -594,7 +594,7 @@ protected function enqueueScriptModule(string $path, string $handle): void
* @param string $path Path to the CSS file
* @param string $handle Unique identifier for the stylesheet
*/
protected function enqueueStyle(string $path, string $handle): void
private function enqueueStyle(string $path, string $handle): void
{
wp_enqueue_style($handle, $path, $this->dependencies, $this->version, $this->media);
if (! in_array($this->inlineContent, [null, '', '0'], true)) {
Expand All @@ -605,7 +605,7 @@ protected function enqueueStyle(string $path, string $handle): void
/**
* Adds module and crossorigin attributes to Vite script tags.
*/
protected function addViteScriptAttributes(string $handle): void
private function addViteScriptAttributes(string $handle): void
{
$this->hookFilter()->add('script_loader_tag', fn ($tag, $scriptHandle, $src) => $scriptHandle === $handle
? '<script type="module" crossorigin src="'.esc_url($src).'"></script>'
Expand All @@ -618,7 +618,7 @@ protected function addViteScriptAttributes(string $handle): void
* @param string $path Asset path
* @return string Full URL to the asset
*/
protected function forceFullUrl(string $path): string
private function forceFullUrl(string $path): string
{
if (str_contains($path, '://')) {
return $path;
Expand All @@ -638,7 +638,7 @@ protected function forceFullUrl(string $path): string
*
* @throws \InvalidArgumentException When file type is not supported
*/
protected function determineFileType(string $path): string
private function determineFileType(string $path): string
{
$type = pathinfo($path, PATHINFO_EXTENSION);

Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/AssetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

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;
use Pollora\Asset\Domain\Models\AssetContainer;
use Pollora\Asset\Infrastructure\Services\AssetEnqueuer;

/** A public directory at a known place */
function publicPathAt(string $root): PublicPath
Expand Down Expand Up @@ -72,11 +72,11 @@ public function path(string $to = ''): string

describe('AssetManager::add()', function (): void {
it('starts the enqueuer the environment builds, with the handle and the file', function (): void {
$enqueuer = Mockery::mock(AssetEnqueuer::class);
$enqueuer = Mockery::mock(AssetEnqueuerInterface::class);
$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 (): AssetEnqueuer => $enqueuer);
$manager = new AssetManager(Mockery::mock(AssetRetrievalService::class), null, fn (): AssetEnqueuerInterface => $enqueuer);

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