Path: blob/master/src/applications/drydock/storage/DrydockBlueprint.php
12256 views
<?php12/**3* @task resource Allocating Resources4* @task lease Acquiring Leases5*/6final class DrydockBlueprint extends DrydockDAO7implements8PhabricatorApplicationTransactionInterface,9PhabricatorPolicyInterface,10PhabricatorCustomFieldInterface,11PhabricatorNgramsInterface,12PhabricatorProjectInterface,13PhabricatorConduitResultInterface {1415protected $className;16protected $blueprintName;17protected $viewPolicy;18protected $editPolicy;19protected $details = array();20protected $isDisabled;2122private $implementation = self::ATTACHABLE;23private $customFields = self::ATTACHABLE;24private $fields = null;2526public static function initializeNewBlueprint(PhabricatorUser $actor) {27$app = id(new PhabricatorApplicationQuery())28->setViewer($actor)29->withClasses(array('PhabricatorDrydockApplication'))30->executeOne();3132$view_policy = $app->getPolicy(33DrydockDefaultViewCapability::CAPABILITY);34$edit_policy = $app->getPolicy(35DrydockDefaultEditCapability::CAPABILITY);3637return id(new DrydockBlueprint())38->setViewPolicy($view_policy)39->setEditPolicy($edit_policy)40->setBlueprintName('')41->setIsDisabled(0);42}4344protected function getConfiguration() {45return array(46self::CONFIG_AUX_PHID => true,47self::CONFIG_SERIALIZATION => array(48'details' => self::SERIALIZATION_JSON,49),50self::CONFIG_COLUMN_SCHEMA => array(51'className' => 'text255',52'blueprintName' => 'sort255',53'isDisabled' => 'bool',54),55) + parent::getConfiguration();56}5758public function generatePHID() {59return PhabricatorPHID::generateNewPHID(60DrydockBlueprintPHIDType::TYPECONST);61}6263public function getImplementation() {64return $this->assertAttached($this->implementation);65}6667public function attachImplementation(DrydockBlueprintImplementation $impl) {68$this->implementation = $impl;69return $this;70}7172public function hasImplementation() {73return ($this->implementation !== self::ATTACHABLE);74}7576public function getDetail($key, $default = null) {77return idx($this->details, $key, $default);78}7980public function setDetail($key, $value) {81$this->details[$key] = $value;82return $this;83}8485public function getFieldValue($key) {86$key = "std:drydock:core:{$key}";87$fields = $this->loadCustomFields();8889$field = idx($fields, $key);90if (!$field) {91throw new Exception(92pht(93'Unknown blueprint field "%s"!',94$key));95}9697return $field->getBlueprintFieldValue();98}99100private function loadCustomFields() {101if ($this->fields === null) {102$field_list = PhabricatorCustomField::getObjectFields(103$this,104PhabricatorCustomField::ROLE_VIEW);105$field_list->readFieldsFromStorage($this);106107$this->fields = $field_list->getFields();108}109return $this->fields;110}111112public function logEvent($type, array $data = array()) {113$log = id(new DrydockLog())114->setEpoch(PhabricatorTime::getNow())115->setType($type)116->setData($data);117118$log->setBlueprintPHID($this->getPHID());119120return $log->save();121}122123public function getURI() {124$id = $this->getID();125return "/drydock/blueprint/{$id}/";126}127128129/* -( Allocating Resources )----------------------------------------------- */130131132/**133* @task resource134*/135public function canEverAllocateResourceForLease(DrydockLease $lease) {136return $this->getImplementation()->canEverAllocateResourceForLease(137$this,138$lease);139}140141142/**143* @task resource144*/145public function canAllocateResourceForLease(DrydockLease $lease) {146return $this->getImplementation()->canAllocateResourceForLease(147$this,148$lease);149}150151152/**153* @task resource154*/155public function allocateResource(DrydockLease $lease) {156return $this->getImplementation()->allocateResource(157$this,158$lease);159}160161162/**163* @task resource164*/165public function activateResource(DrydockResource $resource) {166return $this->getImplementation()->activateResource(167$this,168$resource);169}170171172/**173* @task resource174*/175public function destroyResource(DrydockResource $resource) {176$this->getImplementation()->destroyResource(177$this,178$resource);179return $this;180}181182183/**184* @task resource185*/186public function getResourceName(DrydockResource $resource) {187return $this->getImplementation()->getResourceName(188$this,189$resource);190}191192193/* -( Acquiring Leases )--------------------------------------------------- */194195196/**197* @task lease198*/199public function canAcquireLeaseOnResource(200DrydockResource $resource,201DrydockLease $lease) {202return $this->getImplementation()->canAcquireLeaseOnResource(203$this,204$resource,205$lease);206}207208209/**210* @task lease211*/212public function acquireLease(213DrydockResource $resource,214DrydockLease $lease) {215return $this->getImplementation()->acquireLease(216$this,217$resource,218$lease);219}220221222/**223* @task lease224*/225public function activateLease(226DrydockResource $resource,227DrydockLease $lease) {228return $this->getImplementation()->activateLease(229$this,230$resource,231$lease);232}233234235/**236* @task lease237*/238public function didReleaseLease(239DrydockResource $resource,240DrydockLease $lease) {241$this->getImplementation()->didReleaseLease(242$this,243$resource,244$lease);245return $this;246}247248249/**250* @task lease251*/252public function destroyLease(253DrydockResource $resource,254DrydockLease $lease) {255$this->getImplementation()->destroyLease(256$this,257$resource,258$lease);259return $this;260}261262public function getInterface(263DrydockResource $resource,264DrydockLease $lease,265$type) {266267$interface = $this->getImplementation()268->getInterface($this, $resource, $lease, $type);269270if (!$interface) {271throw new Exception(272pht(273'Unable to build resource interface of type "%s".',274$type));275}276277return $interface;278}279280public function shouldAllocateSupplementalResource(281DrydockResource $resource,282DrydockLease $lease) {283return $this->getImplementation()->shouldAllocateSupplementalResource(284$this,285$resource,286$lease);287}288289290/* -( PhabricatorApplicationTransactionInterface )------------------------- */291292293public function getApplicationTransactionEditor() {294return new DrydockBlueprintEditor();295}296297public function getApplicationTransactionTemplate() {298return new DrydockBlueprintTransaction();299}300301302/* -( PhabricatorPolicyInterface )----------------------------------------- */303304305public function getCapabilities() {306return array(307PhabricatorPolicyCapability::CAN_VIEW,308PhabricatorPolicyCapability::CAN_EDIT,309);310}311312public function getPolicy($capability) {313switch ($capability) {314case PhabricatorPolicyCapability::CAN_VIEW:315return $this->getViewPolicy();316case PhabricatorPolicyCapability::CAN_EDIT:317return $this->getEditPolicy();318}319}320321public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {322return false;323}324325326/* -( PhabricatorCustomFieldInterface )------------------------------------ */327328329public function getCustomFieldSpecificationForRole($role) {330return array();331}332333public function getCustomFieldBaseClass() {334return 'DrydockBlueprintCustomField';335}336337public function getCustomFields() {338return $this->assertAttached($this->customFields);339}340341public function attachCustomFields(PhabricatorCustomFieldAttachment $fields) {342$this->customFields = $fields;343return $this;344}345346347/* -( PhabricatorNgramsInterface )----------------------------------------- */348349350public function newNgrams() {351return array(352id(new DrydockBlueprintNameNgrams())353->setValue($this->getBlueprintName()),354);355}356357358/* -( PhabricatorConduitResultInterface )---------------------------------- */359360361public function getFieldSpecificationsForConduit() {362return array(363id(new PhabricatorConduitSearchFieldSpecification())364->setKey('name')365->setType('string')366->setDescription(pht('The name of this blueprint.')),367id(new PhabricatorConduitSearchFieldSpecification())368->setKey('type')369->setType('string')370->setDescription(pht('The type of resource this blueprint provides.')),371);372}373374public function getFieldValuesForConduit() {375return array(376'name' => $this->getBlueprintName(),377'type' => $this->getImplementation()->getType(),378);379}380381public function getConduitSearchAttachments() {382return array(383);384}385386}387388389