Minify
in package
Abstract minifier class.
Please report bugs on https://github.com/matthiasmullie/minify/issues
Tags
Table of Contents
Properties
- $data : array<string|int, string>
- The data to be minified.
- $patterns : array<string|int, string>
- Array of patterns to match.
Methods
- __construct() : mixed
- Init the minify class - optionally, code may be passed along already.
- add() : static
- Add a file or straight-up code to be minified.
- addFile() : static
- Add a file to be minified.
- cache() : CacheItemInterface
- Minify the data & write it to a CacheItemInterface object.
- execute() : string
- Minify the data.
- gzip() : string
- Minify & gzip the data & (optionally) saves it to a file.
- minify() : string
- Minify the data & (optionally) saves it to a file.
- canImportFile() : bool
- Check if the path is a regular file and can be read.
- executeReplacement() : string
- If $replacement is a callback, execute it, passing in the match data.
- extractStrings() : mixed
- 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.
- load() : string
- Load data.
- openFileForWriting() : resource
- Attempts to open file specified by $path for writing.
- registerPattern() : mixed
- Register a pattern to execute against the source content.
- replace() : string
- 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.
- restoreExtractedData() : string
- 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.
- save() : mixed
- Save to file.
- str_replace_first() : mixed
- stripMultilineComments() : mixed
- Both JS and CSS use the same form of multi-line comment, so putting the common code here.
- writeToFile() : mixed
- Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.
Properties
$data
The data to be minified.
protected
array<string|int, string>
$data
= array()
$patterns
Array of patterns to match.
protected
array<string|int, string>
$patterns
= array()
Methods
__construct()
Init the minify class - optionally, code may be passed along already.
public
__construct() : mixed
add()
Add a file or straight-up code to be minified.
public
add(string|array<string|int, string> $data) : static
Parameters
- $data : string|array<string|int, string>
Return values
staticaddFile()
Add a file to be minified.
public
addFile(string|array<string|int, string> $data) : static
Parameters
- $data : string|array<string|int, string>
Tags
Return values
staticcache()
Minify the data & write it to a CacheItemInterface object.
public
cache(CacheItemInterface $item) : CacheItemInterface
Parameters
- $item : CacheItemInterface
-
Cache item to write the data to
Return values
CacheItemInterface —Cache item with the minifier data
execute()
Minify the data.
public
abstract execute([mixed $path = null ]) : string
Parameters
- $path : mixed = null
Return values
string —The minified data
gzip()
Minify & gzip the data & (optionally) saves it to a file.
public
gzip([mixed $path = null ][, mixed $level = 9 ]) : string
Parameters
- $path : mixed = null
- $level : mixed = 9
Return values
string —The minified & gzipped data
minify()
Minify the data & (optionally) saves it to a file.
public
minify([mixed $path = null ]) : string
Parameters
- $path : mixed = null
Return values
string —The minified data
canImportFile()
Check if the path is a regular file and can be read.
protected
canImportFile(string $path) : bool
Parameters
- $path : string
Return values
boolexecuteReplacement()
If $replacement is a callback, execute it, passing in the match data.
protected
executeReplacement(string|callable $replacement, array<string|int, mixed> $match) : string
If it's a string, just pass it through.
Parameters
- $replacement : string|callable
-
Replacement value
- $match : array<string|int, mixed>
-
Match data, in PREG_OFFSET_CAPTURE form
Return values
stringextractStrings()
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.
protected
extractStrings([mixed $chars = ''"' ][, mixed $placeholderPrefix = '' ]) : mixed
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 : mixed = ''"'
- $placeholderPrefix : mixed = ''
load()
Load data.
protected
load(string $data) : string
Parameters
- $data : string
-
Either a path to a file or the content itself
Return values
stringopenFileForWriting()
Attempts to open file specified by $path for writing.
protected
openFileForWriting(string $path) : resource
Parameters
- $path : string
-
The path to the file
Tags
Return values
resource —Specifier for the target file
registerPattern()
Register a pattern to execute against the source content.
protected
registerPattern(string $pattern[, string|callable $replacement = '' ]) : mixed
If $replacement is a string, it must be plain text. Placeholders like $1 or \2 don't work. If you need that functionality, use a callback instead.
Parameters
- $pattern : string
-
PCRE pattern
- $replacement : string|callable = ''
-
Replacement value for matched pattern
replace()
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.
protected
replace(string $content) : string
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 : string
-
The content to replace patterns in
Return values
string —The (manipulated) content
restoreExtractedData()
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.
protected
restoreExtractedData(string $content) : string
Parameters
- $content : string
Return values
stringsave()
Save to file.
protected
save(string $content, string $path) : mixed
Parameters
- $content : string
-
The minified data
- $path : string
-
The path to save the minified data to
Tags
str_replace_first()
protected
static str_replace_first(mixed $search, mixed $replace, mixed $subject) : mixed
Parameters
- $search : mixed
- $replace : mixed
- $subject : mixed
stripMultilineComments()
Both JS and CSS use the same form of multi-line comment, so putting the common code here.
protected
stripMultilineComments() : mixed
writeToFile()
Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.
protected
writeToFile(resource $handler, string $content[, string $path = '' ]) : mixed
Parameters
- $handler : resource
-
The resource to write to
- $content : string
-
The content to write
- $path : string = ''
-
The path to the file (for exception printing only)