braesident / vendor-assets
Discover and serve JavaScript, CSS, and image assets from Composer packages
Requires
- php: ^7.4 || ^8.0
- matthiasmullie/minify: ^1.3
Requires (Dev)
- braesident/filedropper: ^1.0
- friendsofphp/php-cs-fixer: ^3.2
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2026-08-17 09:17:36 UTC
README
VendorAssets discovers JavaScript, CSS, and image files from local asset folders and Composer dependencies. It can render JS/CSS tags or return asset contents for a controller/route.
The package works with:
- local files in
assets/js/**/*.js - local files in
assets/css/**/*.css - local image files in
assets/images/**/*.{jpg,jpeg,png,gif,webp,svg,ico} - matching files from Composer dependencies in
requirebelowvendor/<vendor>/<package>/**, excluding nestedvendordirectories - optional dev-only asset packages listed in
extra.vendor-assets.packages
Installation
composer require braesident/vendor-assets
JavaScript
<?php use braesident\VendorAssets\JsAsset; echo JsAsset::getScriptTags('/asset?identifier=', 'dropdown'); echo JsAsset::getAsset('dropdown.js', true);
CSS
<?php use braesident\VendorAssets\CssAsset; echo CssAsset::getStyleTags('/asset?identifier=', 'layout'); echo CssAsset::getAsset('layout.css', true);
Images
<?php use braesident\VendorAssets\ImageAsset; $urls = ImageAsset::getUrlArray('/asset?identifier=', 'logo'); header('Content-Type: '.ImageAsset::getMimeType('logo.png')); echo ImageAsset::getAsset('logo.png');
Cache Paths
By default, cache files are written into the package-local cache/ directory. In production, set a writable runtime path before reading asset contents:
<?php use braesident\VendorAssets\JsAsset; JsAsset::setCachePath(__DIR__.'/var/vendor-assets-cache'); JsAsset::setMinifiedCachePath(__DIR__.'/var/vendor-assets-cache/mini');
setCachePath() is available on JsAsset, CssAsset, and ImageAsset. setMinifiedCachePath() is available on JsAsset and CssAsset; if it is not set, minified JS/CSS uses <cache path>/mini.
Public API
JsAsset::setCachePath(string $path): voidJsAsset::setMinifiedCachePath(string $path): voidJsAsset::getAssets(): arrayJsAsset::getScriptTagArray(string $path = '', string ...$matches): arrayJsAsset::getScriptTags(string $path = '', string ...$matches): stringJsAsset::getAsset(string $path, bool $minify = false): stringCssAsset::setCachePath(string $path): voidCssAsset::setMinifiedCachePath(string $path): voidCssAsset::getAssets(): arrayCssAsset::getStyleTagArray(string $path = '', string ...$matches): arrayCssAsset::getStyleTags(string $path = '', string ...$matches): stringCssAsset::getAsset(string $path, bool $minify = false): stringImageAsset::setCachePath(string $path): voidImageAsset::getAssets(): arrayImageAsset::getUrlArray(string $path = '', string ...$matches): arrayImageAsset::getAsset(string $path): stringImageAsset::getMimeType(string $path): string
Asset Names
Asset identifiers use the file name without extension. If a Composer dependency contains an asset name that already exists, the dependency asset can appear as <vendor>.<package>.<filename>.
Path arguments support regular path prefixes like /asset and query-style prefixes like /asset?identifier=.