Changeset View
Changeset View
Standalone View
Standalone View
src/applications/phid/type/PhabricatorPHIDType.php
<?php | <?php | ||||
abstract class PhabricatorPHIDType { | abstract class PhabricatorPHIDType { | ||||
abstract public function getTypeConstant(); | abstract public function getTypeConstant(); | ||||
abstract public function getTypeName(); | abstract public function getTypeName(); | ||||
public function newObject() { | public function newObject() { | ||||
return null; | return null; | ||||
} | } | ||||
public function getTypeIcon() { | public function getTypeIcon() { | ||||
return null; | return null; | ||||
} | } | ||||
/** | |||||
* Get the class name for the application this type belongs to. | |||||
* | |||||
* @return string|null Class name of the corresponding application, or null | |||||
* if the type is not bound to an application. | |||||
*/ | |||||
public function getPHIDTypeApplicationClass() { | |||||
// TODO: Some day this should probably be abstract, but for now it only | |||||
// affects global search and there's no real burning need to go classify | |||||
// every PHID type. | |||||
return null; | |||||
} | |||||
/** | /** | ||||
* Build a @{class:PhabricatorPolicyAwareQuery} to load objects of this type | * Build a @{class:PhabricatorPolicyAwareQuery} to load objects of this type | ||||
* by PHID. | * by PHID. | ||||
* | * | ||||
* If you can not build a single query which satisfies this requirement, you | * If you can not build a single query which satisfies this requirement, you | ||||
* can provide a dummy implementation for this method and overload | * can provide a dummy implementation for this method and overload | ||||
* @{method:loadObjects} instead. | * @{method:loadObjects} instead. | ||||
* | * | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | abstract class PhabricatorPHIDType { | ||||
} | } | ||||
public function loadNamedObjects( | public function loadNamedObjects( | ||||
PhabricatorObjectQuery $query, | PhabricatorObjectQuery $query, | ||||
array $names) { | array $names) { | ||||
throw new Exception("Not implemented!"); | throw new Exception("Not implemented!"); | ||||
} | } | ||||
/** | |||||
* Get all known PHID types. | |||||
* | |||||
* To get PHID types a given user has access to, see | |||||
* @{method:getAllInstalledTypes}. | |||||
* | |||||
* @return dict<string, PhabricatorPHIDType> Map of type constants to types. | |||||
*/ | |||||
public static function getAllTypes() { | public static function getAllTypes() { | ||||
static $types; | static $types; | ||||
if ($types === null) { | if ($types === null) { | ||||
$objects = id(new PhutilSymbolLoader()) | $objects = id(new PhutilSymbolLoader()) | ||||
->setAncestorClass(__CLASS__) | ->setAncestorClass(__CLASS__) | ||||
->loadObjects(); | ->loadObjects(); | ||||
$map = array(); | $map = array(); | ||||
Show All 14 Lines | if ($types === null) { | ||||
} | } | ||||
$types = $map; | $types = $map; | ||||
} | } | ||||
return $types; | return $types; | ||||
} | } | ||||
/** | |||||
* Get all PHID types of applications installed for a given viewer. | |||||
* | |||||
* @param PhabricatorUser Viewing user. | |||||
* @return dict<string, PhabricatorPHIDType> Map of constants to installed | |||||
* types. | |||||
*/ | |||||
public static function getAllInstalledTypes(PhabricatorUser $viewer) { | |||||
$all_types = self::getAllTypes(); | |||||
$installed_types = array(); | |||||
$app_classes = array(); | |||||
foreach ($all_types as $key => $type) { | |||||
$app_class = $type->getPHIDTypeApplicationClass(); | |||||
if ($app_class === null) { | |||||
// If the PHID type isn't bound to an application, include it as | |||||
// installed. | |||||
$installed_types[$key] = $type; | |||||
continue; | |||||
} | |||||
// Otherwise, we need to check if this application is installed before | |||||
// including the PHID type. | |||||
$app_classes[$app_class][$key] = $type; | |||||
} | |||||
if ($app_classes) { | |||||
$apps = id(new PhabricatorApplicationQuery()) | |||||
->setViewer($viewer) | |||||
->withInstalled(true) | |||||
->withClasses(array_keys($app_classes)) | |||||
->execute(); | |||||
foreach ($apps as $app_class => $app) { | |||||
$installed_types += $app_classes[$app_class]; | |||||
} | |||||
} | |||||
return $installed_types; | |||||
} | |||||
} | } |