-
-
Save richthegeek/5865960 to your computer and use it in GitHub Desktop.
<?php | |
function compileSass($filename, $cached = true) { | |
$cache_dir = './phpsass_cache'; | |
# if cached, ensure we can write to the cache directory | |
if ($cached) { | |
if (!file_exists($cache_dir) && !mkdir($cache_dir)) { | |
throw 'SASS cache directory does not exist and cannot be created'; | |
} | |
if (!is_writable($cache_dir)) { | |
throw 'SASS cache directory is not writable'; | |
} | |
} | |
# have to check + throw here for the hashing | |
if (!file_exists($filename)) { | |
throw 'Filename requested for compilation did not exist'; | |
} | |
# create a unique filename based on the contents of the included file | |
$contents = file_get_contents($filename); | |
$hash = md5($contents); | |
$cache_filename = $cache_dir . '/' . $hash . '_' . basename($filename); | |
# if we have a cache hit, return it now. | |
if ($cached && file_exists($cache_filename)) { | |
return file_get_contents($cache_filename); | |
} | |
# no cache hit, so compile as usual | |
require_once './phpsass/SassParser.php'; | |
$syntax = substr($file, -4, 4); | |
$options = array( | |
'style' => 'expanded', | |
'cache' => false, # yes, this is ironic | |
'syntax' => $syntax, | |
'debug' => false | |
); | |
$parser = new SassParser($options); | |
$contents = $parser->toCss($filename); | |
# save to cache | |
if ($cached) { | |
file_put_contents($cache_filename, $contents); | |
} | |
return $contents; | |
} |
<?php | |
function compileSass($filename, $cached = true) { | |
$cache_dir = './phpsass_cache'; | |
# if cached, ensure we can write to the cache directory | |
if ($cached) { | |
if (!file_exists($cache_dir) && !mkdir($cache_dir)) { | |
throw 'SASS cache directory does not exist and cannot be created'; | |
} | |
if (!is_writable($cache_dir)) { | |
throw 'SASS cache directory is not writable'; | |
} | |
} | |
# have to check + throw here for the hashing | |
if (!file_exists($filename)) { | |
throw 'Filename requested for compilation did not exist'; | |
} | |
# create a unique filename based on the contents of the included file | |
$contents = file_get_contents($filename); | |
$cache_filename = $cache_dir . '/' . basename($filename); | |
# if we have a cache hit, return it now. | |
if ($cached && file_exists($cache_filename)) { | |
$source = filemtime($filename); | |
$target = filemtime($cache_filename); | |
if ($target >= $source) { | |
return file_get_contents($cache_filename); | |
} | |
} | |
# no cache hit, so compile as usual | |
require_once './phpsass/SassParser.php'; | |
$syntax = substr($file, -4, 4); | |
$options = array( | |
'style' => 'expanded', | |
'cache' => false, # yes, this is ironic | |
'syntax' => $syntax, | |
'debug' => false | |
); | |
$parser = new SassParser($options); | |
$contents = $parser->toCss($filename); | |
# save to cache | |
if ($cached) { | |
file_put_contents($cache_filename, $contents); | |
} | |
return $contents; | |
} |
The line 24 is because i've been writing nothing but javascript for the last year, thanks for fixing it.
With regards to the duplicate files: in order to know when a cached file changes we can either use the hash or the file mod time. Really, either work fine. Using the mtime would allow the cached filename to be the same in all cases, and so there would not be any duplicate files. One possible issue is that you have two files with the same name which are compiled. With the mtime method, the cached file gets overwritten due to a name collision.
So the two choices for solving the first issue are:
- use the mtime method, but accept that name collisions will be problematic.
- use the hash method, but either accept the duplicate files or add a "delete all files ending in _$filename" loop before the file_put_contents.
I've added a second file which uses mtime instead of hashing for recompiling on save and cache naming.
Hi Rich,
sorry for waiting, i have a lot of trouble last week.
This week doesn't look better, but i taked some minutes to test your code.
Now the cache-method works, and i do a small change in https://gist.github.com/aspross/5870297.
I renamed the cache files in ".css" instead of ".sass".
I hope to find time this week to look for a solution on the include problem.
I want to generate a css file for every imported sass-file.
If you have an idea to solve it, i will be very happy.
Hi Rick,
i added a few changes on the code.
See on https://gist.github.com/aspross/5937524.
Now, i'm able to compile imports.
But my problem now, is when i do changes on an import like layout.sass,
example : @import url("/tmp/sass/layout.sass"). ( content from style.sass)
I didn't update my layout.css.
Only if my main css file called style.sass have any changes, i compiled my imports.
I make a comment on the place i think we have to check the import changes :
// Check the imports in $filename
// if changes on importfiles, replace the import files
Maybe you have any ideas to solve it.
Hi Rich,
good news, i found a solution.
You can found it on https://gist.github.com/aspross/5941082.
Now it will recompile the sass files, if i make any changes on an import sass file.
I know i can probably improve the code, but for now i think it's ok.
Me and my cousin have to test this script to find eventually errors.
Maybe you can test it too and give some feedback ;)
Hi Rich,
i'm the cousin of Tobias who send you a mail yesterday about sass.
First of all, thank you for your help.
We tested your code a few minutes ago, and you can check our modifications here https://gist.github.com/aspross/5870297.
The code works, but we correct line number 24, we replaced "+" by "."
Sass files were save correctly, but after modifications on the source sass file, the old "compiled" cache-file still exists and a new one was created.
The solution we want, is that only one cached file exist per sass source-file.
The other problem ist your issue with the @import, it's a function we really need. It would be really nice if there is a chance to get it work.
Your tip is already safe, but if we get these problems solved, it will be so much greater ;-)