Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/edges/constants/PhabricatorEdgeType.php
- This file was added.
<?php | |||||
/** | |||||
* Defines an edge type. | |||||
* | |||||
* Edges are typed, directed connections between two objects. They are used to | |||||
* represent most simple relationships, like when a user is subscribed to an | |||||
* object or an object is a member of a project. | |||||
* | |||||
* @task load Loading Types | |||||
*/ | |||||
abstract class PhabricatorEdgeType extends Phobject { | |||||
abstract public function getEdgeConstant(); | |||||
public function getInverseEdgeConstant() { | |||||
return null; | |||||
} | |||||
public function shouldPreventCycles() { | |||||
return false; | |||||
} | |||||
public function getTransactionAddString( | |||||
$actor, | |||||
$add_count, | |||||
$add_edges) { | |||||
return pht( | |||||
'%s added %s edge(s): %s.', | |||||
$actor, | |||||
$add_count, | |||||
$add_edges); | |||||
} | |||||
joshuaspence: Would the `$add_edges` argument be better off as an array? Same goes for the other functions. | |||||
public function getTransactionRemoveString( | |||||
$actor, | |||||
$rem_count, | |||||
$rem_edges) { | |||||
return pht( | |||||
'%s removed %s edge(s): %s.', | |||||
$actor, | |||||
$rem_count, | |||||
$rem_edges); | |||||
} | |||||
public function getTransactionEditString( | |||||
$actor, | |||||
$total_count, | |||||
joshuaspenceUnsubmitted Not Done Inline Actions$total_count == $add_count + $rem_count right? If so, we don't need to pass this in explicitly. joshuaspence: `$total_count == $add_count + $rem_count` right? If so, we don't need to pass this in… | |||||
$add_count, | |||||
$add_edges, | |||||
$rem_count, | |||||
$rem_edges) { | |||||
return pht( | |||||
'%s edited %s edges, added %s: %s; removed %s: %s.', | |||||
$actor, | |||||
$total_count, | |||||
$add_count, | |||||
$add_edges, | |||||
$rem_count, | |||||
$rem_edges); | |||||
} | |||||
public function getFeedAddString( | |||||
$actor, | |||||
$object, | |||||
$add_count, | |||||
$add_edges) { | |||||
return pht( | |||||
'%s added %s edge(s) to %s: %s.', | |||||
$actor, | |||||
$add_count, | |||||
$object, | |||||
$add_edges); | |||||
joshuaspenceUnsubmitted Not Done Inline ActionsTo save on code duplication, perhaps just return $this->getTransactionAddString($action, $object, $add_count, $add_edges). joshuaspence: To save on code duplication, perhaps just `return $this->getTransactionAddString($action… | |||||
joshuaspenceUnsubmitted Not Done Inline ActionsOh actually, never mind... the strings are different. joshuaspence: Oh actually, never mind... the strings are different. | |||||
} | |||||
public function getFeedRemoveString( | |||||
$actor, | |||||
$object, | |||||
$rem_count, | |||||
$rem_edges) { | |||||
return pht( | |||||
'%s removed %s edge(s) from %s: %s.', | |||||
$actor, | |||||
$object, | |||||
$rem_count, | |||||
$rem_edges); | |||||
joshuaspenceUnsubmitted Not Done Inline ActionsI think that this should be: return pht( '%s removed %s edge(s) from %s: %s.', $actor, - $object, + $rem_count, - $rem_count, + $object, $rem_edges); joshuaspence: I think that this should be:
```lang=diff
return pht(
'%s removed %s edge(s) from %s… | |||||
epriestleyAuthorUnsubmitted Not Done Inline ActionsOh, good catch. I probably copy/pasted this like five places by now too. I'll clean those up. epriestley: Oh, good catch. I probably copy/pasted this like five places by now too. I'll clean those up. | |||||
joshuaspenceUnsubmitted Not Done Inline ActionsAs above... To save on code duplication, perhaps just return $this->getTransactionRemoveString($action, $object, $rem_count, $rem_edges). joshuaspence: As above... To save on code duplication, perhaps just return $this->getTransactionRemoveString… | |||||
joshuaspenceUnsubmitted Not Done Inline ActionsNevermind. joshuaspence: Nevermind. | |||||
} | |||||
public function getFeedEditString( | |||||
$actor, | |||||
$object, | |||||
$total_count, | |||||
$add_count, | |||||
$add_edges, | |||||
$rem_count, | |||||
$rem_edges) { | |||||
return pht( | |||||
'%s edited %s edges for %s, added %s: %s; removed %s: %s.', | |||||
$actor, | |||||
$total_count, | |||||
$object, | |||||
$add_count, | |||||
$add_edges, | |||||
$rem_count, | |||||
$rem_edges); | |||||
} | |||||
joshuaspenceUnsubmitted Not Done Inline ActionsAs above. joshuaspence: As above. | |||||
joshuaspenceUnsubmitted Not Done Inline ActionsNevermind. joshuaspence: Nevermind. | |||||
/* -( Loading Types )------------------------------------------------------ */ | |||||
/** | |||||
* @task load | |||||
*/ | |||||
public static function getAllTypes() { | |||||
static $type_map; | |||||
if ($type_map === null) { | |||||
$types = id(new PhutilSymbolLoader()) | |||||
->setAncestorClass(__CLASS__) | |||||
->loadObjects(); | |||||
$map = array(); | |||||
// TODO: Remove this once everything is migrated. | |||||
$exclude = mpull($types, 'getEdgeConstant'); | |||||
$map = PhabricatorEdgeConfig::getLegacyTypes($exclude); | |||||
unset($types['PhabricatorLegacyEdgeType']); | |||||
foreach ($types as $class => $type) { | |||||
$const = $type->getEdgeConstant(); | |||||
if (!is_int($const) || ($const <= 0)) { | |||||
throw new Exception( | |||||
pht( | |||||
'Edge type "%s" has an invalid edge constant. Edge constants '. | |||||
'must be positive integers.', | |||||
$class)); | |||||
} | |||||
if (isset($map[$const])) { | |||||
throw new Exception( | |||||
pht( | |||||
'Two edge types ("%s", "%s") share the same edge constant '. | |||||
'(%d). Each edge type must have a unique constant.', | |||||
$class, | |||||
get_class($map[$const]), | |||||
$const)); | |||||
} | |||||
$map[$const] = $type; | |||||
} | |||||
// Check that all the inverse edge definitions actually make sense. If | |||||
// edge type A says B is its inverse, B must exist and say that A is its | |||||
// inverse. | |||||
foreach ($map as $const => $type) { | |||||
$inverse = $type->getInverseEdgeConstant(); | |||||
if ($inverse === null) { | |||||
continue; | |||||
} | |||||
if (empty($map[$inverse])) { | |||||
throw new Exception( | |||||
pht( | |||||
'Edge type "%s" ("%d") defines an inverse type ("%d") which '. | |||||
'does not exist.', | |||||
get_class($type), | |||||
$const, | |||||
$inverse)); | |||||
} | |||||
$inverse_inverse = $map[$inverse]->getInverseEdgeConstant(); | |||||
if ($inverse_inverse !== $const) { | |||||
throw new Exception( | |||||
pht( | |||||
'Edge type "%s" ("%d") defines an inverse type ("%d"), but that '. | |||||
'inverse type defines a different type ("%d") as its '. | |||||
'inverse.', | |||||
get_class($type), | |||||
$const, | |||||
$inverse, | |||||
$inverse_inverse)); | |||||
} | |||||
} | |||||
$type_map = $map; | |||||
} | |||||
return $type_map; | |||||
} | |||||
/** | |||||
* @task load | |||||
*/ | |||||
public static function getByConst($const) { | |||||
$type = idx(self::getAllTypes(), $const); | |||||
if (!$type) { | |||||
throw new Exception( | |||||
pht('Unknown edge constant "%s"!', $const)); | |||||
} | |||||
return $type; | |||||
} | |||||
} |
Would the $add_edges argument be better off as an array? Same goes for the other functions.