心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2016年11月6日
  7. * 应用控制基类
  8. */
  9. namespace core\basic;
  10. use core\view\View;
  11. use core\view\Paging;
  12. class Controller
  13. {
  14. // 显示模板
  15. final protected function display($file)
  16. {
  17. $view = View::getInstance();
  18. $content = $view->parser($file);
  19. $content = $this->runtime($content);
  20. echo $this->gzip($content);
  21. exit();
  22. }
  23. // 解析模板
  24. final protected function parser($file)
  25. {
  26. $view = View::getInstance();
  27. return $view->parser($file);
  28. }
  29. // 缓存页面内容,默认直接显示内容,可传递第二参数false返回内容
  30. final protected function cache($content, $display = true)
  31. {
  32. $view = View::getInstance();
  33. if (Config::get('tpl_html_cache')) {
  34. $content = str_replace('{pboot:runtime}', 'Cached at ' . date('Y-m-d H:i:s'), $content);
  35. } else {
  36. $content = $this->runtime($content);
  37. }
  38. $view->cache($content); // 压缩前缓存
  39. $content = $this->gzip($content);
  40. if ($display) {
  41. echo $content;
  42. exit();
  43. } else {
  44. return $content;
  45. }
  46. }
  47. // 设置视图主题
  48. final protected function setTheme($themeName)
  49. {
  50. $view = View::getInstance();
  51. $view->assign('theme', $themeName);
  52. }
  53. // 变量注入接口
  54. final protected function assign($var, $value)
  55. {
  56. $view = View::getInstance();
  57. $view->assign($var, $value);
  58. }
  59. // 变量获取接口
  60. final protected function getVar($var)
  61. {
  62. $view = View::getInstance();
  63. return $view->getVar($var);
  64. }
  65. // 手动生成分页信息,返回限制语句
  66. final protected function page($tatal, $morePageStr = false)
  67. {
  68. $page = Paging::getInstance();
  69. return $page->limit($tatal, $morePageStr);
  70. }
  71. // 获取配置信息
  72. final protected function config($item = null, $array = false)
  73. {
  74. return Config::get($item, $array);
  75. }
  76. // 缓存配置信息
  77. final protected function setConfig($itemName, array $data)
  78. {
  79. return Config::set($itemName, $data);
  80. }
  81. // 写入日志信息
  82. final protected function log($content, $level = "info")
  83. {
  84. Log::write($content, $level);
  85. }
  86. // 解析运行时间标签
  87. private function runtime($content)
  88. {
  89. return str_replace('{pboot:runtime}', 'Processed in ' . round(microtime(true) - START_TIME, 6) . ' second(s).', $content);
  90. }
  91. // 压缩内容
  92. private function gzip($content)
  93. {
  94. if (Config::get('gzip') && ! headers_sent() && extension_loaded("zlib") && strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")) {
  95. $content = gzencode($content, 6);
  96. header("Content-Encoding: gzip");
  97. header("Vary: Accept-Encoding");
  98. header("Content-Length: " . strlen($content));
  99. }
  100. return $content;
  101. }
  102. }