Skip to content

Commit

Permalink
fixed utf8 encode
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed Apr 26, 2021
1 parent 3934884 commit 6d28592
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions view_json.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,60 @@

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
{
private $_jsonData;

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';
}
}
}

/**
* 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();
Expand All @@ -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);
}
}
}

0 comments on commit 6d28592

Please sign in to comment.