<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\view\driver;
use think\App;
use think\helper\Str;
use think\Template;
use think\template\exception\TemplateNotFoundException;
class Think
{
private $template;
private $app;
protected $config = [
'auto_rule' => 1,
'view_dir_name' => 'view',
'view_path' => '',
'view_suffix' => 'html',
'view_depr' => DIRECTORY_SEPARATOR,
'tpl_cache' => true,
];
public function __construct(App $app, array $config = [])
{
$this->app = $app;
$this->config = array_merge($this->config, (array) $config);
if (empty($this->config['cache_path'])) {
$this->config['cache_path'] = $app->getRuntimePath() . 'temp' . DIRECTORY_SEPARATOR;
}
$this->template = new Template($this->config);
$this->template->setCache($app->cache);
$this->template->extend('$Think', function (array $vars) {
$type = strtoupper(trim(array_shift($vars)));
$param = implode('.', $vars);
switch ($type) {
case 'CONST':
$parseStr = strtoupper($param);
break;
case 'CONFIG':
$parseStr = 'config(\'' . $param . '\')';
break;
case 'LANG':
$parseStr = 'lang(\'' . $param . '\')';
break;
case 'NOW':
$parseStr = "date('Y-m-d g:i a',time())";
break;
case 'LDELIM':
$parseStr = '\'' . ltrim($this->getConfig('tpl_begin'), '\\') . '\'';
break;
case 'RDELIM':
$parseStr = '\'' . ltrim($this->getConfig('tpl_end'), '\\') . '\'';
break;
default:
$parseStr = defined($type) ? $type : '\'\'';
}
return $parseStr;
});
$this->template->extend('$Request', function (array $vars) {
$method = array_shift($vars);
if (!empty($vars)) {
$params = implode('.', $vars);
if ('true' != $params) {
$params = '\'' . $params . '\'';
}
} else {
$params = '';
}
return 'app(\'request\')->' . $method . '(' . $params . ')';
});
}
public function exists(string $template): bool
{
if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
$template = $this->parseTemplate($template);
}
return is_file($template);
}
public function fetch(string $template, array $data = []): void
{
if (empty($this->config['view_path'])) {
$view = $this->config['view_dir_name'];
if (is_dir($this->app->getAppPath() . $view)) {
$path = $this->app->getAppPath() . $view . DIRECTORY_SEPARATOR;
} else {
$appName = $this->app->http->getName();
$path = $this->app->getRootPath() . $view . DIRECTORY_SEPARATOR . ($appName ? $appName . DIRECTORY_SEPARATOR : '');
}
$this->config['view_path'] = $path;
$this->template->view_path = $path;
}
if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
$template = $this->parseTemplate($template);
}
if (!is_file($template)) {
throw new TemplateNotFoundException('template not exists:' . $template, $template);
}
$this->template->fetch($template, $data);
}
public function display(string $template, array $data = []): void
{
$this->template->display($template, $data);
}
private function parseTemplate(string $template): string
{
$request = $this->app['request'];
if (strpos($template, '@')) {
list($app, $template) = explode('@', $template);
}
if (isset($app)) {
$view = $this->config['view_dir_name'];
$viewPath = $this->app->getBasePath() . $app . DIRECTORY_SEPARATOR . $view . DIRECTORY_SEPARATOR;
if (is_dir($viewPath)) {
$path = $viewPath;
} else {
$path = $this->app->getRootPath() . $view . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR;
}
$this->template->view_path = $path;
} else {
$path = $this->config['view_path'];
}
$depr = $this->config['view_depr'];
if (0 !== strpos($template, '/')) {
$template = str_replace(['/', ':'], $depr, $template);
$controller = $request->controller();
if (strpos($controller, '.')) {
$pos = strrpos($controller, '.');
$controller = substr($controller, 0, $pos) . '.' . Str::snake(substr($controller, $pos + 1));
} else {
$controller = Str::snake($controller);
}
if ($controller) {
if ('' == $template) {
if (2 == $this->config['auto_rule']) {
$template = $request->action(true);
} elseif (3 == $this->config['auto_rule']) {
$template = $request->action();
} else {
$template = Str::snake($request->action());
}
$template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $template;
} elseif (false === strpos($template, $depr)) {
$template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $template;
}
}
} else {
$template = str_replace(['/', ':'], $depr, substr($template, 1));
}
return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.');
}
public function config(array $config): void
{
$this->template->config($config);
$this->config = array_merge($this->config, $config);
}
public function getConfig(string $name)
{
return $this->template->getConfig($name);
}
public function __call($method, $params)
{
return call_user_func_array([$this->template, $method], $params);
}
}