-
Notifications
You must be signed in to change notification settings - Fork 3
/
Sassify.module
179 lines (153 loc) · 5.5 KB
/
Sassify.module
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php namespace ProcessWire;
/**
* Sassify module for ProcessWire CMS
* by Rudy Affandi (2016)
* https://github.com/lesaff
* MIT License
*/
require __DIR__ . '/vendor/autoload.php';
use Leafo\ScssPhp\Compiler;
use Leafo\ScssPhp\Exception;
class Sassify extends WireData implements Module {
// Init global configuration variables.
private static $cssFilename;
private static $cssPath;
private static $cssUrl;
private static $sassPath;
private static $sassEntry;
private static $numberPrecision;
private static $outputFormat;
private static $templatePath;
/**
* ------------------------------------------------------------------------
* getModuleInfo is a module required by all modules to tell
* ProcessWire about them
* ------------------------------------------------------------------------
* @return array
*/
public static function getModuleInfo()
{
return [
'title' => "Sassify",
'version' => "0.0.6",
'summary' => "Compile SASS/SCSS/Compass and use it in your project.",
'author' => "Rudy Affandi",
'href' => "https://github.com/lesaff/ProcessWire-Sassify",
'icon' => "css3",
// use autoload if module is to be called each load, if it is only needed to setup something set to false
'autoload' => true,
'singular' => true,
'page' => [
'name' => 'sassify',
'parent' => 'setup',
'title' => 'Sassify Settings',
],
'requires' => "ProcessWire>=2.5",
];
}
/**
* Initialize module
*/
public function init()
{
// Set module config
self::$cssFilename = wire('sanitizer')->name($this->css_filename);
self::$cssPath = $this->css_path;
self::$cssUrl = $this->css_url;
self::$sassPath = $this->sass_path;
self::$sassEntry = $this->sass_entry;
self::$numberPrecision = intval($this->css_number_precision);
self::$outputFormat = $this->sass_formatter;
self::$templatePath = wire('config')->paths->templates;
// Add a hook to compile sass from admin
$this->addHookAfter('Page::render', $this, 'CompileSassFromAdmin');
}
/**
* Does the compiling magic
* @return returns true if success
*/
public static function doCompile(){
// Initialize compiler
$scss = new Compiler();
// Set output format
$scss->setFormatter(self::$outputFormat);
// Set number precision
$scss->setNumberPrecision(self::$numberPrecision);
$scss->addImportPath( self::$sassPath );
$scssIn = file_get_contents( self::$sassPath . self::$sassEntry );
$cssOut = $scss->compile($scssIn);
if ($cssOut) {
file_put_contents(self::$cssPath . self::$cssFilename, $cssOut);
return true;
} else {
return false;
}
}
/**
* Process css, scss and sass and compile result
*
* @param string $stylesheets The stylesheets
*
* @return string ( description_of_the_return_value )
*/
public static function css($stylesheets)
{
// Initialize compiler
$scss = new Compiler();
// Set output format
$scss->setFormatter(self::$outputFormat);
// Set number precision
$scss->setNumberPrecision(self::$numberPrecision);
// Iterate each stylesheet
if (! is_array($stylesheets)) {
$imports[] = '@import "' . self::$templatePath . $stylesheets . '"';
}
else {
foreach ($stylesheets as $stylesheet) {
$imports[] = '@import "' . self::$templatePath . $stylesheet . '"';
}
}
// Compile it
try {
$compiled = $scss->compile(implode(';', $imports));
} catch(Exception $e) {
$this->error('Sassify Error: ' . $e);
}
// Save to template/styles
file_put_contents(self::$cssPath . self::$cssFilename, $compiled);
// Return saved file path
return self::$cssUrl . self::$cssFilename;
}
/**
* Process all scss files into a single .css file.
* @return No return
*/
public static function sassifyAll() {
// get timestamp of most recently modified SCSS file
$files = glob( self::$sassPath . '*' );
$times = array_map('filemtime', $files);
arsort($times);
$scss_time = current($times);
// get timestamp of CSS file
$css_file = self::$cssPath . self::$cssFilename;
// If we've already got a css file, check its time.
if (file_exists($css_file)) {
$css_time = filemtime($css_file);
// Check if the latet sass file is newer than the css file
if( !isset($css_time) || $scss_time > $css_time ) {
self::doCompile();
}
// We don't have a css file, so lets make one.
} else {
self::doCompile();
}
}
// Check if we are in the admin template and have been asked to compile
protected static function CompileSassFromAdmin($event) {
if(wire('input')->get('sass') == 'compile' AND $event->object->template == 'admin') {
self::sassifyAll();
// Redirect to module page
wire('session')->redirect($event->object->httpUrl.'edit?name='.wire('input')->get('name'));
}
}
}