Source of file Engine.php
Size: 6,663 Bytes - Last Modified: 2021-12-24T06:34:06+00:00
/var/www/docs.ssmods.com/process/src/code/libraries/jsont/Engine.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 | <?php namespace Jsont; use Nette\Utils\Json; class Engine { /** @var Compiler */ protected $compiler; /** @var FileLoader */ protected $loader; /** @var string */ protected $cache_directory; protected $data_cache_directory; protected $original_base_path = null; /** @var string */ protected $base_path = '.'; /** * Renders template to output. */ public function render($file_path, $context) { $this->setBasePath(dirname($file_path)); $class = $this->getTemplateClass($file_path); if (!class_exists($class, FALSE)) { if ($this->cache_directory) { $this->loadTemplateFromCache($file_path); } else { $this->loadTemplate($file_path); } } $template = new $class(); return $template->render($context, $this); } public function setDataCacheDirectory($data_cache_directory) { $this->data_cache_directory = $data_cache_directory; } public function getDataCacheDirectory() { return $this->data_cache_directory; } public function getOriginalBasePath() { return $this->original_base_path; } public function getBasePath() { return $this->base_path; } public function setBasePath($base_path) { if (is_null($this->original_base_path)) { $this->original_base_path = $base_path; } $this->base_path = $base_path; } /** * @return string */ public function getTemplateClass($name) { return 'Template' . md5("$this->cache_directory\00$name"); } /** * @return void */ private function loadTemplateFromCache($name) { $file = $this->getCacheFile($name); $lock = NULL; if (defined('PHP_WINDOWS_VERSION_BUILD') && ($lock = @fopen("$file.lock", 'c'))) { // @ - file may not exist flock($lock, LOCK_SH); } if (!$this->isExpired($file, $name) && (@include $file) !== FALSE) { // @ - file may not exist return; } if (!is_dir($this->cache_directory)) { @mkdir($this->cache_directory); // @ - directory may already exist } $lock = $lock ?: fopen("$file.lock", 'c'); if (!$lock || !flock($lock, LOCK_EX)) { throw new \RuntimeException("Unable to acquire exclusive lock '$file.lock'."); } if (!is_file($file) || $this->isExpired($file, $name)) { $code = $this->loadTemplate($name); if (file_put_contents("$file.tmp", $code) !== strlen($code) || !rename("$file.tmp", $file)) { @unlink("$file.tmp"); // @ - file may not exist throw new \RuntimeException("Unable to create '$file'."); } } elseif ((include $file) === FALSE) { throw new \RuntimeException("Unable to load '$file'."); } flock($lock, LOCK_UN); } /** * @return string */ public function getCacheFile($name) { $file = $this->getTemplateClass($name); if (preg_match('#\b\w.{10,50}$#', $name, $m)) { $file = trim(preg_replace('#\W+#', '-', $m[0]), '-') . '-' . $file; } return $this->cache_directory . '/' . $file . '.php'; } /** * @param string * @param string * @return bool */ private function isExpired($file, $name) { return $this->getLoader()->isExpired($name, (int)@filemtime($file)); // @ - file may not exist } /** * @return FileLoader */ public function getLoader() { if (!$this->loader) { $this->loader = new FileLoader; } return $this->loader; } /** * @return self */ public function setLoader(FileLoader $loader) { $this->loader = $loader; return $this; } /** * @return string */ private function loadTemplate($name) { $code = $this->compile($name); try { if (@eval('?>' . $code) === FALSE) { // @ is escalated to exception $error = error_get_last(); throw (new CompileException('Error in template: ' . $error['message']))->setSource($code, $error['line'], $name . ' (compiled)'); } } catch (\ParseError $e) { throw (new CompileException('Error in template: ' . $e->getMessage(), 0, $e))->setSource($code, $e->getLine(), $name . ' (compiled)'); } return $code; } /** * Compiles template to PHP code. * @return string */ public function compile($name) { $this->compiler = null; $source = $this->getLoader()->getContent($name); try { $tokens = Json::decode($source);//, Json::FORCE_ARRAY); $code = $this->getCompiler() ->compile($tokens, $this->getTemplateClass($name)); } catch (\Exception $e) { if (!$e instanceof CompileException) { $e = new CompileException("Thrown exception '{$e->getMessage()}'", NULL, $e); } throw $e->setSource($source, $this->getCompiler()->getLine(), $name); } if (!preg_match('#\n|\?#', $name)) { $code = "<?php\n// source: $name\n?>" . $code; } //$code = Helpers::optimizePhp($code); return $code; } /** * @return Compiler */ public function getCompiler() { if (!$this->compiler) { $this->compiler = new Compiler; } return $this->compiler; } /** * Compiles template to cache. * @param string * @return void * @throws \LogicException */ public function warmupCache($name) { if (!$this->cache_directory) { throw new \LogicException('Path to temporary directory is not set.'); } $class = $this->getTemplateClass($name); if (!class_exists($class, FALSE)) { $this->loadTemplateFromCache($name); } } /** * Sets path to temporary directory. * @return self */ public function setCacheDirectory($path) { $this->cache_directory = $path; return $this; } public function getCacheDirectory() { return $this->cache_directory; } } |