2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * JS and CSS compression.
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();
28 * Collection of JS and CSS compression methods.
34 * @param string $content
35 * @return string minified JS code
37 public static function js($content) {
39 $minifier = new MatthiasMullie\Minify\
JS($content);
40 return $minifier->minify();
41 } catch (Exception
$e) {
43 $error = $e->getMessage();
48 try {console.log('Error: Minimisation of JavaScript failed!');} catch (e) {}
51 // Problem detected during JavaScript minimisation, please review the following code
52 // =================================================================================
57 return $return.$content;
64 * @return string minified JS code
66 public static function js_files(array $files) {
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";
78 $compressed[] = self
::js($content);
81 return implode(";\n", $compressed);
87 * @param string $content
88 * @return string minified CSS
90 public static function css($content) {
93 $minifier = new MatthiasMullie\Minify\
CSS($content);
94 return $minifier->minify();
95 } catch (Exception
$e) {
96 $error = $e->getMessage();
102 /* Problem detected during CSS minimisation, please review the following code */
103 /* ========================================================================== */
108 return $return.$content;
114 * @param array $files
115 * @return string minified CSS code
117 public static function css_files(array $files) {
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";
129 $compressed[] = self
::css($content);
132 return implode("\n", $compressed);