3 final class PhabricatorDocumentRef
11 private $symbolMetadata = array();
13 private $coverage = array();
16 public function setFile(PhabricatorFile
$file) {
21 public function getFile() {
25 public function setMimeType($mime_type) {
26 $this->mimeType
= $mime_type;
30 public function getMimeType() {
31 if ($this->mimeType
!== null) {
32 return $this->mimeType
;
36 return $this->file
->getMimeType();
42 public function setName($name) {
47 public function getName() {
48 if ($this->name
!== null) {
53 return $this->file
->getName();
59 public function setByteLength($length) {
60 $this->byteLength
= $length;
64 public function getByteLength() {
65 if ($this->byteLength
!== null) {
66 return $this->byteLength
;
69 if ($this->data
!== null) {
70 return strlen($this->data
);
74 return (int)$this->file
->getByteSize();
80 public function setData($data) {
85 public function loadData($begin = null, $end = null) {
86 if ($this->data
!== null) {
89 if ($begin !== null && $end !== null) {
90 $data = substr($data, $begin, $end - $begin);
91 } else if ($begin !== null) {
92 $data = substr($data, $begin);
93 } else if ($end !== null) {
94 $data = substr($data, 0, $end);
101 $iterator = $this->file
->getFileDataIterator($begin, $end);
104 foreach ($iterator as $chunk) {
110 throw new PhutilMethodNotImplementedException();
113 public function hasAnyMimeType(array $candidate_types) {
114 $mime_full = $this->getMimeType();
116 if (!phutil_nonempty_string($mime_full)) {
120 $mime_parts = explode(';', $mime_full);
122 $mime_type = head($mime_parts);
123 $mime_type = $this->normalizeMimeType($mime_type);
125 foreach ($candidate_types as $candidate_type) {
126 if ($this->normalizeMimeType($candidate_type) === $mime_type) {
134 private function normalizeMimeType($mime_type) {
135 $mime_type = trim($mime_type);
136 $mime_type = phutil_utf8_strtolower($mime_type);
140 public function isProbablyText() {
141 $snippet = $this->getSnippet();
142 return (strpos($snippet, "\0") === false);
145 public function isProbablyJSON() {
146 if (!$this->isProbablyText()) {
150 $snippet = $this->getSnippet();
152 // If the file is longer than the snippet, we don't detect the content
153 // as JSON. We could use some kind of heuristic here if we wanted, but
154 // see PHI749 for a false positive.
155 if (strlen($snippet) < $this->getByteLength()) {
159 // If the snippet is the whole file, just check if the snippet is valid
160 // JSON. Note that `phutil_json_decode()` only accepts arrays and objects
161 // as JSON, so this won't misfire on files with content like "3".
163 phutil_json_decode($snippet);
165 } catch (Exception
$ex) {
170 public function getSnippet() {
171 if ($this->snippet
=== null) {
172 $this->snippet
= $this->loadData(null, (1024 * 1024 * 1));
175 return $this->snippet
;
178 public function setSymbolMetadata(array $metadata) {
179 $this->symbolMetadata
= $metadata;
183 public function getSymbolMetadata() {
184 return $this->symbolMetadata
;
187 public function setBlameURI($blame_uri) {
188 $this->blameURI
= $blame_uri;
192 public function getBlameURI() {
193 return $this->blameURI
;
196 public function addCoverage($coverage) {
197 $this->coverage
[] = array(
203 public function getCoverage() {
204 return $this->coverage
;