Output.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php namespace Dever;
  2. use Dever;
  3. use Dever\Pagination\Paginator;
  4. class Output
  5. {
  6. private static $format = 'json';
  7. public static function success($data, $uuid = false, $code = 0)
  8. {
  9. if (is_object($data)) {# && $data instanceof \Generator
  10. $result = array();
  11. foreach ($data as $v) {
  12. $result[] = $v;
  13. }
  14. $data = $result;
  15. }
  16. $result = array();
  17. $result['status'] = 1;
  18. $result['msg'] = 'success';
  19. $result['data'] = $data;
  20. $result['code'] = $code;
  21. if ($page = self::page()) {
  22. $result['page'] = $page;
  23. }
  24. if ($uuid) {
  25. $result['uuid'] = Dever::string('uuid');
  26. }
  27. return self::handle($result);
  28. }
  29. public static function error($msg, $param = array(), $code = 1)
  30. {
  31. $result = array();
  32. $result['status'] = 2;
  33. $result['code'] = $code;
  34. if (is_string($msg)) {
  35. $msg = Lang::get($msg, $param);
  36. }
  37. $result['msg'] = $msg;
  38. self::handle($result);
  39. die;
  40. }
  41. public static function out($result)
  42. {
  43. return self::handle($result);
  44. }
  45. public static function format($data)
  46. {
  47. $content = "<pre>\n";
  48. $content .= htmlspecialchars(print_r($data, true));
  49. $content .= "\n</pre>\n";
  50. return $content;
  51. }
  52. public static function handle(&$result)
  53. {
  54. self::json($result);
  55. self::callback($result);
  56. self::func($result);
  57. Debug::out($result);
  58. if (self::$format == 'json') {
  59. print_r($result);
  60. } else {
  61. self::html($result);
  62. }
  63. }
  64. public static function page($method = 'current')
  65. {
  66. return Paginator::getInstance($method)->toArray();
  67. }
  68. private static function json(&$result)
  69. {
  70. if (self::$format == 'json' || Route::input('json') == 1) {
  71. if (!$result) {
  72. $result = (object) $result;
  73. }
  74. $result = Dever::json_encode($result);
  75. self::$format = 'json';
  76. } else {
  77. self::$format = 'str';
  78. }
  79. }
  80. private static function callback(&$result)
  81. {
  82. if ($callback = Route::input('callback')) {
  83. $result = $callback . '(' . $result . ')';
  84. }
  85. }
  86. private static function func(&$result)
  87. {
  88. if ($function = Route::input('function')) {
  89. $result = '<script>parent.' . $function . '(' . $result . ')' . '</script>';
  90. }
  91. }
  92. public static function html($msg)
  93. {
  94. $html = '' . $msg['msg'];
  95. $host = Route::url('');
  96. $name = '404';
  97. if ($msg['code'] > 1) {
  98. $name = $msg['code'];
  99. }
  100. if ($name == 404) {
  101. header("HTTP/1.1 404 Not Found");
  102. header("Status: 404 Not Found");
  103. }
  104. $file = DEVER_APP_PATH . 'config/html/' . $name . '.html';
  105. if (is_file($file)) {
  106. include $file;
  107. } else {
  108. $file = DEVER_PROJECT_PATH . 'config/html/' . $name . '.html';
  109. if (is_file($file)) {
  110. include $file;
  111. } else {
  112. include DEVER_PATH . 'config/html/default.html';
  113. }
  114. }
  115. }
  116. }