123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php namespace Dever;
- use Dever;
- use Dever\Pagination\Paginator;
- class Output
- {
- private static $format = 'json';
- public static function success($data, $uuid = false, $code = 0)
- {
- if (is_object($data)) {# && $data instanceof \Generator
- $result = array();
- foreach ($data as $v) {
- $result[] = $v;
- }
- $data = $result;
- }
- $result = array();
- $result['status'] = 1;
- $result['msg'] = 'success';
- $result['data'] = $data;
- $result['code'] = $code;
- if ($page = self::page()) {
- $result['page'] = $page;
- }
- if ($uuid) {
- $result['uuid'] = Dever::string('uuid');
- }
- return self::handle($result);
- }
- public static function error($msg, $param = array(), $code = 1)
- {
- $result = array();
- $result['status'] = 2;
- $result['code'] = $code;
- if (is_string($msg)) {
- $msg = Lang::get($msg, $param);
- }
- $result['msg'] = $msg;
- self::handle($result);
- die;
- }
- public static function out($result)
- {
- return self::handle($result);
- }
- public static function format($data)
- {
- $content = "<pre>\n";
- $content .= htmlspecialchars(print_r($data, true));
- $content .= "\n</pre>\n";
- return $content;
- }
- public static function handle(&$result)
- {
- self::json($result);
- self::callback($result);
- self::func($result);
- Debug::out($result);
- if (self::$format == 'json') {
- print_r($result);
- } else {
- self::html($result);
- }
- }
- public static function page($method = 'current')
- {
- return Paginator::getInstance($method)->toArray();
- }
- private static function json(&$result)
- {
- if (self::$format == 'json' || Route::input('json') == 1) {
- if (!$result) {
- $result = (object) $result;
- }
- $result = Dever::json_encode($result);
- self::$format = 'json';
- } else {
- self::$format = 'str';
- }
- }
- private static function callback(&$result)
- {
- if ($callback = Route::input('callback')) {
- $result = $callback . '(' . $result . ')';
- }
- }
- private static function func(&$result)
- {
- if ($function = Route::input('function')) {
- $result = '<script>parent.' . $function . '(' . $result . ')' . '</script>';
- }
- }
- public static function html($msg)
- {
- $html = '' . $msg['msg'];
- $host = Route::url('');
- $name = '404';
- if ($msg['code'] > 1) {
- $name = $msg['code'];
- }
- if ($name == 404) {
- header("HTTP/1.1 404 Not Found");
- header("Status: 404 Not Found");
- }
- $file = DEVER_APP_PATH . 'config/html/' . $name . '.html';
- if (is_file($file)) {
- include $file;
- } else {
- $file = DEVER_PROJECT_PATH . 'config/html/' . $name . '.html';
- if (is_file($file)) {
- include $file;
- } else {
- include DEVER_PATH . 'config/html/default.html';
- }
- }
- }
- }
|