Merge branch 'MDL-63137-master' of git://github.com/aanabit/moodle
[moodle.git] / lib / classes / minify.php
blob6a570e440cea94ea6ef123ee5ea8c486b2b9524a
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * JS and CSS compression.
20 * @package core
21 * @copyright 2013 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Collection of JS and CSS compression methods.
30 class core_minify {
31 /**
32 * Minify JS code.
34 * @param string $content
35 * @return string minified JS code
37 public static function js($content) {
38 try {
39 $minifier = new MatthiasMullie\Minify\JS($content);
40 return $minifier->minify();
41 } catch (Exception $e) {
42 ob_end_clean();
43 $error = $e->getMessage();
46 $return = <<<EOD
48 try {console.log('Error: Minimisation of JavaScript failed!');} catch (e) {}
50 // Error: $error
51 // Problem detected during JavaScript minimisation, please review the following code
52 // =================================================================================
55 EOD;
57 return $return.$content;
60 /**
61 * Minify JS files.
63 * @param array $files
64 * @return string minified JS code
66 public static function js_files(array $files) {
67 if (empty($files)) {
68 return '';
71 $compressed = array();
72 foreach ($files as $file) {
73 $content = file_get_contents($file);
74 if ($content === false) {
75 $compressed[] = "\n\n// Cannot read JS file ".basename(dirname(dirname($file))).'/'.basename(dirname($file)).'/'.basename($file)."\n\n";
76 continue;
78 $compressed[] = self::js($content);
81 return implode(";\n", $compressed);
84 /**
85 * Minify CSS code.
87 * @param string $content
88 * @return string minified CSS
90 public static function css($content) {
91 $error = 'unknown';
92 try {
93 $minifier = new MatthiasMullie\Minify\CSS($content);
94 return $minifier->minify();
95 } catch (Exception $e) {
96 $error = $e->getMessage();
99 $return = <<<EOD
101 /* Error: $error */
102 /* Problem detected during CSS minimisation, please review the following code */
103 /* ========================================================================== */
106 EOD;
108 return $return.$content;
112 * Minify CSS files.
114 * @param array $files
115 * @return string minified CSS code
117 public static function css_files(array $files) {
118 if (empty($files)) {
119 return '';
122 $compressed = array();
123 foreach ($files as $file) {
124 $content = file_get_contents($file);
125 if ($content === false) {
126 $compressed[] = "\n\n/* Cannot read CSS file ".basename(dirname(dirname($file))).'/'.basename(dirname($file)).'/'.basename($file)."*/\n\n";
127 continue;
129 $compressed[] = self::css($content);
132 return implode("\n", $compressed);