Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
66% | Total: 854 This week: 1 | All time: 4,087 This week: 39 |
Version | License | PHP version | Categories | |||
minifier 1.0.4 | GNU General Publi... | 5.6 | PHP 5, Text processing, Compression, P... |
Description | Author | |
This package can reduce the size of source files in PHP, CSS or JS. |
<?php
|
The Minifier package is a set of classes aimed at minifying CSS, Javascript, Php and HTML contents.
Additional langages may be supported, since the abstract Minifier base class provides some support methods for that.
All Minifier classes (CssMinifier, JavascriptMinifier, PhpMinifier and HtmlMinifier) provide the same methods to the caller, since they inherit from the abstract Minifier class.
Minifying a file is not complicated ; you can for example minify string or file contents, and get the result as a minified string contents or writing it back directly to an output file.
The following example minifies a CSS file, example.css, and outputs the minified contents on the standard output :
require ( 'CssMinifier.phpclass' ) ;
$minifier = new CssMinifier ( ) ;
echo $minifier -> Minify ( 'example.css' ) ;
Minifying a Javascript file would not require too much modifications :
require ( 'JavascriptMinifier.phpclass' ) ;
$minifier = new JavascriptMinifier ( ) ;
echo $minifier -> Minify ( 'example.css' ) ;
The examples that you will find in the examples/ directory display the contents of a minified example file. They can either be run as command-line scripts or as web pages.
In the case of a web page, you have to be aware that the minified contents will be rendered by your browser ; this means you could notice that some text disappeared, some extra newlines have been added, etc.
This is not a bug of the class, but simply a problem of rendering minified contents in a browser. To get sure, you can use the MinifyTo or MinifyFileTo methods to write the minified contents to a file, then use a text editor to view them.
These classes have been designed, of course, to minimize the amount of data to be transferred during an HTTP request. Depending on your way of coding (spacing, amount of comments, etc.), you may notice a size reduction that may range from, say, 35% to 70%.
However, if your web server is configured to gzip its output, then you may feel a great deception !
Why ? because you have to compare the size of the gzipped source file with the size of the gzipped minified version of the same source. And in this case, you will notice that you can hardly reach 25% in size reduction (and sometimes, it can be below 15%).
However, a size reduction of 15 to 25% is always a good thing when we're talking about http requests !
The second point about these classes is that they have been designed to be both general and (more or less) performant ; this is why they do not "parse" the source code ; the source code is considered to be a stream of tokens, operators, comments and spaces, all of these elements being handled by the Minifier abstract base class.
The third point is that they take the less risky actions ; none of these classes will try to gain a few more bytes if it can reveal risky. I'm thinking especially about this weird Javascript language, where a newline can be considered sometimes as a separator, and where an extraneous semicolon can lead to a syntax error. Such situations would need a parser to handle them correctly, so refer to the point 2 above.
Finally, they were also designed to be used by Css and Javascript includer classes developed in my own - and modest - framework (https://github.com/christian-vigh/thrak/tree/master/PHP/Library/Javascript), which I intend to publish one day. They are able to group JS or CSS files together in a temp file, that will be included instead of the set of corresponding source files, thus minimizing the number of required http requests.
None of the Minifier classes exhibit public properties. This section only lists public methods.
Note that the PhpMinifier class is a really simple class, since it relies on the php\_strip\_whitespace() internal PHP function to process the supplied contents.
Minifies the specified string contents and returns the minified result.
Minifies the contents of the specified file and returns the minified result.
Minifies the string specified by $contents and write them back to the $output file.
Takes the contents of the file specified by the $input parameter, minifies them, then writes them back to the file specified by $output.
Although this package has not been designed for handling languages with weird syntax, such as Forth or Brainfuck, or for languages where column positions are important (Fortran, Cobol or Rpg, to name a few), it can still be used for languages that implement a context-free grammar.
A new minifier for a language not implemented by this package should implement the following :
The JavascriptMinifier class can be used as an example for parsing C-like langages, and even SQL dialects.
The CssMinifier class can be used as an example for simple languages using simple nested constructs, and C-like comments.
The PhpMinifier class is a good example on how to implement a minifier integrated into the Minifier package, when you already have a solution for minifying source contents.
When implementing a minifier for a language not yet addressed by this package, several protected methods and properties are available to you. This section describes them.
Remember that all those methods must be called (or implemented, in the case of the MinifyData method) from your derived class.
A typical minifier should perform the following actions :
All the Setxxx() methods must be called before calling the parent constructor.
This function can be used by the MinifyData() method which must be implemented by derived classes to retrieve the next meaningful element from a source stream.
The function returns false if no more token is available from the input stream.
The parameters are the following :
This method must be implemented by derived class to implement a loop for getting the next token from the input stream using the GetNextToken() method.
You can have a look at the CssMinifier and JavascriptMinifier classes for an example on how to implement such a method.
Note that the PhpMinifier class is an exception : it implements no loop, does not use the GetNextToken() method but instead uses the PHP builtin function php\_strip\_whitespace().
This function must be called from the constructor.
It is used for declaring the various forms of comments that can be found in your source file.
A distinction has been made between single-line and multiple-line comments. For example, C-style comments can have the following forms :
Single-line comments :
// this is a single line comment
Multi-line comments :
/* this is a multiline comment */
Single-line comments should be specified as an array of strings ; for a language such as PHP, it could be :
[ '//', '#' ]
Multiline comments should be described through an array of associative arrays, which should contain the following entries :
A typical example for a language such as Javascript would be :
[
[
'start' => '/*',
'end' => '*/',
'nested' => false
]
]
Sets the continuation string, for C-like languages that accept continuation lines.
A typical example for a language such as javascript or C would be :
"\\"
Defines how quoted strings should be interpreted in the source file.
This is an array of associative arrays that contain the following entries :
A typical example for a language such as Javascript, that handles both single- and double-quotes, would be :
[
[
'quote' => '"',
'escape' => '\\',
'continuation' => "\\\n"
],
[
'quote' => "'",
'escape' => '\\',
'continuation' => "\\\n"
]
] ;
Defines what should be considered as spaces to be ignored in the source file.
The $spaces parameter is an array of strings that defines the set of recognized spaces.
The default value contains the following : space, tab, vertical tab, carriage return and unicode character #160.
Note that the newline is not included here, since it is always considered as a line break by the Minifier class.
Sets the regular expression to be used for recognizing identifiers, such as variable, constant or function names.
The $re parameter is a pcre regular expression, without delimiters and options.
Note that the call to this function is mandatory, otherwise no identifier will ever be recognized.
Defines the tokens around which spaces are to be eaten up ; this is the case for example for the relational operators, parentheses, etc.
Files (16) |
File | Role | Description | ||
---|---|---|---|---|
examples (4 directories) | ||||
CssMinifier.phpclass | Class | Class source | ||
HtmlMinifier.phpclass | Class | Minifies HTML | ||
JavascriptMinifier.phpclass | Class | Class source | ||
LICENSE | Lic. | License text | ||
Minifier.phpclass | Class | Class source | ||
NOTICE | Data | Auxiliary data | ||
PhpMinifier.phpclass | Class | Class source | ||
README.md | Doc. | Documentation |
Files (16) | / | examples | / | css |
File | Role | Description |
---|---|---|
example.css | Data | Auxiliary data |
example.php | Example | Example script |
Files (16) | / | examples | / | html |
File | Role | Description |
---|---|---|
example.html | Doc. | Documentation |
example.php | Example | Example script |
Files (16) | / | examples | / | js |
File | Role | Description |
---|---|---|
example.js | Data | Auxiliary data |
example.php | Example | Example script |
Files (16) | / | examples | / | php |
File | Role | Description |
---|---|---|
example.php | Example | Example script |
example.phpclass | Class | Class source |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
User Ratings | User Comments (1) | ||||||||||||||||||||||||||||||||||
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.