1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-23 02:38:48 +02:00
phorge-phorge/src/applications/diviner/atomizer/DivinerAtomizer.php
epriestley b7f6956ec9 Allow Diviner groups to be configured in .book files
Summary:
Ref T988. Currently, every class/function needs to be annotated with `@group`, but 99% of this data can be inferred from file structure, at least in this project. Allow group specifications like:

  "paste" : {
    "name" : "Paste",
    "include" : "(^src/applications/paste/)"
  }

..to automatically put everything defined there in the "paste" group. A list of regexps is also supported. Depends on D6855.

Test Plan: Regenerated documentation with `bin/diviner generate --book src/docs/book/phabricator.book --clean`, observed all Paste stuff go in the paste group.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T988

Differential Revision: https://secure.phabricator.com/D6856
2013-09-02 11:33:02 -07:00

77 lines
1.9 KiB
PHP

<?php
/**
* Generate @{class:DivinerAtom}s from source code.
*/
abstract class DivinerAtomizer {
private $book;
private $fileName;
private $atomContext;
/**
* If you make a significant change to an atomizer, you can bump this
* version to drop all the old atom caches.
*/
public static function getAtomizerVersion() {
return 1;
}
final public function atomize($file_name, $file_data, array $context) {
$this->fileName = $file_name;
$this->atomContext = $context;
$atoms = $this->executeAtomize($file_name, $file_data);
// Promote the "@group" special to a property. If there's no "@group" on
// an atom but the file it's in matches a group pattern, associate it with
// the right group.
foreach ($atoms as $atom) {
$group = null;
try {
$group = $atom->getDocblockMetaValue('group');
} catch (Exception $ex) {
// There's no docblock metadata.
}
// If there's no group, but the file matches a group, use that group.
if ($group === null && isset($context['group'])) {
$group = $context['group'];
}
if ($group !== null) {
$atom->setProperty('group', $group);
}
}
return $atoms;
}
abstract protected function executeAtomize($file_name, $file_data);
final public function setBook($book) {
$this->book = $book;
return $this;
}
final public function getBook() {
return $this->book;
}
protected function newAtom($type) {
return id(new DivinerAtom())
->setBook($this->getBook())
->setFile($this->fileName)
->setType($type);
}
protected function newRef($type, $name, $book = null, $context = null) {
$book = coalesce($book, $this->getBook());
return id(new DivinerAtomRef())
->setBook($book)
->setContext($context)
->setType($type)
->setName($name);
}
}