Usage

This part will try to explain how to use the assets manager in your scripts.

The assets loader object

The \AssetsManager\Loader class is designed to manage a set of assets based on a table of installed packages (the json file generated during installation/update/removal processes).

The class is based on three paths:

  • base_dir: the package root directory, which must be the directory containing the composer.json file
  • assets_dir: the package asssets directory related to base_dir
  • document_root: the path in the filesystem of the web assets root directory ; this is used to build all related assets HTTP URLs.

For these three paths, the default values are defined on a default package structure:

1 package_name/
2 |----------- src/
3 |----------- www/
4 
5 $loader->base_dir = realpath(package_name)
6 $loader->assets_dir = www
7 $loader->document_root = www or the server DOCUMENT_ROOT

Additionally, a cache_dir variable is defined to store assets cache files (if necessary) and defaults to www/tmp/.

NOTE - These paths are stored in the object without the trailing slash.

Usage example

Once the install process is done, you can access any assets package or load a package's preset using the \AssetsManager\Loader object:

 1 $loader = \AssetsManager\Loader::getInstance(
 2     __DIR__.'/..',      // this is the project root directory
 3     'www',              // this is your assets root directory
 4     __DIR__             // this is your web document root
 5 );
 6 
 7 // to get a package
 8 $package = $loader->getPackage( package name );
 9 // to retrieve a package asset file URL
10 echo $package->find( file name );
11 
12 // to get a preset
13 $preset = $loader->getPreset( preset name );
14 // to write a preset dependencies
15 echo $preset->__toHtml();

As described in the Configuration page, calling a preset will automatically load its internal files (some Javascript files for instance) and its dependencies to other presets or files. The result of the __toHtml() method will then be a string to include the files and scripts definitions, fully functional and ready to be written in your HTML.

Example:

For instance, if you installed the Gentleface sprites package in its assets-installer version you will have:

1 // calling ...
2 echo $loader->getPreset('gentleface-sprites')->__toHtml();
3 // will render something like:
4 <link src="vendor/atelierspierrot/gentleface-sprites/gentleface-sprites.min.css" type="text/css" rel="stylesheet" media="all" />
 Found a typo on this page ? You can fork and edit it!