/** * 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; }