コード例 #1
0
ファイル: Compiler.php プロジェクト: webiny/htpl
 /**
  * Try to retrieve the compiled template from the cache.
  *
  * @param string $templateName Template that should be retrieved.
  *
  * @return bool|Layout Compiled template in form of a string, or bool false if the template is not in cache.
  */
 private function getFromCache($templateName)
 {
     // try to get it from cache
     $cachedTemplate = $this->htpl->getCache()->read($templateName);
     if (!$cachedTemplate) {
         return false;
     }
     /**
      * @var Layout $layout
      */
     $layout = unserialize($cachedTemplate);
     // check when cache was last touched, so we don't need to revalidate all templates
     $lastTouched = $layout->getLastTouched();
     if ($lastTouched > time() - $this->htpl->getOptions()['cacheValidationTTL']) {
         return $layout;
     }
     foreach ($layout->getIncludedFiles() as $tplFile => $tplFileCreatedOn) {
         // verify if cache is still fresh, for all included templates in this layout
         if ($lastTouched < $this->htpl->getTemplateProvider()->createdOn($tplFile)) {
             $this->htpl->getCache()->delete($templateName);
             return false;
         }
     }
     // update last touched
     $layout->setLastTouched(time() + $this->htpl->getOptions()['cacheValidationTTL']);
     // cache it again
     $this->htpl->getCache()->write($templateName, serialize($layout));
     return $layout;
 }
コード例 #2
0
ファイル: HtplTest.php プロジェクト: webiny/htpl
 public function testGetCache()
 {
     $provider = new ArrayProvider(['test.htpl' => '{var}']);
     $cache = new ArrayCache();
     $htpl = new Htpl($provider, $cache);
     $this->assertSame($cache, $htpl->getCache());
 }