weekly release 3.1dev
[moodle.git] / lib / classes / minify.php
CommitLineData
6b32d6bc
PS
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.
13//
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/>.
16
17/**
18 * JS and CSS compression.
19 *
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
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Collection of JS and CSS compression methods.
29 */
30class core_minify {
31 /**
32 * Minify JS code.
33 *
34 * @param string $content
35 * @return string minified JS code
36 */
37 public static function js($content) {
38 global $CFG;
39 require_once("$CFG->libdir/minify/lib/JSMinPlus.php");
40
41 try {
42 ob_start(); // JSMinPlus just echos errors, weird...
43 $compressed = JSMinPlus::minify($content);
44 if ($compressed !== false) {
45 ob_end_clean();
46 return $compressed;
47 }
48 $error = ob_get_clean();
49
50 } catch (Exception $e) {
51 ob_end_clean();
52 $error = $e->getMessage();
53 }
54
55 $return = <<<EOD
56
57try {console.log('Error: Minimisation of JavaScript failed!');} catch (e) {}
58
59// Error: $error
60// Problem detected during JavaScript minimisation, please review the following code
61// =================================================================================
62
63
64EOD;
65
66 return $return.$content;
67 }
68
69 /**
70 * Minify JS files.
71 *
72 * @param array $files
73 * @return string minified JS code
74 */
75 public static function js_files(array $files) {
76 if (empty($files)) {
77 return '';
78 }
79
80 $compressed = array();
81 foreach ($files as $file) {
82 $content = file_get_contents($file);
83 if ($content === false) {
84 $compressed[] = "\n\n// Cannot read JS file ".basename(dirname(dirname($file))).'/'.basename(dirname($file)).'/'.basename($file)."\n\n";
85 continue;
86 }
87 $compressed[] = self::js($content);
88 }
89
19d97d6b 90 return implode(";\n", $compressed);
6b32d6bc
PS
91 }
92
93 /**
94 * Minify CSS code.
95 *
96 * @param string $content
97 * @return string minified CSS
98 */
99 public static function css($content) {
100 global $CFG;
101 require_once("$CFG->libdir/minify/lib/Minify/CSS/Compressor.php");
102
103 $error = 'unknown';
104 try {
105 $compressed = Minify_CSS_Compressor::process($content);
106 if ($compressed !== false) {
107 return $compressed;
108 }
109
110 } catch (Exception $e) {
111 $error = $e->getMessage();
112 }
113
114 $return = <<<EOD
115
116/* Error: $error */
117/* Problem detected during CSS minimisation, please review the following code */
118/* ========================================================================== */
119
120
121EOD;
122
123 return $return.$content;
124 }
125
126 /**
127 * Minify CSS files.
128 *
129 * @param array $files
130 * @return string minified CSS code
131 */
132 public static function css_files(array $files) {
133 if (empty($files)) {
134 return '';
135 }
136
137 $compressed = array();
138 foreach ($files as $file) {
139 $content = file_get_contents($file);
140 if ($content === false) {
141 $compressed[] = "\n\n/* Cannot read CSS file ".basename(dirname(dirname($file))).'/'.basename(dirname($file)).'/'.basename($file)."*/\n\n";
142 continue;
143 }
144 $compressed[] = self::css($content);
145 }
146
147 return implode("\n", $compressed);
148 }
149}