Remove product literal strings in "pht()", part 5
[phabricator.git] / src / applications / files / document / PhabricatorJSONDocumentEngine.php
blob9797ce3da26d90cd36a3f991e4b0ba456d61a52d
1 <?php
3 final class PhabricatorJSONDocumentEngine
4 extends PhabricatorTextDocumentEngine {
6 const ENGINEKEY = 'json';
8 public function getViewAsLabel(PhabricatorDocumentRef $ref) {
9 return pht('View as JSON');
12 protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) {
13 return 'fa-database';
16 protected function getContentScore(PhabricatorDocumentRef $ref) {
18 $name = $ref->getName();
19 if ($name !== null) {
20 if (preg_match('/\.json\z/', $name)) {
21 return 2000;
25 if ($ref->isProbablyJSON()) {
26 return 1750;
29 return 500;
32 protected function newDocumentContent(PhabricatorDocumentRef $ref) {
33 $raw_data = $this->loadTextData($ref);
35 try {
36 $data = phutil_json_decode($raw_data);
38 // See T13635. "phutil_json_decode()" always turns JSON into a PHP array,
39 // and we lose the distinction between "{}" and "[]". This distinction is
40 // important when rendering a document.
41 $data = json_decode($raw_data, false);
42 if (!$data) {
43 throw new PhabricatorDocumentEngineParserException(
44 pht(
45 'Failed to "json_decode(...)" JSON document after successfully '.
46 'decoding it with "phutil_json_decode(...).'));
49 if (preg_match('/^\s*\[/', $raw_data)) {
50 $content = id(new PhutilJSON())->encodeAsList($data);
51 } else {
52 $content = id(new PhutilJSON())->encodeFormatted($data);
55 $message = null;
56 $content = PhabricatorSyntaxHighlighter::highlightWithLanguage(
57 'json',
58 $content);
59 } catch (PhutilJSONParserException $ex) {
60 $message = $this->newMessage(
61 pht(
62 'This document is not valid JSON: %s',
63 $ex->getMessage()));
65 $content = $raw_data;
66 } catch (PhabricatorDocumentEngineParserException $ex) {
67 $message = $this->newMessage(
68 pht(
69 'Unable to parse this document as JSON: %s',
70 $ex->getMessage()));
72 $content = $raw_data;
75 return array(
76 $message,
77 $this->newTextDocumentContent($ref, $content),