/** * Compiles template to PHP code. * @return string */ public function compile($name) { foreach ($this->onCompile ?: [] as $cb) { call_user_func(Helpers::checkCallback($cb), $this); } $this->onCompile = []; $source = $this->getLoader()->getContent($name); try { $tokens = $this->getParser()->setContentType($this->contentType)->parse($source); $code = $this->getCompiler()->setContentType($this->contentType)->compile($tokens, $this->getTemplateClass($name)); } catch (\Exception $e) { if (!$e instanceof CompileException) { $e = new CompileException("Thrown exception '{$e->getMessage()}'", NULL, $e); } $line = isset($tokens) ? $this->getCompiler()->getLine() : $this->getParser()->getLine(); throw $e->setSource($source, $line, $name); } if (!preg_match('#\\n|\\?#', $name)) { $code = "<?php\n// source: {$name}\n?>" . $code; } $code = Helpers::optimizePhp($code); return $code; }
/** * @return string */ private function loadTemplate($name) { $code = $this->compile($name); try { if (@eval('?>' . $code) === FALSE) { // @ is escalated to exception $error = error_get_last(); $e = new CompileException('Error in template: ' . $error['message']); throw $e->setSource($code, $error['line'], $name . ' (compiled)'); } } catch (\ParseError $e) { $e = new CompileException('Error in template: ' . $e->getMessage(), 0, $e); throw $e->setSource($code, $e->getLine(), $name . ' (compiled)'); } return $code; }
/** * @return void */ private function loadTemplate($name) { if (!$this->tempDirectory) { $code = $this->compile($name); if (@eval('?>' . $code) === FALSE) { // @ is escalated to exception $error = error_get_last(); $e = new CompileException('Error in template: ' . $error['message']); throw $e->setSource($code, $error['line'], "{$name} (compiled)"); } return; } $file = $this->getCacheFile($name); if (!$this->isExpired($file, $name) && @(include $file) !== FALSE) { // @ - file may not exist return; } if (!is_dir($this->tempDirectory)) { @mkdir($this->tempDirectory); // @ - directory may already exist } $handle = fopen("{$file}.lock", 'c+'); if (!$handle || !flock($handle, LOCK_EX)) { throw new \RuntimeException("Unable to acquire exclusive lock '{$file}.lock'."); } if (!is_file($file) || $this->isExpired($file, $name)) { $code = $this->compile($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}'."); } } if ((include $file) === FALSE) { throw new \RuntimeException("Unable to load '{$file}'."); } flock($handle, LOCK_UN); }