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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ;)