Basic usage

Minifine uses Matthias Mullie's minify library by default to minify javascript and stylesheet files.

In order to use Minifine an instance needs to be created of the main minifier. This minifier will be able to minify both javascript as well as stylesheets. To create an instance a factory can be used to set up the object using the default minifiers:

$minifier = (new \Minifine\Factory())->build('/path/to/resources');

The above creates a new minifer instance ready to be used to minify resources. The Minifine\Factory::build() method accepts two parameters:

string $basePath

The base path is a required parameter. This will be used to find the resources on disk. Under many circumstances this point to the project's document root.

bool $production

The production flag prevents minifying resources on production environments. Instead a cached files will be loaded to prevent spending resources and time on production environment. The default value is false.

When testing and developing this parameter should either be ommitted or set to false. This means the resources will be minified, but the unminified resources will still be loaded to ease development and debugging.

Minifying stylesheets

A typical <head> section in an HTML document will look something like below:

<head>
    <meta charset="UTF-8">
    <title>Page title</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/jquery.ui.min.css">
    <link rel="stylesheet" href="/css/theme.css">
    <link rel="stylesheet" href="/css/custom.css">
</head>

To use Minifine to combine and minify these stylesheet simply use the following code:

<head>
    <meta charset="UTF-8">
    <title>Page title</title>
    <?= $minifier->css(['/css/bootstrap.min.css', '/css/jquery.ui.min.css', '/css/theme.css', '/css/custom.css'], '/css/min.css'); ?>
</head>

Minifine will now combine and minify the four stylesheet and the resuling HTML will (on production environments) look like:

<head>
    <meta charset="UTF-8">
    <title>Page title</title>
    <link rel="stylesheet" href="/css/min.css">
</head>

The Minifine\Minifine::css() method used above accepts three parameters:

array $files

Contains a list of files that need to be combined and minified.

string $outputDirectory

The relative path to the directory that is used to generate the minified file in. This path is relative to the $basepath parameter used when setting op Minifine.

string $outputFile

The filename of the generated file (containing the combined and minified files). This name is used both for storing the data on the server as well as for generating the correct HTML tags.

Minifying javascript files

Combining and minifying javascript files works basically the same as for stylesheets.

A typical block of javascript files in an HTML document will look something like below:

<script src="/js/jquery-1.11.2.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/custom.js"></script>

To use Minifine to combine and minify these javascript files simply use the following code:

<?= $minifier->js(['/js/jquery-1.11.2.min.js', '/js/bootstrap.min.js', '/js/custom.js'], '/js/min.js'); ?>

Minifine will now combine and minify the three javascript files and the resuling HTML will (on production environments) look like:

<script src="/js/min.js"></script>

The Minifine\Minifine::js() method used above accepts three parameters:

array $files

Contains a list of files that need to be combined and minified.

string $outputDirectory

The relative path to the directory that is used to generate the minified file in. This path is relative to the $basepath parameter used when setting op Minifine.

string $outputFile

The filename of the generated file (containing the combined and minified files). This name is used both for storing the data on the server as well as for generating the correct HTML tags.