Asset Component

Caches and delivers assets of every sort, from any location, with hands-off versioning. Manipulates images on-the-fly. Minifies and combines (on-demand) css and javascript files.

use BootPress\Asset\Component as Asset;

Packagist License MIT HHVM Tested PHP 7 Supported Build Status Code Climate Test Coverage


public static bool|object cached ( string $dir [, array $glide ] )

Check if the current page is a cached asset you need to $page->send().

@param $dir

The folder you want to cache all your assets in.

@param $glide

Optional parameters to use when setting up the Glide Server Factory. The only ones we'll use are:

  • 'group_cache_in_folders' => Whether to group cached images in folders
  • 'watermarks' => Watermarks filesystem
  • 'driver' => Image driver (gd or imagick)
  • 'max_image_size' => Image size limit
@return

Either false or a Symfony\Component\HttpFoundation\Response for you to send.

use BootPress\Page\Component as Page;
use BootPress\Asset\Component as Asset;

$page = Page::html();
if ($asset = Asset::cached('assets')) {
    $page->send($asset);
}

public static string|array urls ( string|array $html )

Finds all the assets in your $html, and caches them.

You only need to use this if you are not $page->display()ing the html you want to send.

@return

The $html with all of your asset links cached.

$json = array('<p>Content</p>');
$page->sendJson(Asset::urls($json));

public static object dispatch ( string $file [, string|array $options ] )

Prepares a Symfony Response for you to send.

@param $file

Either a file location, or the type of file you are sending eg. html, txt, less, scss, json, xml, rdf, rss, atom, js, css

@param $options

The string of data you want to send, or an array of options if $file is a location. The available options are:

  • (string) 'name' => Changes a downloadable asset's file name.
  • (int) 'expires' => The max_age (in seconds) to cache the file for. Defaults to 0 which indicates that it must be constantly revalidated.
  • (bool) 'xsendfile' => Whether or not the X-Sendfile-Type header should be trusted. Defaults to false.

If you are sending the content directly and want to cache it, then you can make this an array($content, 'expires' => ...).

@return

A Symfony\Component\HttpFoundation\Response for you to send.

$html = $page->display('<p>Content</p>');
$page->send(Asset::dispatch('html', $html));

public static string|array mime ( string|array $type )

Get the mime type(s) associated with a file extension.

@param $type

If this is a string then we'll give you the main mime type (for sending). If it's an array then we'll give you all of the mime types (for verifying).

@return

The mime type(s).

echo Asset::mime('html'); // text/html

echo implode(', ', Asset::mime(array('html'))); // text/html, application/xhtml+xml, text/plain
Document Your Code

Asset::cached() is a one-stop method for all of your asset caching needs. This should be the first thing that you call. It checks to see if the page is looking for a cached asset. If it is, then it will return a response that you can $page->send(). If not, then just continue on your merry way. When you $page->display() your html, it will look for all of your assets, and convert them to cached urls.

Installation

Add the following to your composer.json file.

{
    "require ": {
        "bootpress/asset": "^1.0"
    }
}

Example Usage

<?php

use BootPress\Page\Component as Page;
use BootPress\Asset\Component as Asset;

$page = Page::html();
if ($asset = Asset::cached('assets')) {
    $page->send($asset);
}

$html = $page->display('<p>Content</p>');
$page->send(Asset::dispatch('html', $html));