Overview

Namespaces

  • MatthiasMullie
    • Minify
      • Exceptions
    • PathConverter
  • None
  • ReCaptcha
    • RequestMethod
  • TOTP

Classes

  • CSS
  • JS
  • Minify

Exceptions

  • Exception
  • Overview
  • Namespace
  • Class

Class Minify

Abstract minifier class.

Please report bugs on https://github.com/matthiasmullie/minify/issues

Direct known subclasses

MatthiasMullie\Minify\CSS, MatthiasMullie\Minify\JS
Abstract
Namespace: MatthiasMullie\Minify
Copyright: Copyright (c) 2012, Matthias Mullie. All rights reserved
License: License
Author: Matthias Mullie minify@mullie.eu
Located at Sources/minify/src/Minify.php
Methods summary
public
# __construct( )

Init the minify class - optionally, code may be passed along already.

Init the minify class - optionally, code may be passed along already.

public
# add( string|string[] $data )

Add a file or straight-up code to be minified.

Add a file or straight-up code to be minified.

Parameters

$data
public string
# minify( string[optional] $path = null )

Minify the data & (optionally) saves it to a file.

Minify the data & (optionally) saves it to a file.

Parameters

$path
Path to write the data to

Returns

string
The minified data
public string
# gzip( string[optional] $path = null, int[optional] $level = 9 )

Minify & gzip the data & (optionally) saves it to a file.

Minify & gzip the data & (optionally) saves it to a file.

Parameters

$path
Path to write the data to
$level
Compression level, from 0 to 9

Returns

string
The minified & gzipped data
public Psr\Cache\CacheItemInterface
# cache( Psr\Cache\CacheItemInterface $item )

Minify the data & write it to a CacheItemInterface object.

Minify the data & write it to a CacheItemInterface object.

Parameters

$item
Cache item to write the data to

Returns

Psr\Cache\CacheItemInterface
Cache item with the minifier data
abstract public string
# execute( string[optional] $path = null )

Minify the data.

Minify the data.

Parameters

$path
Path to write the data to

Returns

string
The minified data
protected string
# load( string $data )

Load data.

Load data.

Parameters

$data
Either a path to a file or the content itself

Returns

string
protected
# save( string $content, string $path )

Save to file.

Save to file.

Parameters

$content
The minified data
$path
The path to save the minified data to

Throws

MatthiasMullie\Minify\Exceptions\IOException
protected
# registerPattern( string $pattern, string|callable $replacement = '' )

Register a pattern to execute against the source content.

Register a pattern to execute against the source content.

Parameters

$pattern
PCRE pattern
$replacement
Replacement value for matched pattern
protected string
# replace( string $content )

We can't "just" run some regular expressions against JavaScript: it's a complex language. E.g. having an occurrence of // xyz would be a comment, unless it's used within a string. Of you could have something that looks like a 'string', but inside a comment. The only way to accurately replace these pieces is to traverse the JS one character at a time and try to find whatever starts first.

We can't "just" run some regular expressions against JavaScript: it's a complex language. E.g. having an occurrence of // xyz would be a comment, unless it's used within a string. Of you could have something that looks like a 'string', but inside a comment. The only way to accurately replace these pieces is to traverse the JS one character at a time and try to find whatever starts first.

Parameters

$content
The content to replace patterns in

Returns

string
The (manipulated) content
protected string
# replacePattern( string $pattern, string|callable $replacement, string $content )

This is where a pattern is matched against $content and the matches are replaced by their respective value. This function will be called plenty of times, where $content will always move up 1 character.

This is where a pattern is matched against $content and the matches are replaced by their respective value. This function will be called plenty of times, where $content will always move up 1 character.

Parameters

$pattern
Pattern to match
$replacement
Replacement value
$content
Content to match pattern against

Returns

string
protected
# extractStrings( string[optional] $chars = '\'"' )

Strings are a pattern we need to match, in order to ignore potential code-like content inside them, but we just want all of the string content to remain untouched.

Strings are a pattern we need to match, in order to ignore potential code-like content inside them, but we just want all of the string content to remain untouched.

This method will replace all string content with simple STRING# placeholder text, so we've rid all strings from characters that may be misinterpreted. Original string content will be saved in $this->extracted and after doing all other minifying, we can restore the original content via restoreStrings().

Parameters

$chars
protected string
# restoreExtractedData( string $content )

This method will restore all extracted data (strings, regexes) that were replaced with placeholder text in extract*(). The original content was saved in $this->extracted.

This method will restore all extracted data (strings, regexes) that were replaced with placeholder text in extract*(). The original content was saved in $this->extracted.

Parameters

$content

Returns

string
protected boolean
# canImportFile( string $path )

Check if the path is a regular file and can be read.

Check if the path is a regular file and can be read.

Parameters

$path

Returns

boolean
protected resource
# openFileForWriting( string $path )

Attempts to open file specified by $path for writing.

Attempts to open file specified by $path for writing.

Parameters

$path
The path to the file

Returns

resource
Specifier for the target file

Throws

MatthiasMullie\Minify\Exceptions\IOException
protected
# writeToFile( resource $handler, string $content, string $path = '' )

Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.

Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.

Parameters

$handler
The resource to write to
$content
The content to write
$path
The path to the file (for exception printing only)

Throws

MatthiasMullie\Minify\Exceptions\IOException
Properties summary
protected string[] $data

The data to be minified.

The data to be minified.

# array()
protected string[] $patterns

Array of patterns to match.

Array of patterns to match.

# array()
public string[] $extracted

This array will hold content of strings and regular expressions that have been extracted from the JS source code, so we can reliably match "code", without having to worry about potential "code-like" characters inside.

This array will hold content of strings and regular expressions that have been extracted from the JS source code, so we can reliably match "code", without having to worry about potential "code-like" characters inside.

# array()
API documentation generated by ApiGen