public function compile($template, $mode = null)
{
if (!is_string($template)) {
throw new \InvalidArgumentException('string expected');
}
$source = $this->options['source'];
$adapter = $this->options['adapter'];
$path = $this->resolvePath($template);
$class = self::CLASS_PREFIX . md5($path);
if (!$adapter->isReadable($path)) {
throw new \RuntimeException(sprintf('%s is not a valid readable template', $template));
}
$target = $this->options['target'] . '/' . $class . '.php';
if (!isset($mode)) {
$mode = $this->options['mode'];
}
switch ($mode) {
case self::RECOMPILE_ALWAYS:
$compile = true;
break;
case self::RECOMPILE_NEVER:
$compile = !file_exists($target);
break;
case self::RECOMPILE_NORMAL:
default:
$compile = !file_exists($target) || filemtime($target) < $adapter->lastModified($path);
break;
}
if ($compile) {
try {
$lexer = new Lexer($adapter->getContents($path));
$parser = new Parser($lexer->tokenize());
$compiler = new Compiler($parser->parse());
$compiler->compile($path, $target);
} catch (SyntaxError $e) {
throw $e->setMessage($path . ': ' . $e->getMessage());
}
}
return $this;
}