private function write_cache_tmpfile($cfile, $files, CacheRenderHandler $rh) { $tmpfile = tempnam(dirname($cfile), $cfile); $fhc = @fopen($tmpfile, 'w+'); if (!$fhc) { trigger_error("Cannot write to temporary file: {$tmpfile}", E_USER_WARNING); return null; } if ($rh->getConfig()->get('write_headers')) { $rh->writeHeader($fhc, $files); } $res = true; $merge = !!$rh->getConfig()->get('merge_tags'); if ($merge) { $rh->startWrite(); } foreach ($files as $file) { try { $rh->processFile($fhc, $file); } catch (\Exception $e) { trigger_error(sprintf("Exception %s while processing %s:\n\n%s", get_class($e), $file['file'], $e->getMessage()), E_USER_WARNING); $res = false; break; } } if ($merge) { $rh->endWrite($fhc); } fclose($fhc); return $res ? $tmpfile : null; }
private function write_cache($cfile, $files, CacheRenderHandler $rh) { $lockfile = $cfile . ".lock"; $fhl = @fopen($lockfile, 'w'); if (!$fhl) { trigger_error("Cannot create cache-lockfile: {$lockfile}", E_USER_WARNING); return false; } $wb = false; if (!@flock($fhl, LOCK_EX | LOCK_NB, $wb)) { trigger_error("Canot lock cache-lockfile: {$lockfile}", E_USER_WARNING); return false; } if ($wb) { // another process is writing the cache. Let's just return false // the caller will leave the CSS unaltered return false; } $fhc = @fopen($cfile, 'w+'); if (!$fhc) { trigger_error("Cannot open cache file: {$cfile}", E_USER_WARNING); fclose($fhl); unlink($lockfile); return false; } if ($rh->getConfig()->get('write_headers')) { $rh->writeHeader($fhc, $files); } $res = true; foreach ($files as $file) { try { $rh->processFile($fhc, $file); } catch (\Exception $e) { trigger_error(sprintf("Exception %s while processing %s:\n\n%s", get_class($e), $file['file'], $e->getMessage()), E_USER_WARNING); $res = false; break; } } $ts = time(); if (function_exists('gzencode')) { $enc = "{$cfile}.gz"; fseek($fhc, 0, SEEK_SET); file_put_contents($enc, gzencode(stream_get_contents($fhc), 9)); touch($enc, $ts); } fclose($fhc); touch($cfile, $ts); fclose($fhl); unlink($lockfile); return $res; }