-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpsass-compile.php
113 lines (106 loc) · 3.54 KB
/
phpsass-compile.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
<?php
/**
* This file is intended to be used in conjunction with Apache2's mod_actions,
* wherein you can have a .htaccess file like so for automatic compilation:
* Action compile-sass /git/phpsass/compile-apache.php
* AddHandler compile-sass .sass .scss
*/
header('X-Powered-By: phpsass');
header('Content-type: text/css');
$file_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$cache_path = __DIR__ . '/css/cache/';
$css_file = rtrim($_SERVER['DOCUMENT_ROOT'], '/') . $file_path;
$syntax = strtolower(substr($css_file, -4, 4));
if( $syntax != 'scss' && $syntax != 'sass' ){
exit();
}
$style = isset($_GET['style']) ? $_GET['style'] : 'expanded';
if(!is_string($style) || !in_array($style, array('expanded', 'nested', 'compact', 'compressed'))){
$style = 'expanded';
}
$cache_file = $cache_path . '/' . md5($file_path) . ".{$syntax}.{$style}.css";
$options = array(
'style' => $style,
'cache' => TRUE,
'syntax' => $syntax,
'debug' => TRUE,
'line_numbers' => TRUE,
'callbacks' => array(
'warn' => 'warn',
'debug' => 'debug'
),
'load_path_functions' => array('loadCallback'),
'load_paths' => array(dirname($css_file)),
'functions' => getFunctions(array('Compass')),
'extensions' => array('Compass')
);
// Execute the compiler.
try {
if( $options['cache'] ){
if( file_modify_time($css_file) > file_modify_time($cache_file) ){
include './phpsass/SassParser.php';
$parser = new SassParser($options);
$css = $parser->toCss($css_file);
file_put_contents($cache_file, $css);
echo $css;
}else{
echo file_get_contents($cache_file);
}
}else{
include './phpsass/SassParser.php';
$parser = new SassParser($options);
echo $parser->toCss($css_file);
}
} catch (Exception $e) {
echo $e->getMessage();
}
function file_modify_time($file_path)
{
return file_exists($file_path) ? max( @filectime($file_path), @filemtime($file_path) ) : -1;
}
function warn($text, $context)
{
print "/** WARN: $text, on line {$context->node->token->line} of {$context->node->token->filename} **/\n";
}
function debug($text, $context)
{
print "/** DEBUG: $text, on line {$context->node->token->line} of {$context->node->token->filename} **/\n";
}
function loadCallback($file, $parser)
{
$paths = array();
foreach ($parser->extensions as $extensionName) {
$namespace = ucwords(preg_replace('/[^0-9a-z]+/', '_', strtolower($extensionName)));
$extensionPath = './phpsass/Extensions/' . $namespace . '/' . $namespace . '.php';
if (file_exists($extensionPath)) {
require_once($extensionPath);
$hook = $namespace . '::resolveExtensionPath';
$returnPath = call_user_func($hook, $file, $parser);
if (!empty($returnPath)) {
$paths[] = $returnPath;
}
}
}
return $paths;
}
function getFunctions($extensions)
{
$output = array();
if (!empty($extensions)) {
foreach ($extensions as $extension) {
$name = explode('/', $extension, 2);
$namespace = ucwords(preg_replace('/[^0-9a-z]+/', '_', strtolower(array_shift($name))));
$extensionPath = './phpsass/Extensions/' . $namespace . '/' . $namespace . '.php';
if (file_exists(
$extensionPath
)
) {
require_once($extensionPath);
$namespace = $namespace . '::';
$function = 'getFunctions';
$output = array_merge($output, call_user_func($namespace . $function, $namespace));
}
}
}
return $output;
}