Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
MatthiasMullie\Minify\Minify Class Reference

Abstract minifier class. More...

Public Member Functions

 __construct ()
 Init the minify class - optionally, code may be passed along already.
 
 add ($data)
 Add a file or straight-up code to be minified.
 
 addFile ($data)
 Add a file to be minified.
 
 cache (CacheItemInterface $item)
 Minify the data & write it to a CacheItemInterface object.
 
 execute ($path=null)
 Minify the data.
 
 gzip ($path=null, $level=9)
 Minify & gzip the data & (optionally) saves it to a file.
 
 minify ($path=null)
 Minify the data & (optionally) saves it to a file.
 

Public Attributes

string[] $extracted = array()
 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.
 

Protected Member Functions

 canImportFile ($path)
 Check if the path is a regular file and can be read.
 
 executeReplacement ($replacement, $match)
 If $replacement is a callback, execute it, passing in the match data.
 
 extractStrings ($chars='\'"', $placeholderPrefix = '')
 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 ($data)
 Load data.
 
 openFileForWriting ($path)
 Attempts to open file specified by $path for writing.
 
 registerPattern ($pattern, $replacement='')
 Register a pattern to execute against the source content.
 
 replace ($content)
 We can't "just" run some regular expressions against JavaScript: it's a complex language.
 
 restoreExtractedData ($content)
 This method will restore all extracted data (strings, regexes) that were replaced with placeholder text in extract*().
 
 save ($content, $path)
 Save to file.
 
 stripMultilineComments ()
 Both JS and CSS use the same form of multi-line comment, so putting the common code here.
 
 writeToFile ($handler, $content, $path='')
 Attempts to write $content to the file specified by $handler.
 

Static Protected Member Functions

static str_replace_first ($search, $replace, $subject)
 

Protected Attributes

string[] $data = array()
 The data to be minified.
 
string[] $patterns = array()
 Array of patterns to match.
 

Detailed Description

Abstract minifier class.

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

Author
Matthias Mullie minif.nosp@m.y@mu.nosp@m.llie..nosp@m.eu
License
MIT License

Member Function Documentation

◆ add()

MatthiasMullie\Minify\Minify::add ( $data)

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

Parameters
string | string[]$data
Return values
static

◆ addFile()

MatthiasMullie\Minify\Minify::addFile ( $data)

Add a file to be minified.

Parameters
string | string[]$data
Return values
static
Exceptions
IOException

◆ cache()

MatthiasMullie\Minify\Minify::cache ( CacheItemInterface $item)

Minify the data & write it to a CacheItemInterface object.

Parameters
CacheItemInterface$itemCache item to write the data to
Return values
CacheItemInterfaceCache item with the minifier data

◆ canImportFile()

MatthiasMullie\Minify\Minify::canImportFile ( $path)
protected

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

Parameters
string$path
Return values
bool

◆ execute()

MatthiasMullie\Minify\Minify::execute ( $path = null)
abstract

Minify the data.

Parameters
string[optional]$path Path to write the data to
Return values
stringThe minified data

◆ executeReplacement()

MatthiasMullie\Minify\Minify::executeReplacement ( $replacement,
$match )
protected

If $replacement is a callback, execute it, passing in the match data.

If it's a string, just pass it through.

Parameters
string | callable$replacementReplacement value
array$matchMatch data, in PREG_OFFSET_CAPTURE form
Return values
string

◆ extractStrings()

MatthiasMullie\Minify\Minify::extractStrings ( )
protected

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
string[optional]$chars
string[optional]$placeholderPrefix

◆ gzip()

MatthiasMullie\Minify\Minify::gzip ( $path = null,
$level = 9 )

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

Parameters
string[optional]$path Path to write the data to
int[optional]$level Compression level, from 0 to 9
Return values
stringThe minified & gzipped data

◆ load()

MatthiasMullie\Minify\Minify::load ( $data)
protected

Load data.

Parameters
string$dataEither a path to a file or the content itself
Return values
string

◆ minify()

MatthiasMullie\Minify\Minify::minify ( $path = null)

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

Parameters
string[optional]$path Path to write the data to
Return values
stringThe minified data

◆ openFileForWriting()

MatthiasMullie\Minify\Minify::openFileForWriting ( $path)
protected

Attempts to open file specified by $path for writing.

Parameters
string$pathThe path to the file
Return values
resourceSpecifier for the target file
Exceptions
IOException

◆ registerPattern()

MatthiasMullie\Minify\Minify::registerPattern ( $pattern,
$replacement = '' )
protected

Register a pattern to execute against the source content.

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
string$patternPCRE pattern
string | callable$replacementReplacement value for matched pattern

◆ replace()

MatthiasMullie\Minify\Minify::replace ( $content)
protected

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
string$contentThe content to replace patterns in
Return values
stringThe (manipulated) content

◆ restoreExtractedData()

MatthiasMullie\Minify\Minify::restoreExtractedData ( $content)
protected

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
string$content
Return values
string

◆ save()

MatthiasMullie\Minify\Minify::save ( $content,
$path )
protected

Save to file.

Parameters
string$contentThe minified data
string$pathThe path to save the minified data to
Exceptions
IOException

◆ writeToFile()

MatthiasMullie\Minify\Minify::writeToFile ( $handler,
$content,
$path = '' )
protected

Attempts to write $content to the file specified by $handler.

$path is used for printing exceptions.

Parameters
resource$handlerThe resource to write to
string$contentThe content to write
string$pathThe path to the file (for exception printing only)
Exceptions
IOException

The documentation for this class was generated from the following file: