|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace think; |
|
|
|
|
|
class View |
|
|
{ |
|
|
|
|
|
protected static $instance; |
|
|
|
|
|
public $engine; |
|
|
|
|
|
protected $data = []; |
|
|
|
|
|
protected static $var = []; |
|
|
|
|
|
protected $replace = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct($engine = [], $replace = []) |
|
|
{ |
|
|
|
|
|
$this->engine($engine); |
|
|
|
|
|
$request = Request::instance(); |
|
|
$base = $request->root(); |
|
|
$root = strpos($base, '.') ? ltrim(dirname($base), DS) : $base; |
|
|
if ('' != $root) { |
|
|
$root = '/' . ltrim($root, '/'); |
|
|
} |
|
|
|
|
|
if($GLOBALS['config']['site']['new_version'] == 1 || !isset($GLOBALS['config']['site']['new_version']) || (empty($GLOBALS['config']['site']['new_version']) && $GLOBALS['config']['site']['new_version'] != 0)){ |
|
|
$root . $static_path = '/static_new/'; |
|
|
}else{ |
|
|
$root . $static_path = '/static/'; |
|
|
} |
|
|
$baseReplace = [ |
|
|
'__ROOT__' => $root, |
|
|
'MAC_BASE_PATH' => $root, |
|
|
'__URL__' => $base . '/' . $request->module() . '/' . Loader::parseName($request->controller()), |
|
|
'__STATIC__' => $root . $static_path, |
|
|
'__CSS__' => $root . $static_path . '/css', |
|
|
'__JS__' => $root . $static_path .'/js', |
|
|
]; |
|
|
$this->assign('MAC_BASE_PATH', $this->mac_base_path()); |
|
|
$this->replace = array_merge($baseReplace, (array) $replace); |
|
|
} |
|
|
|
|
|
public function mac_base_path() |
|
|
{ |
|
|
return $GLOBALS['rootpath']; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function instance($engine = [], $replace = []) |
|
|
{ |
|
|
if (is_null(self::$instance)) { |
|
|
self::$instance = new self($engine, $replace); |
|
|
} |
|
|
return self::$instance; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function share($name, $value = '') |
|
|
{ |
|
|
if (is_array($name)) { |
|
|
self::$var = array_merge(self::$var, $name); |
|
|
} else { |
|
|
self::$var[$name] = $value; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function assign($name, $value = '') |
|
|
{ |
|
|
if (is_array($name)) { |
|
|
$this->data = array_merge($this->data, $name); |
|
|
} else { |
|
|
$this->data[$name] = $value; |
|
|
} |
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function engine($options = []) |
|
|
{ |
|
|
if (is_string($options)) { |
|
|
$type = $options; |
|
|
$options = []; |
|
|
} else { |
|
|
$type = !empty($options['type']) ? $options['type'] : 'Think'; |
|
|
} |
|
|
|
|
|
$class = false !== strpos($type, '\\') ? $type : '\\think\\view\\driver\\' . ucfirst($type); |
|
|
if (isset($options['type'])) { |
|
|
unset($options['type']); |
|
|
} |
|
|
$this->engine = new $class($options); |
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function config($name, $value = null) |
|
|
{ |
|
|
$this->engine->config($name, $value); |
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function fetch($template = '', $vars = [], $replace = [], $config = [], $renderContent = false) |
|
|
{ |
|
|
|
|
|
$vars = array_merge(self::$var, $this->data, $vars); |
|
|
|
|
|
|
|
|
ob_start(); |
|
|
ob_implicit_flush(0); |
|
|
|
|
|
|
|
|
try { |
|
|
$method = $renderContent ? 'display' : 'fetch'; |
|
|
|
|
|
$replace = array_merge($this->replace, $replace, (array) $this->engine->config('tpl_replace_string')); |
|
|
$this->engine->config('tpl_replace_string', $replace); |
|
|
$this->engine->$method($template, $vars, $config); |
|
|
} catch (\Exception $e) { |
|
|
ob_end_clean(); |
|
|
throw $e; |
|
|
} |
|
|
|
|
|
|
|
|
$content = ob_get_clean(); |
|
|
|
|
|
Hook::listen('view_filter', $content); |
|
|
return $content; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function replace($content, $replace = '') |
|
|
{ |
|
|
if (is_array($content)) { |
|
|
$this->replace = array_merge($this->replace, $content); |
|
|
} else { |
|
|
$this->replace[$content] = $replace; |
|
|
} |
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function display($content, $vars = [], $replace = [], $config = []) |
|
|
{ |
|
|
return $this->fetch($content, $vars, $replace, $config, true); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __set($name, $value) |
|
|
{ |
|
|
$this->data[$name] = $value; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __get($name) |
|
|
{ |
|
|
return $this->data[$name]; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __isset($name) |
|
|
{ |
|
|
return isset($this->data[$name]); |
|
|
} |
|
|
} |
|
|
|