|
| 1 | +<?php |
| 2 | + |
| 3 | +final class DivinerAtom { |
| 4 | + |
| 5 | + const TYPE_FILE = 'file'; |
| 6 | + const TYPE_ARTICLE = 'article'; |
| 7 | + |
| 8 | + private $type; |
| 9 | + private $name; |
| 10 | + private $file; |
| 11 | + private $line; |
| 12 | + private $hash; |
| 13 | + private $contentRaw; |
| 14 | + private $length; |
| 15 | + private $language; |
| 16 | + private $docblockRaw; |
| 17 | + private $docblockText; |
| 18 | + private $docblockMeta; |
| 19 | + private $warnings = array(); |
| 20 | + private $parentHash; |
| 21 | + private $childHashes = array(); |
| 22 | + private $context; |
| 23 | + private $extends = array(); |
| 24 | + private $links = array(); |
| 25 | + private $project; |
| 26 | + |
| 27 | + public function setProject($project) { |
| 28 | + $this->project = $project; |
| 29 | + return $this; |
| 30 | + } |
| 31 | + |
| 32 | + public function getProject() { |
| 33 | + return $this->project; |
| 34 | + } |
| 35 | + |
| 36 | + public function setContext($context) { |
| 37 | + $this->context = $context; |
| 38 | + return $this; |
| 39 | + } |
| 40 | + |
| 41 | + public function getContext() { |
| 42 | + return $this->context; |
| 43 | + } |
| 44 | + |
| 45 | + public static function getAtomSerializationVersion() { |
| 46 | + return 1; |
| 47 | + } |
| 48 | + |
| 49 | + public function addWarning($warning) { |
| 50 | + $this->warnings[] = $warning; |
| 51 | + return $this; |
| 52 | + } |
| 53 | + |
| 54 | + public function getWarnings() { |
| 55 | + return $this->warnings; |
| 56 | + } |
| 57 | + |
| 58 | + public function setDocblockRaw($docblock_raw) { |
| 59 | + $this->docblockRaw = $docblock_raw; |
| 60 | + |
| 61 | + $parser = new PhutilDocblockParser(); |
| 62 | + list($text, $meta) = $parser->parse($docblock_raw); |
| 63 | + $this->docblockText = $text; |
| 64 | + $this->docblockMeta = $meta; |
| 65 | + |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + public function getDocblockRaw() { |
| 70 | + return $this->docblockRaw; |
| 71 | + } |
| 72 | + |
| 73 | + public function getDocblockText() { |
| 74 | + if ($this->docblockText === null) { |
| 75 | + throw new Exception("Call setDocblockRaw() before getDocblockText()!"); |
| 76 | + } |
| 77 | + return $this->docblockText; |
| 78 | + } |
| 79 | + |
| 80 | + public function getDocblockMeta() { |
| 81 | + if ($this->docblockMeta === null) { |
| 82 | + throw new Exception("Call setDocblockRaw() before getDocblockMeta()!"); |
| 83 | + } |
| 84 | + return $this->docblockMeta; |
| 85 | + } |
| 86 | + |
| 87 | + public function setType($type) { |
| 88 | + $this->type = $type; |
| 89 | + return $this; |
| 90 | + } |
| 91 | + |
| 92 | + public function getType() { |
| 93 | + return $this->type; |
| 94 | + } |
| 95 | + |
| 96 | + public function setName($name) { |
| 97 | + $this->name = $name; |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + public function getName() { |
| 102 | + return $this->name; |
| 103 | + } |
| 104 | + |
| 105 | + public function setFile($file) { |
| 106 | + $this->file = $file; |
| 107 | + return $this; |
| 108 | + } |
| 109 | + |
| 110 | + public function getFile() { |
| 111 | + return $this->file; |
| 112 | + } |
| 113 | + |
| 114 | + public function setLine($line) { |
| 115 | + $this->line = $line; |
| 116 | + return $this; |
| 117 | + } |
| 118 | + |
| 119 | + public function getLine() { |
| 120 | + return $this->line; |
| 121 | + } |
| 122 | + |
| 123 | + public function setContentRaw($content_raw) { |
| 124 | + $this->contentRaw = $content_raw; |
| 125 | + return $this; |
| 126 | + } |
| 127 | + |
| 128 | + public function getContentRaw() { |
| 129 | + return $this->contentRaw; |
| 130 | + } |
| 131 | + |
| 132 | + public function setHash($hash) { |
| 133 | + $this->hash = $hash; |
| 134 | + return $this; |
| 135 | + } |
| 136 | + |
| 137 | + public function addLink(DivinerAtomRef $ref) { |
| 138 | + $this->links[] = $ref; |
| 139 | + return $this; |
| 140 | + } |
| 141 | + |
| 142 | + public function addExtends(DivinerAtomRef $ref) { |
| 143 | + $this->extends[] = $ref; |
| 144 | + return $this; |
| 145 | + } |
| 146 | + |
| 147 | + public function getLinkDictionaries() { |
| 148 | + return mpull($this->links, 'toDictionary'); |
| 149 | + } |
| 150 | + |
| 151 | + public function getExtendsDictionaries() { |
| 152 | + return mpull($this->extends, 'toDictionary'); |
| 153 | + } |
| 154 | + |
| 155 | + public function getHash() { |
| 156 | + if ($this->hash) { |
| 157 | + return $this->hash; |
| 158 | + } |
| 159 | + |
| 160 | + $parts = array( |
| 161 | + $this->getType(), |
| 162 | + $this->getName(), |
| 163 | + $this->getFile(), |
| 164 | + $this->getLine(), |
| 165 | + $this->getLength(), |
| 166 | + $this->getLanguage(), |
| 167 | + $this->getContentRaw(), |
| 168 | + $this->getDocblockRaw(), |
| 169 | + mpull($this->extends, 'toHash'), |
| 170 | + mpull($this->links, 'toHash'), |
| 171 | + ); |
| 172 | + |
| 173 | + return md5(serialize($parts)).'N'; |
| 174 | + } |
| 175 | + |
| 176 | + public function setLength($length) { |
| 177 | + $this->length = $length; |
| 178 | + return $this; |
| 179 | + } |
| 180 | + |
| 181 | + public function getLength() { |
| 182 | + return $this->length; |
| 183 | + } |
| 184 | + |
| 185 | + public function setLanguage($language) { |
| 186 | + $this->language = $language; |
| 187 | + return $this; |
| 188 | + } |
| 189 | + |
| 190 | + public function getLanguage() { |
| 191 | + return $this->language; |
| 192 | + } |
| 193 | + |
| 194 | + public function addChildHash($child_hash) { |
| 195 | + $this->childHashes[] = $child_hash; |
| 196 | + return $this; |
| 197 | + } |
| 198 | + |
| 199 | + public function getChildHashes() { |
| 200 | + return $this->childHashes; |
| 201 | + } |
| 202 | + |
| 203 | + public function setParentHash($parent_hash) { |
| 204 | + if ($this->parentHash) { |
| 205 | + throw new Exception("Atom already has a parent!"); |
| 206 | + } |
| 207 | + $this->parentHash = $parent_hash; |
| 208 | + return $this; |
| 209 | + } |
| 210 | + |
| 211 | + public function getParentHash() { |
| 212 | + return $this->parentHash; |
| 213 | + } |
| 214 | + |
| 215 | + public function addChild(DivinerAtom $atom) { |
| 216 | + $atom->setParentHash($this->getHash()); |
| 217 | + $this->addChildHash($atom->getHash()); |
| 218 | + return $this; |
| 219 | + } |
| 220 | + |
| 221 | + public function getURI() { |
| 222 | + $parts = array(); |
| 223 | + $parts[] = phutil_escape_uri_path_component($this->getType()); |
| 224 | + if ($this->getContext()) { |
| 225 | + $parts[] = phutil_escape_uri_path_component($this->getContext()); |
| 226 | + } |
| 227 | + $parts[] = phutil_escape_uri_path_component($this->getName()); |
| 228 | + $parts[] = null; |
| 229 | + return implode('/', $parts); |
| 230 | + } |
| 231 | + |
| 232 | + |
| 233 | + public function toDictionary() { |
| 234 | + // NOTE: If you change this format, bump the format version in |
| 235 | + // getAtomSerializationVersion(). |
| 236 | + |
| 237 | + return array( |
| 238 | + 'type' => $this->getType(), |
| 239 | + 'name' => $this->getName(), |
| 240 | + 'file' => $this->getFile(), |
| 241 | + 'line' => $this->getLine(), |
| 242 | + 'hash' => $this->getHash(), |
| 243 | + 'uri' => $this->getURI(), |
| 244 | + 'length' => $this->getLength(), |
| 245 | + 'context' => $this->getContext(), |
| 246 | + 'language' => $this->getLanguage(), |
| 247 | + 'docblockRaw' => $this->getDocblockRaw(), |
| 248 | + 'warnings' => $this->getWarnings(), |
| 249 | + 'parentHash' => $this->getParentHash(), |
| 250 | + 'childHashes' => $this->getChildHashes(), |
| 251 | + 'extends' => $this->getExtendsDictionaries(), |
| 252 | + 'links' => $this->getLinkDictionaries(), |
| 253 | + 'ref' => $this->getRef()->toDictionary(), |
| 254 | + ); |
| 255 | + } |
| 256 | + |
| 257 | + public function getRef() { |
| 258 | + return id(new DivinerAtomRef()) |
| 259 | + ->setProject($this->getProject()) |
| 260 | + ->setContext($this->getContext()) |
| 261 | + ->setType($this->getType()) |
| 262 | + ->setName($this->getName()); |
| 263 | + } |
| 264 | + |
| 265 | + public static function newFromDictionary(array $dictionary) { |
| 266 | + $atom = id(new DivinerAtom()) |
| 267 | + ->setType(idx($dictionary, 'type')) |
| 268 | + ->setName(idx($dictionary, 'name')) |
| 269 | + ->setFile(idx($dictionary, 'file')) |
| 270 | + ->setLine(idx($dictionary, 'line')) |
| 271 | + ->setHash(idx($dictionary, 'hash')) |
| 272 | + ->setLength(idx($dictionary, 'length')) |
| 273 | + ->setContext(idx($dictionary, 'context')) |
| 274 | + ->setLanguage(idx($dictionary, 'language')) |
| 275 | + ->setParentHash(idx($dictionary, 'parentHash')) |
| 276 | + ->setDocblockRaw(idx($dictionary, 'docblockRaw')); |
| 277 | + |
| 278 | + foreach (idx($dictionary, 'warnings', array()) as $warning) { |
| 279 | + $atom->addWarning($warning); |
| 280 | + } |
| 281 | + |
| 282 | + foreach (idx($dictionary, 'childHashes', array()) as $child) { |
| 283 | + $atom->addChildHash($child); |
| 284 | + } |
| 285 | + |
| 286 | + return $atom; |
| 287 | + } |
| 288 | + |
| 289 | +} |
0 commit comments