Skip to content

New "extend asset" feature - #63

Merged
Chrico merged 5 commits into
masterfrom
feature/extend-asset
Oct 30, 2025
Merged

New "extend asset" feature#63
Chrico merged 5 commits into
masterfrom
feature/extend-asset

Conversation

@Chrico

@Chrico Chrico commented Oct 29, 2025

Copy link
Copy Markdown
Member

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes/features)
  • Docs have been added/updated (for bug fixes/features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Currently, when using a Loader like the Inpsyde\Assets\Loader\WebpackManifestLoader, it is not possible to easily extend the Asset with additional data like "enqueue".

The following new methods were added the the Inpsyde\Assets\AssetManager:

  • AssetManager::extendAsset() - allows to extend the Asset with additional data before it is processed.
  • AssetManager::isAssetProcessed() - The AssetManager now keeps track of assets that are processed.

Considering we have the following manifest.json:

{
    "script.js": "/public/path/script.23dafsf2138d.js",
    "style.css": "style.23dafsf2138d.css"
}

What is the current behavior? (You can also link to an open issue here)

To extend the Assets loaded from the manifest.json we would need to do the following right now:

<?php
use Inpsyde\Assets\AssetManager;
use Inpsyde\Assets\Script;
use Inpsyde\Assets\ScriptModule;
use Inpsyde\Assets\Style;
use Inpsyde\Assets\Loader\WebpackManifestLoader;

add_action(
	AssetManager::ACTION_SETUP,
	function(AssetManager $assetManager) {
	
	    $loader = new WebpackManifestLoader();
        /** @var \Inpsyde\Assets\Asset[] $assets */
        $assets = $loader->load('manifest.json');
		
		foreach($assets as $key => $asset) {
			if($asset->handle() === 'script.js' && $asset instance of Script::class){
				$asset->canEnqueue(static fn(): bool => is_user_logged_in());
				$asset->isInFooter();
				$assets[$key] = $asset;
				continue;
			}
			if($asset->handle() === 'style.js' && $asset instance of Style::class){
				$asset->canEnqueue(false);
				$asset->withInlineStyles(['before' => ':root { --black: #000; }']);
				$assets[$key] = $asset;
				continue;
			}
		}

		$assetManager->register(...$assets);
	}
);

What is the new behavior (if this is a feature change)?

The new approach will look like the following:

<?php
use Inpsyde\Assets\AssetManager;
use Inpsyde\Assets\Script;
use Inpsyde\Assets\ScriptModule;
use Inpsyde\Assets\Style;
use Inpsyde\Assets\Loader\WebpackManifestLoader;

add_action(
	AssetManager::ACTION_SETUP,
	function(AssetManager $assetManager) {
		$assetManager->extendAsset(
			Style::class, 
			'style.css',
			[
				'inline' => ['before' => ':root { --black: #000; }']
				'enqueue' => false,
			]
		);
		$assetManager->extendAsset(
			Script::class,
			'script.js',
			[
				'enqueue' => static fn(): bool => is_user_logged_in(),
				'inFooter' => true,
			]
		);
	
		$loader = new WebpackManifestLoader();
		/** @var \Inpsyde\Assets\Asset[] $assets */
		$assets = $loader->load('manifest.json');

		$assetManager->register(...$assets);
	}
);

Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)

No.

Other information

This PR will also contain the following changes:

  • AssetManager::$assets is now an Inpsyde\Assets\AssetCollection instead of SplObjectStorage

@codecov

codecov Bot commented Oct 29, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.25000% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.50%. Comparing base (b328f2f) to head (3c07f52).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
src/AssetCollection.php 61.53% 10 Missing ⚠️
src/AssetManager.php 82.60% 8 Missing ⚠️
src/AssetFactory.php 87.50% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master      #63      +/-   ##
============================================
- Coverage     87.55%   86.50%   -1.06%     
- Complexity      293      310      +17     
============================================
  Files            26       27       +1     
  Lines           860      882      +22     
============================================
+ Hits            753      763      +10     
- Misses          107      119      +12     
Flag Coverage Δ
unittests 86.50% <76.25%> (-1.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cristianobaptista cristianobaptista left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after latest changes👍

@Chrico
Chrico merged commit f683beb into master Oct 30, 2025
10 of 12 checks passed
@Chrico
Chrico deleted the feature/extend-asset branch October 30, 2025 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants