Path: blob/master/src/applications/almanac/storage/AlmanacService.php
12256 views
<?php12final class AlmanacService3extends AlmanacDAO4implements5PhabricatorPolicyInterface,6PhabricatorApplicationTransactionInterface,7PhabricatorProjectInterface,8AlmanacPropertyInterface,9PhabricatorDestructibleInterface,10PhabricatorNgramsInterface,11PhabricatorConduitResultInterface,12PhabricatorExtendedPolicyInterface {1314protected $name;15protected $nameIndex;16protected $viewPolicy;17protected $editPolicy;18protected $serviceType;1920private $almanacProperties = self::ATTACHABLE;21private $bindings = self::ATTACHABLE;22private $activeBindings = self::ATTACHABLE;23private $serviceImplementation = self::ATTACHABLE;2425public static function initializeNewService($type) {26$type_map = AlmanacServiceType::getAllServiceTypes();2728$implementation = idx($type_map, $type);29if (!$implementation) {30throw new Exception(31pht(32'No Almanac service type "%s" exists!',33$type));34}3536return id(new AlmanacService())37->setViewPolicy(PhabricatorPolicies::POLICY_USER)38->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN)39->attachAlmanacProperties(array())40->setServiceType($type)41->attachServiceImplementation($implementation);42}4344protected function getConfiguration() {45return array(46self::CONFIG_AUX_PHID => true,47self::CONFIG_COLUMN_SCHEMA => array(48'name' => 'text128',49'nameIndex' => 'bytes12',50'serviceType' => 'text64',51),52self::CONFIG_KEY_SCHEMA => array(53'key_name' => array(54'columns' => array('nameIndex'),55'unique' => true,56),57'key_nametext' => array(58'columns' => array('name'),59),60'key_servicetype' => array(61'columns' => array('serviceType'),62),63),64) + parent::getConfiguration();65}6667public function getPHIDType() {68return AlmanacServicePHIDType::TYPECONST;69}7071public function save() {72AlmanacNames::validateName($this->getName());7374$this->nameIndex = PhabricatorHash::digestForIndex($this->getName());7576return parent::save();77}7879public function getURI() {80return '/almanac/service/view/'.$this->getName().'/';81}8283public function getBindings() {84return $this->assertAttached($this->bindings);85}8687public function getActiveBindings() {88return $this->assertAttached($this->activeBindings);89}9091public function attachBindings(array $bindings) {92$active_bindings = array();93foreach ($bindings as $key => $binding) {94// Filter out disabled bindings.95if ($binding->getIsDisabled()) {96continue;97}9899// Filter out bindings to disabled devices.100if ($binding->getDevice()->isDisabled()) {101continue;102}103104$active_bindings[$key] = $binding;105}106107$this->attachActiveBindings($active_bindings);108109$this->bindings = $bindings;110return $this;111}112113public function attachActiveBindings(array $bindings) {114$this->activeBindings = $bindings;115return $this;116}117118public function getServiceImplementation() {119return $this->assertAttached($this->serviceImplementation);120}121122public function attachServiceImplementation(AlmanacServiceType $type) {123$this->serviceImplementation = $type;124return $this;125}126127public function isClusterService() {128return $this->getServiceImplementation()->isClusterServiceType();129}130131132/* -( AlmanacPropertyInterface )------------------------------------------- */133134135public function attachAlmanacProperties(array $properties) {136assert_instances_of($properties, 'AlmanacProperty');137$this->almanacProperties = mpull($properties, null, 'getFieldName');138return $this;139}140141public function getAlmanacProperties() {142return $this->assertAttached($this->almanacProperties);143}144145public function hasAlmanacProperty($key) {146$this->assertAttached($this->almanacProperties);147return isset($this->almanacProperties[$key]);148}149150public function getAlmanacProperty($key) {151return $this->assertAttachedKey($this->almanacProperties, $key);152}153154public function getAlmanacPropertyValue($key, $default = null) {155if ($this->hasAlmanacProperty($key)) {156return $this->getAlmanacProperty($key)->getFieldValue();157} else {158return $default;159}160}161162public function getAlmanacPropertyFieldSpecifications() {163return $this->getServiceImplementation()->getFieldSpecifications();164}165166public function getBindingFieldSpecifications(AlmanacBinding $binding) {167$impl = $this->getServiceImplementation();168return $impl->getBindingFieldSpecifications($binding);169}170171public function newAlmanacPropertyEditEngine() {172return new AlmanacServicePropertyEditEngine();173}174175public function getAlmanacPropertySetTransactionType() {176return AlmanacServiceSetPropertyTransaction::TRANSACTIONTYPE;177}178179public function getAlmanacPropertyDeleteTransactionType() {180return AlmanacServiceDeletePropertyTransaction::TRANSACTIONTYPE;181}182183184/* -( PhabricatorPolicyInterface )----------------------------------------- */185186187public function getCapabilities() {188return array(189PhabricatorPolicyCapability::CAN_VIEW,190PhabricatorPolicyCapability::CAN_EDIT,191);192}193194public function getPolicy($capability) {195switch ($capability) {196case PhabricatorPolicyCapability::CAN_VIEW:197return $this->getViewPolicy();198case PhabricatorPolicyCapability::CAN_EDIT:199return $this->getEditPolicy();200}201}202203public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {204return false;205}206207208/* -( PhabricatorExtendedPolicyInterface )--------------------------------- */209210211public function getExtendedPolicy($capability, PhabricatorUser $viewer) {212switch ($capability) {213case PhabricatorPolicyCapability::CAN_EDIT:214if ($this->isClusterService()) {215return array(216array(217new PhabricatorAlmanacApplication(),218AlmanacManageClusterServicesCapability::CAPABILITY,219),220);221}222break;223}224225return array();226}227228229/* -( PhabricatorApplicationTransactionInterface )------------------------- */230231232public function getApplicationTransactionEditor() {233return new AlmanacServiceEditor();234}235236public function getApplicationTransactionTemplate() {237return new AlmanacServiceTransaction();238}239240241/* -( PhabricatorDestructibleInterface )----------------------------------- */242243244public function destroyObjectPermanently(245PhabricatorDestructionEngine $engine) {246247$bindings = id(new AlmanacBindingQuery())248->setViewer($engine->getViewer())249->withServicePHIDs(array($this->getPHID()))250->execute();251foreach ($bindings as $binding) {252$engine->destroyObject($binding);253}254255$this->delete();256}257258259/* -( PhabricatorNgramsInterface )----------------------------------------- */260261262public function newNgrams() {263return array(264id(new AlmanacServiceNameNgrams())265->setValue($this->getName()),266);267}268269270/* -( PhabricatorConduitResultInterface )---------------------------------- */271272273public function getFieldSpecificationsForConduit() {274return array(275id(new PhabricatorConduitSearchFieldSpecification())276->setKey('name')277->setType('string')278->setDescription(pht('The name of the service.')),279id(new PhabricatorConduitSearchFieldSpecification())280->setKey('serviceType')281->setType('string')282->setDescription(pht('The service type constant.')),283);284}285286public function getFieldValuesForConduit() {287return array(288'name' => $this->getName(),289'serviceType' => $this->getServiceType(),290);291}292293public function getConduitSearchAttachments() {294return array(295id(new AlmanacPropertiesSearchEngineAttachment())296->setAttachmentKey('properties'),297id(new AlmanacBindingsSearchEngineAttachment())298->setAttachmentKey('bindings'),299id(new AlmanacBindingsSearchEngineAttachment())300->setIsActive(true)301->setAttachmentKey('activeBindings'),302);303}304305}306307308