public function exec() { timer('start', 'cache.parse'); // Recover cached headers $this->_headers = @json_decode(file_get_contents("{$this->_getCachePath()}.json"), true); // Cache specific headers $this->addHeader('LP-Cache', $this->_hash); //$this->addHeader('Expires', date('r', $this->_expire)); // Thu, 19 Nov 1981 08:52:00 GMT $this->addHeader('Pragma', 'public'); $this->addHeader('Cache-Control', 'public, max-age=' . ($this->_expire - time())); // Send headers $this->_sendHeaders(); // Optimizations: // - optimized local images, convert small images into inline base64 // - compress and combine local CSS & JS // - get local external CSS files into the HTML output // TODO: compress HTML? echo Event::filter('cache', file_get_contents($this->_getCachePath())); timer('end', 'cache.parse'); }
public static function init() { Event::register('content', function ($content) { // Var assignation $content = preg_replace_callback('/{{var ([^=}]*)=([^}]*)}}/', function ($M) { $varname = trim($M[1]); $value = trim($M[2]); if (substr($value, 0, 3) == '~~~') { $value = I18n::getSingleton()->translate(substr($value, 3)); } Mvc::getResponse()->setData($varname, $value); return ''; }, $content); // Var echo $content = preg_replace_callback('/{{var ([^}]*)}}/', function ($M) { return Mvc::getResponse()->getData(trim($M[1])); }, $content); // Translate $content = preg_replace_callback('/{{~~~([^}]*)}}/', function ($M) { return I18n::getSingleton()->translate(trim($M[1])); }, $content); // Asset URL $content = preg_replace_callback('/{{asset ([^}]*)}}/', function ($M) { return asset(ltrim(trim($M[1]), '/')); }, $content); // Page URL helper $content = preg_replace_callback('/{{url[ ]?([^}]*)}}/', function ($M) { return page_url(trim($M[1])); }, $content); // Template include $content = preg_replace_callback('/{{template ([^}]*)}}/', function ($M) { ob_start(); template(trim($M[1])); return Event::filter('content', ob_get_clean()); }, $content); // {{header Content-Type: text/plain}} $content = preg_replace_callback('/{{header ([^}]*)}}/', function ($M) { if (preg_match('/^([^:]*): (.*)$/', trim($M[1]), $L)) { Mvc::getResponse()->addHeader(trim($L[1]), trim($L[2])); } return ''; }, $content); // {{redirect template/name}} $content = preg_replace_callback('/{{redirect ([^}]*)}}/', function ($M) { Mvc::getResponse()->redirect(page_url(trim($M[1]))); return ''; }, $content); return $content; }); }
public function getHtml() { if ($this->hasCache()) { $this->_content = file_get_contents($this->getCacheFile()); } else { ob_start(); Template::parse($this->_template, $this->_data); $this->_content = Event::filter('content', ob_get_clean()); if ($this->isCacheEnabled()) { @mkdir(dirname($this->getCacheFile()), 0777, true); file_put_contents($this->getCacheFile(), $this->_content); if ($ttl = $this->getData('ttl')) { file_put_contents($this->getCacheFile() . '.expire', "{$ttl}"); } else { @unlink($this->getCacheFile() . '.expire'); } } } return $this->_content; }