From 6d28592a9bd07695e952680aef692a3c9a6d9c1e Mon Sep 17 00:00:00 2001 From: Hill Date: Mon, 26 Apr 2021 00:04:02 +0000 Subject: [PATCH] fixed utf8 encode --- view_json.php | 57 +++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/view_json.php b/view_json.php index 817bb51..2de3de5 100755 --- a/view_json.php +++ b/view_json.php @@ -3,9 +3,8 @@ use PMVC\Event; use PMVC\HashMap; -use UnexpectedValueException; -${_INIT_CONFIG}[_CLASS] = __NAMESPACE__.'\view_json'; +${_INIT_CONFIG}[_CLASS] = __NAMESPACE__ . '\view_json'; class view_json extends ViewEngine { @@ -13,26 +12,20 @@ class view_json extends ViewEngine public function init() { - $this['headers']=[ - 'Content-type: application/json' - ]; + $this['headers'] = ['Content-type: application/json']; $this->_jsonData = new HashMap(); - \PMVC\callPlugin( - 'dispatcher', - 'attachBefore', - [ - $this, - Event\MAP_REQUEST - ] - ); + \PMVC\callPlugin('dispatcher', 'attachBefore', [ + $this, + Event\MAP_REQUEST, + ]); } public function onMapRequest() { - if (\PMVC\exists('controller','plugin')) { + if (\PMVC\exists('controller', 'plugin')) { $accept = \PMVC\plug('getenv')->get('HTTP_ACCEPT'); - if ('application/json'===$accept) { - \PMVC\plug('controller')[_VIEW_ENGINE]='json'; + if ('application/json' === $accept) { + \PMVC\plug('controller')[_VIEW_ENGINE] = 'json'; } } } @@ -40,20 +33,30 @@ public function onMapRequest() /** * Set theme folder */ - public function setThemeFolder($val) { } + public function setThemeFolder($val) + { + } public function onFinish() { - $lastGet = $this->get(); + $lastGet = $this->get(); if ($lastGet) { - $this->_jsonData[[]] = $lastGet; + $this->_jsonData[[]] = $lastGet; + } + if (defined('JSON_INVALID_UTF8_SUBSTITUTE')) { + echo json_encode(\PMVC\get($this->_jsonData), JSON_INVALID_UTF8_SUBSTITUTE); + } else { + echo \PMVC\utf8JsonEncode(\PMVC\get($this->_jsonData)); } - echo json_encode(\PMVC\get($this->_jsonData)); if (json_last_error() !== JSON_ERROR_NONE) { - throw new UnexpectedValueException(json_last_error_msg()); + echo json_encode([ + 'debugs' => [ + 'error' => json_last_error_msg(), + ], + ]); } } - + public function process() { $this->_jsonData[[]] = $this->get(); @@ -62,15 +65,15 @@ public function process() $this->_jsonData['themePath'] = $this->get('themePath'); } $this->clean(); - if (\PMVC\getOption(Event\FINISH) || - !\PMVC\exists('dispatcher','plugin') + if ( + \PMVC\getOption(Event\FINISH) || + !\PMVC\exists('dispatcher', 'plugin') ) { // run directly if miss event return $this->onFinish(); } else { - // only run by finish event - \PMVC\plug('dispatcher') - ->attachAfter($this, Event\FINISH); + // only run by finish event + \PMVC\plug('dispatcher')->attachAfter($this, Event\FINISH); } } }