DifferentialRevisionPHIDType.php 2.14 KB
Newer Older
1 2
<?php

Joshua Spence's avatar
Joshua Spence committed
3
final class DifferentialRevisionPHIDType extends PhabricatorPHIDType {
4 5 6 7

  const TYPECONST = 'DREV';

  public function getTypeName() {
Chad Little's avatar
Chad Little committed
8
    return pht('Differential Revision');
9 10 11 12 13 14
  }

  public function newObject() {
    return new DifferentialRevision();
  }

15 16 17 18
  public function getPHIDTypeApplicationClass() {
    return 'PhabricatorDifferentialApplication';
  }

19
  protected function buildQueryForObjects(
20 21 22 23
    PhabricatorObjectQuery $query,
    array $phids) {

    return id(new DifferentialRevisionQuery())
24
      ->withPHIDs($phids);
25 26 27 28 29 30 31 32 33 34 35 36
  }

  public function loadHandles(
    PhabricatorHandleQuery $query,
    array $handles,
    array $objects) {

    foreach ($handles as $phid => $handle) {
      $revision = $objects[$phid];

      $title = $revision->getTitle();
      $status = $revision->getStatus();
37 38
      $monogram = $revision->getMonogram();
      $uri = $revision->getURI();
39

40 41 42 43
      $handle
        ->setName($monogram)
        ->setURI($uri)
        ->setFullName("{$monogram}: {$title}");
44

45
      if ($revision->isClosed()) {
46
        $handle->setStatus(PhabricatorObjectHandle::STATUS_CLOSED);
47
      }
48 49 50 51 52 53 54 55 56 57 58 59

      $status = $revision->getStatus();

      $icon = DifferentialRevisionStatus::getRevisionStatusIcon($status);
      $color = DifferentialRevisionStatus::getRevisionStatusColor($status);
      $name = ArcanistDifferentialRevisionStatus::getNameForRevisionStatus(
        $status);

      $handle
        ->setStateIcon($icon)
        ->setStateColor($color)
        ->setStateName($name);
60 61 62 63
    }
  }

  public function canLoadNamedObject($name) {
64
    return preg_match('/^D[1-9]\d*$/i', $name);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  }

  public function loadNamedObjects(
    PhabricatorObjectQuery $query,
    array $names) {

    $id_map = array();
    foreach ($names as $name) {
      $id = (int)substr($name, 1);
      $id_map[$id][] = $name;
    }

    $objects = id(new DifferentialRevisionQuery())
      ->setViewer($query->getViewer())
      ->withIDs(array_keys($id_map))
      ->execute();

    $results = array();
    foreach ($objects as $id => $object) {
      foreach (idx($id_map, $id, array()) as $name) {
        $results[$name] = $object;
      }
    }

    return $results;
  }

}