forked from panique/php-sass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sass-compiler.php
123 lines (112 loc) · 4.27 KB
/
sass-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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* Class SassCompiler
*
* This simple tool compiles all .scss files in folder A to .css files (with exactly the same name) into folder B.
* Everything happens right when you run your app, on-the-fly, in pure PHP. No Ruby needed, no configuration needed.
*
* SassWatcher is not a standalone compiler, it's just a little method that uses the excellent scssphp compiler written
* by Leaf Corcoran (https://twitter.com/moonscript), which can be found here: http://leafo.net/scssphp/ and adds
* automatic compiling to it.
*
* The currently supported version of SCSS syntax is 3.2.12, which is the latest one.
* To avoid confusion: SASS is the name of the language itself, and also the "name" of the "first" version of the
* syntax (which was quite different than CSS). Then SASS's syntax was changed to "SCSS", which is more like CSS, but
* with awesome additional possibilities and features.
*
* The compiler uses the SCSS syntax, which is recommended and mostly used. The old SASS syntax is not supported.
*
* @see SASS Wikipedia: http://en.wikipedia.org/wiki/Sass_%28stylesheet_language%29
* @see SASS Homepage: http://sass-lang.com/
* @see scssphp, the used compiler (in PHP): http://leafo.net/scssphp/
*
* Example:
*
function load_sass($assets, $target_file) {
if (getenv('ENV') != 'production') {
foreach($assets as $index => $file) {
$file_append = ($index==0) ? false : true;
SassCompiler::run($file, $target_file, "scss_formatter_compressed", $file_append);
}
return $target_file;
} else {
return $target_file;
}
}
load_sass(
[
"assets/scss/styles1.scss",
"assets/scss/styles2.scss",
...
],
"assets/css/style-join-all.css"
);
*/
class SassCompiler
{
/**
* Compiles all .scss files in a given folder into .css files in a given folder
*
* @param string $scss_folder source folder where you have your .scss files
* @param string $css_folder destination folder where you want your .css files
* @param string $format_style CSS output format, see http://leafo.net/scssphp/docs/#output_formatting for more.
*/
static public function run($scss_folder, $css_folder, $format_style = "scss_formatter", $file_append=false)
{
// scssc will be loaded automatically via Composer
$scss_compiler = new scssc();
// set the path where your _mixins are
$scss_compiler->setImportPaths($scss_folder);
// set css formatting (normal, nested or minimized), @see http://leafo.net/scssphp/docs/#output_formatting
$scss_compiler->setFormatter($format_style);
// create master css file
if(!is_dir($css_folder) && $file_append == false) {
file_put_contents($css_folder, "");
}
if(is_dir($scss_folder)) {
$scan = scandir($scss_folder);
} else {
$scan = [$scss_folder];
}
if(!file_exists($scss_folder)) {
throw new Exception("Arquivo inexistente: {$scss_folder}");
}
foreach($scan as $file) {
if(in_array($file, array(".", ".."))) continue;
if(is_dir($scss_folder)) {
if(!file_exists($css_folder.$file)) {
if(is_dir($css_folder)) {
if(!mkdir($css_folder.$file."/")) {
throw new Exception("Falha ao compilar arquivo .scss. Não foi possível criar a pasta {$css_folder}{$file}/");
}
if(!chmod($css_folder.$file, 0755)) {
throw new Exception("Falha ao compilar arquivo .scss. Não foi possível mudar a permissão do arquivo {$css_folder}{$file}/ para 755 (rwxr-xr-x)");
}
}
}
if(is_dir($css_folder)) {
self::run($scss_folder.$file."/", $css_folder.$file."/", $format_style);
} else {
self::run($scss_folder.$file, $css_folder, $format_style);
}
} else {
if(preg_match('/(.*).scss$/', $file) and $file[0]!='.') {
// get path elements from that file
$file_path_elements = pathinfo($file);
// get file's name without extension
$file_name = $file_path_elements['filename'];
// get .scss's content, put it into $string_sass
$string_sass = file_get_contents($scss_folder);
// compile this SASS code to CSS
$string_css = $scss_compiler->compile($string_sass);
// write CSS into file with the same filename, but .css extension
if(is_dir($css_folder)) {
file_put_contents($css_folder . $file_name . ".css", $string_css);
} else {
file_put_contents($css_folder, $string_css, FILE_APPEND);
}
}
}
}
}
}