Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/inteve/assets-manager

Assets manager for PHP apps.
https://github.com/inteve/assets-manager

assets-management nette php

Last synced: 9 days ago
JSON representation

Assets manager for PHP apps.

Awesome Lists containing this project

README

        

# AssetsManager

[![Build Status](https://github.com/inteve/assets-manager/workflows/Build/badge.svg)](https://github.com/inteve/assets-manager/actions)
[![Downloads this Month](https://img.shields.io/packagist/dm/inteve/assets-manager.svg)](https://packagist.org/packages/inteve/assets-manager)
[![Latest Stable Version](https://poser.pugx.org/inteve/assets-manager/v/stable)](https://github.com/inteve/assets-manager/releases)
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/inteve/assets-manager/blob/master/license.md)

Assets manager for PHP apps.

Donate

## Installation

[Download a latest package](https://github.com/inteve/assets-manager/releases) or use [Composer](http://getcomposer.org/):

```
composer require inteve/assets-manager
```

Inteve\Assets-manager requires PHP 5.6.0 or later.

## Usage

``` php
$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 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

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

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

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

### Cache busting

```php
$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.*

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

### Bundles

```php
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:

```html

```

------------------------------

License: [New BSD License](license.md)

Author: Jan Pecha, https://www.janpecha.cz/