rumenx / php-assets
Framework-agnostic PHP package to manage frontend assets in the backend. Integrations for Laravel and Symfony included.
v1.0.0-beta
2025-06-10 16:10 UTC
Requires
- php: >=8.3
Requires (Dev)
- illuminate/support: ^12.17
- pestphp/pest: ^2.0
- phpstan/phpstan: ^1.10
- symfony/http-kernel: ^7.3
This package is auto-updated.
Last update: 2025-06-10 19:15:57 UTC
README
Framework-agnostic PHP package to manage frontend assets in the backend. Works with plain PHP, Laravel, and Symfony (via adapters).
Features
- Add, order, and output CSS, LESS, and JS assets from PHP
- Cache busting (file or function based)
- Environment and domain support
- Laravel and Symfony integration via adapters
- 100% test coverage, static analysis, and CI
Installation
composer require rumenx/php-assets
Usage Examples
Plain PHP
use Rumenx\Assets\Asset; // Add assets Asset::add('style.css'); Asset::add('theme.less'); Asset::add('app.js'); Asset::add(['extra.js', 'extra2.js'], 'footer'); // Add inline style or script Asset::addStyle('body { background: #fafafa; }'); Asset::addScript('console.log("Hello!");'); // Output in your template Asset::css(); // <link rel="stylesheet" ...> Asset::less(); // <link rel="stylesheet/less" ...> Asset::js(); // <script src=...></script> Asset::styles(); // <style>...</style> Asset::scripts(); // <script>...</script> // Use cachebuster (file-based) Asset::setCachebuster(__DIR__.'/cache.json'); // Use cachebuster (function-based) Asset::setCacheBusterGeneratorFunction(function($file) { return md5($file); }); // Custom domain or prefix Asset::setDomain('https://cdn.example.com/'); Asset::setPrefix('X-');
Laravel Integration
-
Register the service provider in
config/app.php
:Rumenx\Assets\Laravel\AssetServiceProvider::class,
-
Use the Asset class anywhere in your app:
use Rumenx\Assets\Asset; Asset::add('main.css'); Asset::add('main.js'); // In your Blade template {!! Asset::css() !!} {!! Asset::js() !!}
-
(Optional) Bindings are available via the Laravel container:
$assets = app('assets'); $assets::add('custom.js');
Symfony Integration
-
Register the bundle in your Symfony app:
// config/bundles.php return [ Rumenx\Assets\Symfony\AssetBundle::class => ['all' => true], ];
-
Use the Asset class in your controllers or templates:
use Rumenx\Assets\Asset; Asset::add('main.css'); Asset::add('main.js'); // In a Twig template dump(Asset::css()); dump(Asset::js());
Advanced Usage
- Add assets to specific locations:
Asset::add('file.js', 'header');
// Add JS to headerAsset::addFirst('file.js');
// Add as first assetAsset::addBefore('new.js', 'old.js');
// Insert before anotherAsset::addAfter('new.js', 'old.js');
// Insert after another
- Environment detection:
Asset::$envResolver = fn() => app()->environment();
- Custom URL generator:
Asset::$urlGenerator = fn($file, $secure) => asset($file, $secure);
Testing
composer test
Static Analysis
composer analyze
CI/CD
- GitHub Actions for tests, static analysis, and Codecov coverage reporting.
License
This project is licensed under the MIT License.