PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/applications/diviner/atomizer/DivinerAtomizer.php

https://github.com/navyuginfo/phabricator
PHP | 77 lines | 50 code | 15 blank | 12 comment | 3 complexity | 98a089ae68e3c2337541adf3e2ed2f8f MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-3.0, MIT, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. /**
  3. * Generate @{class:DivinerAtom}s from source code.
  4. */
  5. abstract class DivinerAtomizer {
  6. private $book;
  7. private $fileName;
  8. private $atomContext;
  9. /**
  10. * If you make a significant change to an atomizer, you can bump this
  11. * version to drop all the old atom caches.
  12. */
  13. public static function getAtomizerVersion() {
  14. return 1;
  15. }
  16. final public function atomize($file_name, $file_data, array $context) {
  17. $this->fileName = $file_name;
  18. $this->atomContext = $context;
  19. $atoms = $this->executeAtomize($file_name, $file_data);
  20. // Promote the "@group" special to a property. If there's no "@group" on
  21. // an atom but the file it's in matches a group pattern, associate it with
  22. // the right group.
  23. foreach ($atoms as $atom) {
  24. $group = null;
  25. try {
  26. $group = $atom->getDocblockMetaValue('group');
  27. } catch (Exception $ex) {
  28. // There's no docblock metadata.
  29. }
  30. // If there's no group, but the file matches a group, use that group.
  31. if ($group === null && isset($context['group'])) {
  32. $group = $context['group'];
  33. }
  34. if ($group !== null) {
  35. $atom->setProperty('group', $group);
  36. }
  37. }
  38. return $atoms;
  39. }
  40. abstract protected function executeAtomize($file_name, $file_data);
  41. final public function setBook($book) {
  42. $this->book = $book;
  43. return $this;
  44. }
  45. final public function getBook() {
  46. return $this->book;
  47. }
  48. protected function newAtom($type) {
  49. return id(new DivinerAtom())
  50. ->setBook($this->getBook())
  51. ->setFile($this->fileName)
  52. ->setType($type);
  53. }
  54. protected function newRef($type, $name, $book = null, $context = null) {
  55. $book = coalesce($book, $this->getBook());
  56. return id(new DivinerAtomRef())
  57. ->setBook($book)
  58. ->setContext($context)
  59. ->setType($type)
  60. ->setName($name);
  61. }
  62. }