2017 © Pedro Peláez
 

library assetic-extensions

Improved caching for Assetic

image

jasny/assetic-extensions

Improved caching for Assetic

  • Monday, June 5, 2017
  • by jasny
  • Repository
  • 0 Watchers
  • 0 Stars
  • 964 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 21 % Grown

The README.md

Jasny Assetic extensions

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight Packagist Stable Version Packagist License, (*1)

Improved caching for Assetic., (*2)

Installation

Jasny's Twig Extensions can be easily installed using composer, (*3)

composer require jasny/assetic-extensions

Internal caching when using a factory

The AssetCacheWorker wraps each asset in a asset in an AssetCache object. This enables the use of asset caching when using a factory., (*4)

use Assetic\Factory\AssetFactory;
use Jasny\Assetic\AssetCacheWorker;

$factory = new AssetFactory('/path/to/asset/directory/');
$factory->setAssetManager($am);
$factory->setFilterManager($fm);

$factory->addWorker(new AssetCacheWorker(
    new FilesystemCache('/path/to/cache')
));

Versioning assets

The AssetVersionWorker add a version number to each generated assets. This works well on a production environment, preventing the need of removing, checking or overwriting the asset files., (*5)

If the output file is set to all.css and version is set to 1.3.7, the output file will be named all-1.3.7.css., (*6)

use Assetic\Factory\AssetFactory;
use Jasny\Assetic\AssetVersionWorker;

$factory = new AssetFactory('/path/to/asset/directory/');
$factory->setAssetManager($am);
$factory->setFilterManager($fm);

$factory->addWorker(new AssetVersionWorker($version));

Caching when using Twig

With the example code from the Assetic readme, each template is parsed on each request. This considerably slows down your application. The TwigCachingFormulaLoader using Twig cache to store the assetic formulae is finds in each template. The formula loader uses the cache and auto_reload options of the Twig environment., (*7)

The PersistentAssetWriter is an asset writer with an overwrite option. When overwrite is disabled, existing assets are not overwritten. This can speed up your production environment. It's recommended to add a version number in the output files, either manually or by using the AssetVersionWorker., (*8)

use Jasny\Assetic\PersistentAssetWriter;
use Jasny\Assetic\TwigCachingFormulaLoader;
use Assetic\Extension\Twig\TwigResource;
use Assetic\Factory\LazyAssetManager;

$twigLoader = new Twig_Loader_Filesystem('/path/to/views');
$twig = new Twig_Environment($twigLoader, ['cache' => '/path/to/cache', 'auto_reload' => true]);

$am = new LazyAssetManager($factory);

// enable loading assets from twig templates, caching the formulae
$am->setLoader('twig', new TwigCachingFormulaLoader($twig));

// loop through all your templates
foreach ($templates as $template) {
    $resource = new TwigResource($twigLoader, $template);
    $am->addResource($resource, 'twig');
}

$writer = new PersistentAssetWriter('/path/to/web');
$writer->writeManagerAssets($am);

The Versions

05/06 2017

dev-master

9999999-dev

Improved caching for Assetic

  Sources   Download

MIT

The Requires

 

The Development Requires

05/06 2017

v1.0.0

1.0.0.0

Improved caching for Assetic

  Sources   Download

MIT

The Requires

 

The Development Requires