PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Libs.class.php

https://github.com/ywl890227/longphp
PHP | 111 lines | 102 code | 9 blank | 0 comment | 14 complexity | 6d525fc1699734e009fbeb79966b2284 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. abstract class Libs{
  3. abstract protected function init();
  4. abstract protected function main();
  5. public function run(){
  6. $this->init();
  7. $this->before();
  8. $this->main();
  9. $this->after();
  10. }
  11. public function before(){
  12. if(!empty($this->db)){
  13. $db_arr = include_once DIR_CONF.'db.conf.php';
  14. $this->load_class('mysql');
  15. $this->db_arr = explode(',', $this->db);
  16. foreach($this->db_arr as $v){
  17. if(!empty($v)){
  18. $v = trim($v);
  19. $db = $db_arr[$v];
  20. $prefix = $v.'_prefix';
  21. $this->$v = new Mysql($db['host'], $db['port'], $db['name'], $db['pass'], $db['database'], $db['prefix']);
  22. $this->$prefix = $db['prefix'];
  23. }
  24. }
  25. }
  26. if(!empty($this->is_smarty)){
  27. require_once DIR_CONF.'smarty.conf.php';
  28. $this->smarty = $smarty;
  29. }
  30. }
  31. public function after(){
  32. global $file;
  33. if(!empty($this->tpl)){
  34. $this->tpl_include($this->tpl);
  35. }else if(!empty($this->_json_data)){
  36. echo json_encode($this->_json_data);
  37. }
  38. if(!empty($this->db_arr)){
  39. foreach((array)$this->db_arr as $v){
  40. $v = trim($v);
  41. $this->$v->close();
  42. }
  43. }
  44. }
  45. public function tpl_include($tpl){
  46. global $file;
  47. $t_arr = explode('_', $tpl);
  48. $tplname = '';
  49. foreach($t_arr as $v){
  50. $tplname .= htmlspecialchars(ucwords(strtolower($v)), ENT_QUOTES, 'UTF-8').'_';
  51. }
  52. $tplname = substr($tplname, 0, -1);
  53. $tplname = explode('/', $tplname);
  54. $tpl_arr_count = count($tplname);
  55. $tplname[$tpl_arr_count - 1] = ucwords(strtolower($tplname[$tpl_arr_count - 1]));
  56. $tplname = implode('/', $tplname);
  57. if(file_exists(DIR_TPL.$file.$tplname.'.tpl.html')){
  58. foreach($this as $k => $v){
  59. $$k = $v;
  60. if(!empty($this->is_smarty)){
  61. $this->smarty->assign($k, $$k);
  62. }
  63. }
  64. if(!empty($this->is_smarty)){
  65. $this->smarty->display($file.$tplname.'.tpl.html');
  66. }else {
  67. require DIR_TPL.$file.$tplname.'.tpl.html';
  68. }
  69. }else {
  70. if(DEBUG){
  71. exit('模版文件: '.DIR_TPL.$file.$tplname.'.tpl.html 不存在');
  72. }else {
  73. header('HTTP/1.1 404 Not Found');
  74. header("status: 404 Not Found");
  75. }
  76. }
  77. }
  78. public function load_fun($fun_name){
  79. if(file_exists(DIR_FUN.ucwords(strtolower($fun_name)).'.fun.php')){
  80. require_once DIR_FUN.ucwords(strtolower($fun_name)).'.fun.php';
  81. }else {
  82. if(DEBUG){
  83. exit('函数文件:'.ucwords(strtolower($fun_name)).'.fun.php 不存在');
  84. }else {
  85. header('HTTP/1.1 404 Not Found');
  86. header("status: 404 Not Found");
  87. }
  88. }
  89. }
  90. public function load_class($class_name){
  91. if(file_exists(DIR_CLASS.ucwords(strtolower($class_name)).'.class.php')){
  92. require_once DIR_CLASS.ucwords(strtolower($class_name)).'.class.php';
  93. }else {
  94. if(DEBUG){
  95. exit('类文件:'.ucwords(strtolower($class_name)).'.class.php 不存在');
  96. }else {
  97. header('HTTP/1.1 404 Not Found');
  98. header("status: 404 Not Found");
  99. }
  100. }
  101. }
  102. }