public function load($template, $from = '')
{
if ($template instanceof Template) {
return $template;
}
if (!is_string($template)) {
throw new \InvalidArgumentException('string expected');
}
$source = $this->options['source'];
$adapter = $this->options['adapter'];
if (isset($this->paths[$template . $from])) {
$path = $this->paths[$template . $from];
} else {
$path = $this->resolvePath($template, $from);
$this->paths[$template . $from] = $path;
}
$class = self::CLASS_PREFIX . md5($path);
if (isset($this->cache[$class])) {
return $this->cache[$class];
}
if (!class_exists($class, false)) {
if (!$adapter->isReadable($path)) {
throw new \RuntimeException(sprintf('%s is not a valid readable template', $template));
}
$target = $this->options['target'] . '/' . $class . '.php';
switch ($this->options['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());
}
}
require_once $target;
}
$this->cache[$class] = new $class($this, $this->options['helpers']);
return $this->cache[$class];
}