<?php
<zhaishuaigan@qq.com> <http://zhaishuaigan.cn>
namespace think\view\driver;
use think\Request;
use think\App;
use think\angular\Angular as AngularTpl;
use think\template\driver\File as Storage;
class Angular
{
private $template = null;
private $config = [];
private $storage = null;
public function __construct($config = [])
{
$default = [
'debug' => App::$debug, 'tpl_path' => App::$modulePath . 'view' . DS, 'tpl_suffix' => '.html', 'tpl_cache_path' => RUNTIME_PATH . 'temp' . DS, 'tpl_cache_suffix' => '.php', 'directive_prefix' => 'php-', 'directive_max' => 10000, ];
$this->config = array_merge($default, $config);
$this->template = new AngularTpl($this->config);
$this->storage = new Storage();
}
public function fetch($template, $data = [], $config = [])
{
$template = $this->parseTemplatePath($template);
$tpl_cache_file = $this->config['tpl_cache_path'] . 'angular_' . md5($template) . '.php';
if (App::$debug || !is_file($tpl_cache_file) || !$this->storage->check($tpl_cache_file, 0)) {
$content = $this->template->compiler($template, $data);
$this->storage->write($tpl_cache_file, $content);
}
$this->storage->read($tpl_cache_file, $data);
}
public function display($template, $data = [], $config = [])
{
return $this->fetch($template, $data, $config);
}
public function parseTemplatePath($template = '')
{
$request = Request::instance();
$controller = strtolower($request->controller());
$action = $request->action();
if (!$template) {
$template = $controller . DS . $action;
$template = str_replace('.', DS, $template);
return $template;
} elseif (strpos($template, '/') === false) {
$template = $controller . DS . $template;
$template = str_replace('.', DS, $template);
return $template;
}
return $template;
}
public function __call($method, $params)
{
return call_user_func_array([$this->template, $method], $params);
}
}