]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/jsmin.php
Release 6.5.0
[Github/sugarcrm.git] / jssource / jsmin.php
1 <?php
2
3 class SugarMin {
4
5     /**
6      * jsParser will take javascript source code and minify it.
7      *
8      * Note: There is a lot of redundant code since both passes
9      * operate similarly but with slight differences. It will probably
10      * be a good idea to refactor the code at a later point when it is stable.
11      *
12      * JSParser will perform 3 passes on the code. Pass 1 takes care of single
13      * line and mult-line comments. Pass 2 performs some sanitation on each of the lines
14      * and pass 3 works on stripping out unnecessary spaces.
15      *
16      * @param string $js
17      * @param string $currentOptions
18      * @return void
19      */
20     private function __construct($text, $compression) {
21         $this->text = trim($text)."\n";
22         $this->compression = $compression;
23     }
24
25     /**
26      * Entry point function to minify javascript.
27      *
28      * @param string $js Javascript source code as a string.
29      * @param string $compression Compression option. {light, deep}.
30      * @return string $output Output javascript code as a string.
31      */
32     static public function minify($js, $compression = 'light') {
33         try {
34             $me = new SugarMin($js, $compression);
35             $output = $me->jsParser();
36
37             return $output;
38         } catch (Exception $e) {
39             // Exception handling is left up to the implementer.
40             throw $e;
41         }
42     }
43
44     protected function jsParser() {
45         require_once('jssource/Minifier.php');
46         return Minifier::minify($this->text);
47         }
48 }