Skip to content

Commit

Permalink
cache css dependencies when merging is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
pilif committed May 27, 2015
1 parent 0f8e202 commit b37ced9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/sacy/fragment-cache.php
Expand Up @@ -7,7 +7,7 @@ class FileCache{
function __construct(){
$this->cache_dir = implode(DIRECTORY_SEPARATOR, array(
ASSET_COMPILE_OUTPUT_DIR,
'fragments'
'cache'
));
if (!is_dir($this->cache_dir)){
if (!@mkdir($this->cache_dir, 0755, true)){
Expand All @@ -26,14 +26,14 @@ function key2file($key){

function get($key){
$p = $this->key2file($key);
return file_exists($p) ? @file_get_contents($p) : null;
return file_exists($p) ? @unserialize(file_get_contents($p)) : null;
}

function set($key, $value){
$p = $this->key2file($key);
if (!@mkdir(dirname($p), 0755, true)){
throw new Exception("Failed to create fragment cache dir: $p");
}
return @file_put_contents($p, $value);
return @file_put_contents($p, serialize($value));
}
}
48 changes: 40 additions & 8 deletions src/sacy/sacy.php
Expand Up @@ -306,11 +306,11 @@ private function render_style_units($work_units, $cat){
$cs = $cat ? sprintf(' media="%s"', htmlspecialchars($c[0], ENT_QUOTES)) : '';
}
if ($work_units[0]['file']){
if ($res = $this->generate_file_cache($work_units, new CssRenderHandler($this->_cfg, $this->_source_file))){
if ($res = $this->generate_file_cache($work_units, new CssRenderHandler($this->_cfg, $this->fragment_cache, $this->_source_file))){
$res = sprintf('<link rel="stylesheet" type="text/css"%s href="%s" />'."\n", $cs, htmlspecialchars($res, ENT_QUOTES));
}
}else{
$res = $this->generate_content_cache($work_units, new CssRenderHandler($this->_cfg, $this->_source_file));
$res = $this->generate_content_cache($work_units, new CssRenderHandler($this->_cfg, $this->fragment_cache, $this->_source_file));
$res = sprintf('<style type="text/css"%s>%s</style>'."\n", $cs, $res);
}
return $res;
Expand All @@ -319,12 +319,12 @@ private function render_style_units($work_units, $cat){

private function render_script_units($work_units, $cat){
if ($work_units[0]['file']){
if ($res = $this->generate_file_cache($work_units, new JavaScriptRenderHandler($this->_cfg, $this->_source_file))){
if ($res = $this->generate_file_cache($work_units, new JavaScriptRenderHandler($this->_cfg, $this->fragment_cache, $this->_source_file))){
$this->rendered_bits[] = array('type' => 'file', 'src' => $res);
return sprintf('<script type="text/javascript" src="%s"></script>'."\n", htmlspecialchars($res, ENT_QUOTES));
}
}else{
$res = $this->generate_content_cache($work_units, new JavaScriptRenderHandler($this->_cfg, $this->_source_file));
$res = $this->generate_content_cache($work_units, new JavaScriptRenderHandler($this->_cfg, $this->fragment_cache, $this->_source_file));
if($res) $this->rendered_bits[] = array('type' => 'string', 'content' => $res);
return sprintf('<script type="text/javascript">%s</script>'."\n", $res);
}
Expand Down Expand Up @@ -387,8 +387,9 @@ private function generate_file_cache($work_units, CacheRenderHandler $rh){
$idents[] = array(
$f['group'], $f['file'], $f['type'], $f['tag']
);
$f['mtime'] = filemtime($f['file']);
$f['additional_files'] = $rh->getAdditionalFiles($f);
$max = max($max, filemtime($f['file']));
$max = max($max, $f['mtime']);
foreach($f['additional_files'] as $af){
$max = max($max, filemtime($af));
}
Expand Down Expand Up @@ -489,7 +490,7 @@ private function write_cache_tmpfile($cfile, $files, CacheRenderHandler $rh){
}

interface CacheRenderHandler{
function __construct(Config $cfg, $source_file);
function __construct(Config $cfg, $fragment_cache, $source_file);
function getFileExtension();
static function willTransformType($type);
function writeHeader($fh, $work_units);
Expand All @@ -504,10 +505,12 @@ function getConfig();
abstract class ConfiguredRenderHandler implements CacheRenderHandler{
private $_cfg;
private $_source_file;
private $_cache;

function __construct(Config $cfg, $source_file){
function __construct(Config $cfg, $fragment_cache, $source_file){
$this->_cfg = $cfg;
$this->_source_file = $source_file;
$this->_cache = $fragment_cache;
}

protected function getSourceFile(){
Expand All @@ -518,6 +521,13 @@ public function getConfig(){
return $this->_cfg;
}

/**
* @return FileCache
*/
protected function getCache(){
return $this->_cache;
}

static public function willTransformType($type){
return false;
}
Expand Down Expand Up @@ -605,6 +615,7 @@ function getAdditionalFiles($work_unit) {
}

class CssRenderHandler extends ConfiguredRenderHandler{
private $depcache = [];
private $to_process = [];
private $collecting = false;

Expand Down Expand Up @@ -770,8 +781,25 @@ private function find_imports($type, $file, $level){
return [];

if ($level > 10) throw new Exception("CSS Include nesting level of $level too deep");
$fh = fopen($file, 'r');

$res = [];

$normalized_file = realpath($file);
$key = md5("depcache".$normalized_file.$type.filemtime($file));

$deps = array_key_exists($key, $this->depcache)
? $this->depcache[$key]
: $this->getCache()->get($key);

if (is_array($deps)){
foreach($deps as $f){
$res[] = $f;
$res = array_merge($res, $this->find_imports($type, $f, $level));
}
return $res;
}

$fh = fopen($file, 'r');
while(false !== ($line = fgets($fh))){
if (preg_match('#^\s*$#', $line)) continue;
if (preg_match('#^\s*@import(.*)$#', $line, $matches)){
Expand All @@ -783,11 +811,15 @@ private function find_imports($type, $file, $level){
}
}
fclose($fh);

$this->depcache[$key] = $res;
$this->getCache()->set($key, $res);
return $res;
}

function getAdditionalFiles($work_unit) {
$level = 0;

return $this->find_imports($work_unit['type'], $work_unit['file'], $level);
}

Expand Down

0 comments on commit b37ced9

Please sign in to comment.