inteve / assets-manager
Assets manager for PHP apps.
Fund package maintenance!
Other
Installs: 6 385
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.6.0
- czproject/assert: ^1.3
- czproject/dependency-php: ^2.0
- nette/utils: ^2.4 || ^3.0
Requires (Dev)
- nette/bootstrap: ^2.4 || ^3.0
- nette/caching: ^2.4 || ^3.0
- nette/di: ^2.4 || ^3.0
- nette/tester: ^2.0
README
Assets manager for PHP apps.
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/