Path: blob/master/src/applications/packages/storage/PhabricatorPackagesPackage.php
13450 views
<?php12final class PhabricatorPackagesPackage3extends PhabricatorPackagesDAO4implements5PhabricatorPolicyInterface,6PhabricatorApplicationTransactionInterface,7PhabricatorDestructibleInterface,8PhabricatorSubscribableInterface,9PhabricatorProjectInterface,10PhabricatorConduitResultInterface,11PhabricatorNgramsInterface {1213protected $name;14protected $publisherPHID;15protected $packageKey;16protected $viewPolicy;17protected $editPolicy;1819private $publisher = self::ATTACHABLE;2021public static function initializeNewPackage(PhabricatorUser $actor) {22$packages_application = id(new PhabricatorApplicationQuery())23->setViewer($actor)24->withClasses(array('PhabricatorPackagesApplication'))25->executeOne();2627$view_policy = $packages_application->getPolicy(28PhabricatorPackagesPackageDefaultViewCapability::CAPABILITY);2930$edit_policy = $packages_application->getPolicy(31PhabricatorPackagesPackageDefaultEditCapability::CAPABILITY);3233return id(new self())34->setViewPolicy($view_policy)35->setEditPolicy($edit_policy);36}3738protected function getConfiguration() {39return array(40self::CONFIG_AUX_PHID => true,41self::CONFIG_COLUMN_SCHEMA => array(42'name' => 'sort64',43'packageKey' => 'sort64',44),45self::CONFIG_KEY_SCHEMA => array(46'key_package' => array(47'columns' => array('publisherPHID', 'packageKey'),48'unique' => true,49),50),51) + parent::getConfiguration();52}5354public function generatePHID() {55return PhabricatorPHID::generateNewPHID(56PhabricatorPackagesPackagePHIDType::TYPECONST);57}5859public function getURI() {60$full_key = $this->getFullKey();61return "/package/{$full_key}/";62}6364public function getFullKey() {65$publisher = $this->getPublisher();66$publisher_key = $publisher->getPublisherKey();67$package_key = $this->getPackageKey();68return "{$publisher_key}/{$package_key}";69}7071public function attachPublisher(PhabricatorPackagesPublisher $publisher) {72$this->publisher = $publisher;73return $this;74}7576public function getPublisher() {77return $this->assertAttached($this->publisher);78}7980public static function assertValidPackageName($value) {81$length = phutil_utf8_strlen($value);82if (!$length) {83throw new Exception(84pht(85'Package name "%s" is not valid: package names are required.',86$value));87}8889$max_length = 64;90if ($length > $max_length) {91throw new Exception(92pht(93'Package name "%s" is not valid: package names must not be '.94'more than %s characters long.',95$value,96new PhutilNumber($max_length)));97}98}99100public static function assertValidPackageKey($value) {101$length = phutil_utf8_strlen($value);102if (!$length) {103throw new Exception(104pht(105'Package key "%s" is not valid: package keys are required.',106$value));107}108109$max_length = 64;110if ($length > $max_length) {111throw new Exception(112pht(113'Package key "%s" is not valid: package keys must not be '.114'more than %s characters long.',115$value,116new PhutilNumber($max_length)));117}118119if (!preg_match('/^[a-z]+\z/', $value)) {120throw new Exception(121pht(122'Package key "%s" is not valid: package keys may only contain '.123'lowercase latin letters.',124$value));125}126}127128129/* -( PhabricatorSubscribableInterface )----------------------------------- */130131132public function isAutomaticallySubscribed($phid) {133return false;134}135136137/* -( Policy Interface )--------------------------------------------------- */138139140public function getCapabilities() {141return array(142PhabricatorPolicyCapability::CAN_VIEW,143PhabricatorPolicyCapability::CAN_EDIT,144);145}146147public function getPolicy($capability) {148switch ($capability) {149case PhabricatorPolicyCapability::CAN_VIEW:150return $this->getViewPolicy();151case PhabricatorPolicyCapability::CAN_EDIT:152return $this->getEditPolicy();153}154}155156public function hasAutomaticCapability($capability, PhabricatorUser $user) {157return false;158}159160161/* -( PhabricatorDestructibleInterface )----------------------------------- */162163164public function destroyObjectPermanently(165PhabricatorDestructionEngine $engine) {166$viewer = $engine->getViewer();167168$this->openTransaction();169170$versions = id(new PhabricatorPackagesVersionQuery())171->setViewer($viewer)172->withPackagePHIDs(array($this->getPHID()))173->execute();174foreach ($versions as $version) {175$engine->destroyObject($version);176}177178$this->delete();179180$this->saveTransaction();181}182183184/* -( PhabricatorApplicationTransactionInterface )------------------------- */185186187public function getApplicationTransactionEditor() {188return new PhabricatorPackagesPackageEditor();189}190191public function getApplicationTransactionTemplate() {192return new PhabricatorPackagesPackageTransaction();193}194195196/* -( PhabricatorNgramsInterface )----------------------------------------- */197198199public function newNgrams() {200return array(201id(new PhabricatorPackagesPackageNameNgrams())202->setValue($this->getName()),203);204}205206207/* -( PhabricatorConduitResultInterface )---------------------------------- */208209210public function getFieldSpecificationsForConduit() {211return array(212id(new PhabricatorConduitSearchFieldSpecification())213->setKey('name')214->setType('string')215->setDescription(pht('The name of the package.')),216id(new PhabricatorConduitSearchFieldSpecification())217->setKey('packageKey')218->setType('string')219->setDescription(pht('The unique key of the package.')),220);221}222223public function getFieldValuesForConduit() {224$publisher = $this->getPublisher();225226$publisher_map = array(227'id' => (int)$publisher->getID(),228'phid' => $publisher->getPHID(),229'name' => $publisher->getName(),230'publisherKey' => $publisher->getPublisherKey(),231);232233return array(234'name' => $this->getName(),235'packageKey' => $this->getPackageKey(),236'fullKey' => $this->getFullKey(),237'publisher' => $publisher_map,238);239}240241public function getConduitSearchAttachments() {242return array();243}244245246}247248249