123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace think\view\driver;
- use think\App;
- use think\exception\TemplateNotFoundException;
- use think\Loader;
- use think\Log;
- use think\Request;
- use think\Template;
- class Think
- {
-
- private $template;
-
- protected $config = [
-
- 'view_base' => '',
-
- 'view_path' => '',
-
- 'view_suffix' => 'html',
-
- 'view_depr' => DS,
-
- 'tpl_cache' => true,
-
- 'auto_rule' => 1,
- ];
- public function __construct($config = [])
- {
- $this->config = array_merge($this->config, $config);
- if (empty($this->config['view_path'])) {
- $this->config['view_path'] = App::$modulePath . 'view' . DS;
- }
- $this->template = new Template($this->config);
- }
-
- public function exists($template)
- {
- if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
-
- $template = $this->parseTemplate($template);
- }
- return is_file($template);
- }
-
- public function fetch($template, $data = [], $config = [])
- {
- if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
-
- $template = $this->parseTemplate($template);
- }
-
- if (!is_file($template)) {
- throw new TemplateNotFoundException('template not exists:' . $template, $template);
- }
-
- App::$debug && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info');
- $this->template->fetch($template, $data, $config);
- }
-
- public function display($template, $data = [], $config = [])
- {
- $this->template->display($template, $data, $config);
- }
-
- private function parseTemplate($template)
- {
-
- $request = Request::instance();
-
- if (strpos($template, '@')) {
-
- list($module, $template) = explode('@', $template);
- }
- if ($this->config['view_base']) {
-
- $module = isset($module) ? $module : $request->module();
- $path = $this->config['view_base'] . ($module ? $module . DS : '');
- } else {
- $path = isset($module) ? APP_PATH . $module . DS . 'view' . DS : $this->config['view_path'];
- }
- $depr = $this->config['view_depr'];
- if (0 !== strpos($template, '/')) {
- $template = str_replace(['/', ':'], $depr, $template);
- $controller = Loader::parseName($request->controller());
- if ($controller) {
- if ('' == $template) {
-
- $template = str_replace('.', DS, $controller) . $depr . (1 == $this->config['auto_rule'] ? Loader::parseName($request->action(true)) : $request->action());
- } elseif (false === strpos($template, $depr)) {
- $template = str_replace('.', DS, $controller) . $depr . $template;
- }
- }
- } else {
- $template = str_replace(['/', ':'], $depr, substr($template, 1));
- }
- return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.');
- }
-
- public function config($name, $value = null)
- {
- if (is_array($name)) {
- $this->template->config($name);
- $this->config = array_merge($this->config, $name);
- } elseif (is_null($value)) {
- return $this->template->config($name);
- } else {
- $this->template->$name = $value;
- $this->config[$name] = $value;
- }
- }
- public function __call($method, $params)
- {
- return call_user_func_array([$this->template, $method], $params);
- }
- }
|