Last active
December 17, 2018 10:52
-
-
Save richthegeek/5865960 to your computer and use it in GitHub Desktop.
A simple caching wrapper for PHPSass - does not take into account changes in files referrred to from @include.
Assumes that the PHPSass library is located in a directory at ./phpsass
Assumes it can write to a directory called ./phpsass_cache, or create it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
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 ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.