inteve/assets-manager

Assets manager for PHP apps.

Fund package maintenance!
Other

v0.5.1 2023-07-10 19:12 UTC

This package is auto-updated.

Last update: 2024-12-13 08:43:34 UTC


README

Build Status Downloads this Month Latest Stable Version License

Assets manager for PHP apps.

Donate

Installation

Download a latest package or use Composer:

composer require inteve/assets-manager

Inteve\Assets-manager requires PHP 5.6.0 or later.

Usage

$currentEnvironment = PRODUCTION_MODE ? 'production' : 'development';
$manager = new Inteve\AssetsManager\AssetsManager($currentEnvironment, '/public/path');


// get public file path
echo $manager->getPath('css/my-file.css'); // '/public/path/css/my-file.css'
echo $manager->getPath('imgs/avatar.png'); // '/public/path/imgs/avatar.png'


// stylesheets
$manager->addStylesheet(string $file, string $environment = NULL);
$manager->addStylesheet('css/style.css');
$manager->addStylesheet('css/dev.css', 'development');

$assetFiles = $manager->getStylesheet();


// scripts
$manager->addScript(string $file, string $environment = NULL);
$manager->addScript('js/script.js');
$manager->addScript('js/prod.js', 'production');

$assetFiles = $manager->getScripts();


// critical scripts (scripts in <head> for example)
$manager->addCriticalScript(string $file, string $environment = NULL);
$manager->addCriticalScript('js/script.js');
$manager->addCriticalScript('js/prod.js', 'production');

$assetFiles = $manager->getCriticalScripts();

HTML tags

foreach ($manager->getStylesheetsTags() as $tag) {
	echo $tag;
}


foreach ($manager->getScriptsTags() as $tag) {
	echo $tag;
}


foreach ($manager->getCriticalScriptsTags() as $tag) {
	echo $tag;
}

Cache busting

$fileHashProvider = new Inteve\AssetsManager\Md5FileHashProvider(__DIR__ . '/real/path/to/assets');
$manager = new Inteve\AssetsManager\AssetsManager(
	$currentEnvironment,
	'/public/path/to/assets',
	[],
	$fileHashProvider
);

echo $manager->getPath('css/styles.css'); // prints something like '/public/path/to/assets/css/styles.ab9cd8ef76.css'

External sources

This isn't recommended usage.

$manager->addScripts('https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js');

Bundles

class JQueryBundle implements Inteve\AssetsManager\IAssetsBundle
{
	function getName()
	{
		return 'jquery';
	}


	function registerAssets(Bundle $bundle)
	{
		$bundle->addScripts('https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js');
	}
}


class ContactFormBundle implements Inteve\AssetsManager\IAssetsBundle
{
	function getName()
	{
		return 'myweb/contactForm';
	}


	function registerAssets(Bundle $bundle)
	{
		$bundle->requireBundle('jquery');
		$bundle->addScripts('components/contact-form.js');
		$bundle->addStylesheet('components/contact-form.css');
	}
}

$manager = new Inteve\AssetsManager\AssetsManager(
	$currentEnvironment,
	'/public/path/to/assets',
	[
		new JQueryBundle,
		new ContactFormBundle,
	]
);
$manager->requireBundle('myweb/contactForm');

echo implode("\n", $manager->getScriptsTags());
echo implode("\n", $manager->getStylesheetsTags());

Prints:

<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<script src="/public/path/to/assets/components/contact-form.js"></script>
<link rel="stylesheet" type="text/css" href="/public/path/to/assets/components/contact-form.css">

License: New BSD License
Author: Jan Pecha, https://www.janpecha.cz/