Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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

Author: Matthias Mullie <minify@mullie.eu>
Copyright: Copyright (c) 2012, Matthias Mullie. All rights reserved
License: MIT License
File Size: 497 lines (16 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

Minify:: (16 methods):
  __construct()
  add()
  addFile()
  minify()
  gzip()
  cache()
  load()
  save()
  registerPattern()
  replace()
  replacePattern()
  extractStrings()
  restoreExtractedData()
  canImportFile()
  openFileForWriting()
  writeToFile()


Class: Minify  - X-Ref

Abstract minifier class.

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

__construct()   X-Ref
Init the minify class - optionally, code may be passed along already.


add($data )   X-Ref
Add a file or straight-up code to be minified.

param: string|string[] $data
return: static

addFile($data )   X-Ref
Add a file to be minified.

param: string|string[] $data
return: static

minify($path = null)   X-Ref
Minify the data & (optionally) saves it to a file.

param: string[optional] $path Path to write the data to
return: string The minified data

gzip($path = null, $level = 9)   X-Ref
Minify & gzip the data & (optionally) saves it to a file.

param: string[optional] $path  Path to write the data to
param: int[optional]    $level Compression level, from 0 to 9
return: string The minified & gzipped data

cache(CacheItemInterface $item)   X-Ref
Minify the data & write it to a CacheItemInterface object.

param: CacheItemInterface $item Cache item to write the data to
return: CacheItemInterface Cache item with the minifier data

load($data)   X-Ref
Load data.

param: string $data Either a path to a file or the content itself
return: string

save($content, $path)   X-Ref
Save to file.

param: string $content The minified data
param: string $path    The path to save the minified data to

registerPattern($pattern, $replacement = '')   X-Ref
Register a pattern to execute against the source content.

param: string          $pattern     PCRE pattern
param: string|callable $replacement Replacement value for matched pattern

replace($content)   X-Ref
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.

param: string $content The content to replace patterns in
return: string The (manipulated) content

replacePattern($pattern, $replacement, $content)   X-Ref
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.

param: string          $pattern     Pattern to match
param: string|callable $replacement Replacement value
param: string          $content     Content to match pattern against
return: string

extractStrings($chars = '\'"', $placeholderPrefix = '')   X-Ref
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().

param: string[optional] $chars
param: string[optional] $placeholderPrefix

restoreExtractedData($content)   X-Ref
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.

param: string $content
return: string

canImportFile($path)   X-Ref
Check if the path is a regular file and can be read.

param: string $path
return: bool

openFileForWriting($path)   X-Ref
Attempts to open file specified by $path for writing.

param: string $path The path to the file
return: resource Specifier for the target file

writeToFile($handler, $content, $path = '')   X-Ref
Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.

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