Path: blob/master/src/applications/files/document/PhabricatorJSONDocumentEngine.php
12241 views
<?php12final class PhabricatorJSONDocumentEngine3extends PhabricatorTextDocumentEngine {45const ENGINEKEY = 'json';67public function getViewAsLabel(PhabricatorDocumentRef $ref) {8return pht('View as JSON');9}1011protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) {12return 'fa-database';13}1415protected function getContentScore(PhabricatorDocumentRef $ref) {1617$name = $ref->getName();18if ($name !== null) {19if (preg_match('/\.json\z/', $name)) {20return 2000;21}22}2324if ($ref->isProbablyJSON()) {25return 1750;26}2728return 500;29}3031protected function newDocumentContent(PhabricatorDocumentRef $ref) {32$raw_data = $this->loadTextData($ref);3334try {35$data = phutil_json_decode($raw_data);3637// See T13635. "phutil_json_decode()" always turns JSON into a PHP array,38// and we lose the distinction between "{}" and "[]". This distinction is39// important when rendering a document.40$data = json_decode($raw_data, false);41if (!$data) {42throw new PhabricatorDocumentEngineParserException(43pht(44'Failed to "json_decode(...)" JSON document after successfully '.45'decoding it with "phutil_json_decode(...).'));46}4748if (preg_match('/^\s*\[/', $raw_data)) {49$content = id(new PhutilJSON())->encodeAsList($data);50} else {51$content = id(new PhutilJSON())->encodeFormatted($data);52}5354$message = null;55$content = PhabricatorSyntaxHighlighter::highlightWithLanguage(56'json',57$content);58} catch (PhutilJSONParserException $ex) {59$message = $this->newMessage(60pht(61'This document is not valid JSON: %s',62$ex->getMessage()));6364$content = $raw_data;65} catch (PhabricatorDocumentEngineParserException $ex) {66$message = $this->newMessage(67pht(68'Unable to parse this document as JSON: %s',69$ex->getMessage()));7071$content = $raw_data;72}7374return array(75$message,76$this->newTextDocumentContent($ref, $content),77);78}7980}818283