Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

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: 501 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.

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

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

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

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

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

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

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

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

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

load($data)   X-Ref
Load data.

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

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.

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

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.

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

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.

return: string
param: string $content

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

return: bool
param: string $path

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

return: resource Specifier for the target file
param: string $path The path to the 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)