-
Notifications
You must be signed in to change notification settings - Fork 3
/
Compiler.php
72 lines (51 loc) · 1.64 KB
/
Compiler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
// TODO: Performance audit RE: concatenation of CSS in Compiler#compile.
/**
* Create a new Compiler. Requires a style object on construct.
* Then call compile. Returns true if successfully written
* to a file, false if there is an unknown error. Throws an exception
* if a known error occurred.
*/
class Compiler extends PluginObject {
private $compiled;
private $style;
private $css_text;
private $less;
private $css;
private $css_comment;
function __construct( $style, $css_text, $compress ) {
$this->style = $style;
$this->css_text = stripslashes($css_text);
$this->less = $this->style->less;
$this->css = $this->style->css;
$this->css_comment = "/*\r\nCSS compiled from the file: ".$this->less->url. "\r\n*/\r\n";
$this->compress = $compress;
}
public function is_compiled() {
return $this->compiled;
}
public function compile() {
if ($this->compress) {
$this->css_text = $this->_minify_css( $this->css_text );
}
$this->css_text = $this->_add_css_comment( $this->css_comment, $this->css_text );
return $this->_write_to_file( $this->css_text, $this->css->path );
}
private function _minify_css( $css_string ) {
require_once( "lib/YUI-CSS-compressor-PHP-port/cssmin.php" );
$compressor = new CSSmin();
return $compressor->run($css_string);
}
private function _write_to_file( $text, $file_name ) {
$file = fopen( $file_name , "w" );
if ( ! fwrite( $file, $text ) ) {
throw new Exception("Could not write to CSS file.");
}
fclose( $file );
$this->compiled = true;
}
private function _add_css_comment( $comment, $css ) {
return $comment.$css;
}
}
?>