|
| 1 | +<?php |
| 2 | + |
| 3 | +final class AlmanacQueryServicesConduitAPIMethod |
| 4 | + extends AlmanacConduitAPIMethod { |
| 5 | + |
| 6 | + public function getAPIMethodName() { |
| 7 | + return 'almanac.queryservices'; |
| 8 | + } |
| 9 | + |
| 10 | + public function getMethodDescription() { |
| 11 | + return pht('Query Almanac services.'); |
| 12 | + } |
| 13 | + |
| 14 | + public function defineParamTypes() { |
| 15 | + return array( |
| 16 | + 'ids' => 'optional list<id>', |
| 17 | + 'phids' => 'optional list<phid>', |
| 18 | + 'names' => 'optional list<phid>', |
| 19 | + ) + self::getPagerParamTypes(); |
| 20 | + } |
| 21 | + |
| 22 | + public function defineReturnType() { |
| 23 | + return 'list<wild>'; |
| 24 | + } |
| 25 | + |
| 26 | + public function defineErrorTypes() { |
| 27 | + return array(); |
| 28 | + } |
| 29 | + |
| 30 | + protected function execute(ConduitAPIRequest $request) { |
| 31 | + $viewer = $request->getUser(); |
| 32 | + |
| 33 | + $query = id(new AlmanacServiceQuery()) |
| 34 | + ->setViewer($viewer); |
| 35 | + |
| 36 | + $ids = $request->getValue('ids'); |
| 37 | + if ($ids !== null) { |
| 38 | + $query->withIDs($ids); |
| 39 | + } |
| 40 | + |
| 41 | + $phids = $request->getValue('phids'); |
| 42 | + if ($phids !== null) { |
| 43 | + $query->withPHIDs($phids); |
| 44 | + } |
| 45 | + |
| 46 | + $names = $request->getValue('names'); |
| 47 | + if ($names !== null) { |
| 48 | + $query->withNames($names); |
| 49 | + } |
| 50 | + |
| 51 | + $pager = $this->newPager($request); |
| 52 | + |
| 53 | + $services = $query->executeWithCursorPager($pager); |
| 54 | + |
| 55 | + $bindings = id(new AlmanacBindingQuery()) |
| 56 | + ->setViewer($viewer) |
| 57 | + ->withServicePHIDs(mpull($services, 'getPHID')) |
| 58 | + ->execute(); |
| 59 | + $bindings = mgroup($bindings, 'getServicePHID'); |
| 60 | + |
| 61 | + $data = array(); |
| 62 | + foreach ($services as $service) { |
| 63 | + $phid = $service->getPHID(); |
| 64 | + |
| 65 | + $properties = $service->getAlmanacProperties(); |
| 66 | + $properties = mpull($properties, 'getFieldValue', 'getFieldName'); |
| 67 | + |
| 68 | + $service_bindings = idx($bindings, $phid, array()); |
| 69 | + $service_bindings = array_values($service_bindings); |
| 70 | + foreach ($service_bindings as $key => $service_binding) { |
| 71 | + $service_bindings[$key] = $this->getBindingDictionary($service_binding); |
| 72 | + } |
| 73 | + |
| 74 | + $data[] = $this->getServiceDictionary($service) + array( |
| 75 | + 'bindings' => $service_bindings, |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + $results = array( |
| 80 | + 'data' => $data, |
| 81 | + ); |
| 82 | + |
| 83 | + return $this->addPagerResults($results, $pager); |
| 84 | + } |
| 85 | + |
| 86 | + private function getServiceDictionary(AlmanacService $service) { |
| 87 | + return array( |
| 88 | + 'id' => (int)$service->getID(), |
| 89 | + 'phid' => $service->getPHID(), |
| 90 | + 'name' => $service->getName(), |
| 91 | + 'uri' => PhabricatorEnv::getProductionURI($service->getURI()), |
| 92 | + 'properties' => $this->getPropertiesDictionary($service), |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + private function getBindingDictionary(AlmanacBinding $binding) { |
| 97 | + return array( |
| 98 | + 'id' => (int)$binding->getID(), |
| 99 | + 'phid' => $binding->getPHID(), |
| 100 | + 'properties' => $this->getPropertiesDictionary($binding), |
| 101 | + 'interface' => $this->getInterfaceDictionary($binding->getInterface()), |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + private function getPropertiesDictionary(AlmanacPropertyInterface $obj) { |
| 106 | + $properties = $obj->getAlmanacProperties(); |
| 107 | + return (object)mpull($properties, 'getFieldValue', 'getFieldName'); |
| 108 | + } |
| 109 | + |
| 110 | + private function getInterfaceDictionary(AlmanacInterface $interface) { |
| 111 | + return array( |
| 112 | + 'id' => (int)$interface->getID(), |
| 113 | + 'phid' => $interface->getPHID(), |
| 114 | + 'address' => $interface->getAddress(), |
| 115 | + 'port' => (int)$interface->getPort(), |
| 116 | + 'device' => $this->getDeviceDictionary($interface->getDevice()), |
| 117 | + 'network' => $this->getNetworkDictionary($interface->getNetwork()), |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + private function getDeviceDictionary(AlmanacDevice $device) { |
| 122 | + return array( |
| 123 | + 'id' => (int)$device->getID(), |
| 124 | + 'phid' => $device->getPHID(), |
| 125 | + 'name' => $device->getName(), |
| 126 | + 'properties' => $this->getPropertiesDictionary($device), |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + private function getNetworkDictionary(AlmanacNetwork $network) { |
| 131 | + return array( |
| 132 | + 'id' => (int)$network->getID(), |
| 133 | + 'phid' => $network->getPHID(), |
| 134 | + 'name' => $network->getName(), |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments