Angular.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | OneThink [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016 http://www.thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 翟帅干 <zhaishuaigan@qq.com> <http://zhaishuaigan.cn>
  8. // +----------------------------------------------------------------------
  9. namespace think\view\driver;
  10. use think\Request;
  11. use think\App;
  12. use think\angular\Angular as AngularTpl;
  13. use think\template\driver\File as Storage;
  14. class Angular
  15. {
  16. private $template = null;
  17. private $config = [];
  18. private $storage = null;
  19. public function __construct($config = [])
  20. {
  21. $default = [
  22. 'debug' => App::$debug, // 是否开启调试模式
  23. 'tpl_path' => App::$modulePath . 'view' . DS, // 模板目录
  24. 'tpl_suffix' => '.html', // 模板后缀
  25. 'tpl_cache_path' => RUNTIME_PATH . 'temp' . DS, // 模板缓存目录
  26. 'tpl_cache_suffix' => '.php', // 模板缓存文件后缀
  27. 'directive_prefix' => 'php-', // 指令前缀
  28. 'directive_max' => 10000, // 指令的最大解析次数
  29. ];
  30. $this->config = array_merge($default, $config);
  31. $this->template = new AngularTpl($this->config);
  32. // 初始化模板编译存储器
  33. $this->storage = new Storage();
  34. }
  35. /**
  36. * 获取模版运行结果
  37. * @param string $template 模版地址
  38. * @param array $data 模版数据
  39. * @param array $config 配置
  40. * @return string
  41. */
  42. public function fetch($template, $data = [], $config = [])
  43. {
  44. // 处理模版地址
  45. $template = $this->parseTemplatePath($template);
  46. // 根据模版文件名定位缓存文件
  47. $tpl_cache_file = $this->config['tpl_cache_path'] . 'angular_' . md5($template) . '.php';
  48. if (App::$debug || !is_file($tpl_cache_file) || !$this->storage->check($tpl_cache_file, 0)) {
  49. // 编译模板内容
  50. $content = $this->template->compiler($template, $data);
  51. $this->storage->write($tpl_cache_file, $content);
  52. }
  53. $this->storage->read($tpl_cache_file, $data);
  54. }
  55. /**
  56. * fetch的别名
  57. * @param string $template 模版地址
  58. * @param array $data 模版数据
  59. * @param array $config 配置
  60. * @return string
  61. */
  62. public function display($template, $data = [], $config = [])
  63. {
  64. return $this->fetch($template, $data, $config);
  65. }
  66. /**
  67. * 如果模版为空, 则通过URL参数获取模版地址
  68. * @param string $template 模版地址
  69. * @return string
  70. */
  71. public function parseTemplatePath($template = '')
  72. {
  73. $request = Request::instance();
  74. $controller = strtolower($request->controller());
  75. $action = $request->action();
  76. if (!$template) {
  77. // 没有传模版名
  78. $template = $controller . DS . $action;
  79. $template = str_replace('.', DS, $template);
  80. return $template;
  81. } elseif (strpos($template, '/') === false) {
  82. // 只传了操作名
  83. $template = $controller . DS . $template;
  84. $template = str_replace('.', DS, $template);
  85. return $template;
  86. }
  87. // 默认原样返回
  88. return $template;
  89. }
  90. public function __call($method, $params)
  91. {
  92. return call_user_func_array([$this->template, $method], $params);
  93. }
  94. }