1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-27 09:12:41 +01:00

Further simplify PhabricatorCustomFieldInterface

Summary:
Ref T1703. Ref T3718. This introduces `PhabricatorCustomFieldAttachment`, which is just a fancy `array()`. The goal here is to simplify `PhabricatorCustomFieldInterface` as much as possible.

In particular, it can now use common infrastructure (`assertAttached()`) and is more difficult to get wrong.

Test Plan: Edited custom fields on profile.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T1703, T3718

Differential Revision: https://secure.phabricator.com/D6752
This commit is contained in:
epriestley 2013-08-14 09:53:59 -07:00
parent 938b63aaa9
commit 026137f92f
5 changed files with 54 additions and 32 deletions

View file

@ -1025,6 +1025,7 @@ phutil_register_library_map(array(
'PhabricatorCrumbsView' => 'view/layout/PhabricatorCrumbsView.php', 'PhabricatorCrumbsView' => 'view/layout/PhabricatorCrumbsView.php',
'PhabricatorCursorPagedPolicyAwareQuery' => 'infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php', 'PhabricatorCursorPagedPolicyAwareQuery' => 'infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php',
'PhabricatorCustomField' => 'infrastructure/customfield/field/PhabricatorCustomField.php', 'PhabricatorCustomField' => 'infrastructure/customfield/field/PhabricatorCustomField.php',
'PhabricatorCustomFieldAttachment' => 'infrastructure/customfield/field/PhabricatorCustomFieldAttachment.php',
'PhabricatorCustomFieldConfigOptionType' => 'infrastructure/customfield/config/PhabricatorCustomFieldConfigOptionType.php', 'PhabricatorCustomFieldConfigOptionType' => 'infrastructure/customfield/config/PhabricatorCustomFieldConfigOptionType.php',
'PhabricatorCustomFieldDataNotAvailableException' => 'infrastructure/customfield/exception/PhabricatorCustomFieldDataNotAvailableException.php', 'PhabricatorCustomFieldDataNotAvailableException' => 'infrastructure/customfield/exception/PhabricatorCustomFieldDataNotAvailableException.php',
'PhabricatorCustomFieldImplementationIncompleteException' => 'infrastructure/customfield/exception/PhabricatorCustomFieldImplementationIncompleteException.php', 'PhabricatorCustomFieldImplementationIncompleteException' => 'infrastructure/customfield/exception/PhabricatorCustomFieldImplementationIncompleteException.php',

View file

@ -36,7 +36,7 @@ final class PhabricatorUser
private $status = self::ATTACHABLE; private $status = self::ATTACHABLE;
private $preferences = null; private $preferences = null;
private $omnipotent = false; private $omnipotent = false;
private $customFields = array(); private $customFields = self::ATTACHABLE;
protected function readField($field) { protected function readField($field) {
switch ($field) { switch ($field) {
@ -843,15 +843,12 @@ EOBODY;
return 'PhabricatorUserCustomField'; return 'PhabricatorUserCustomField';
} }
public function getCustomFields($role) { public function getCustomFields() {
if (idx($this->customFields, $role) === null) { return $this->assertAttached($this->customFields);
PhabricatorCustomField::raiseUnattachedException($this, $role);
}
return $this->customFields[$role];
} }
public function attachCustomFields($role, PhabricatorCustomFieldList $list) { public function attachCustomFields(PhabricatorCustomFieldAttachment $fields) {
$this->customFields[$role] = $list; $this->customFields = $fields;
return $this; return $this;
} }

View file

@ -30,17 +30,6 @@ abstract class PhabricatorCustomField {
/* -( Building Applications with Custom Fields )--------------------------- */ /* -( Building Applications with Custom Fields )--------------------------- */
/**
* @task apps
*/
public static function raiseUnattachedException(
PhabricatorCustomFieldInterface $object,
$role) {
throw new PhabricatorCustomFieldNotAttachedException(
"Call attachCustomFields() before getCustomFields()!");
}
/** /**
* @task apps * @task apps
*/ */
@ -49,7 +38,14 @@ abstract class PhabricatorCustomField {
$role) { $role) {
try { try {
$field_list = $object->getCustomFields($role); $attachment = $object->getCustomFields();
} catch (PhabricatorDataNotAttachedException $ex) {
$attachment = new PhabricatorCustomFieldAttachment();
$object->attachCustomFields($attachment);
}
try {
$field_list = $attachment->getCustomFieldList($role);
} catch (PhabricatorCustomFieldNotAttachedException $ex) { } catch (PhabricatorCustomFieldNotAttachedException $ex) {
$base_class = $object->getCustomFieldBaseClass(); $base_class = $object->getCustomFieldBaseClass();
@ -74,7 +70,7 @@ abstract class PhabricatorCustomField {
} }
$field_list = new PhabricatorCustomFieldList($fields); $field_list = new PhabricatorCustomFieldList($fields);
$object->attachCustomFields($role, $field_list); $attachment->addCustomFieldList($role, $field_list);
} }
return $field_list; return $field_list;
@ -88,7 +84,10 @@ abstract class PhabricatorCustomField {
PhabricatorCustomFieldInterface $object, PhabricatorCustomFieldInterface $object,
$role, $role,
$field_key) { $field_key) {
return idx(self::getObjectFields($object, $role)->getFields(), $field_key);
$fields = self::getObjectFields($object, $role)->getFields();
return idx($fields, $field_key);
} }

View file

@ -0,0 +1,28 @@
<?php
/**
* Convenience class which simplifies the implementation of
* @{interface:PhabricatorCustomFieldInterface} by obscuring the details of how
* custom fields are stored.
*
* Generally, you should not use this class directly. It is used by
* @{class:PhabricatorCustomField} to manage field storage on objects.
*/
final class PhabricatorCustomFieldAttachment {
private $lists = array();
public function addCustomFieldList($role, PhabricatorCustomFieldList $list) {
$this->lists[$role] = $list;
return $this;
}
public function getCustomFieldList($role) {
if (empty($this->lists[$role])) {
throw new PhabricatorCustomFieldNotAttachedException(
"Role list '{$role}' is not available!");
}
return $this->lists[$role];
}
}

View file

@ -4,8 +4,8 @@ interface PhabricatorCustomFieldInterface {
public function getCustomFieldBaseClass(); public function getCustomFieldBaseClass();
public function getCustomFieldSpecificationForRole($role); public function getCustomFieldSpecificationForRole($role);
public function getCustomFields($role); public function getCustomFields();
public function attachCustomFields($role, PhabricatorCustomFieldList $list); public function attachCustomFields(PhabricatorCustomFieldAttachment $fields);
} }
@ -16,7 +16,7 @@ interface PhabricatorCustomFieldInterface {
/* -( PhabricatorCustomFieldInterface )------------------------------------ */ /* -( PhabricatorCustomFieldInterface )------------------------------------ */
/* /*
private $customFields = array(); private $customFields = self::ATTACHABLE;
public function getCustomFieldSpecificationForRole($role) { public function getCustomFieldSpecificationForRole($role) {
return PhabricatorEnv::getEnvConfig(<<<'application.fields'>>>); return PhabricatorEnv::getEnvConfig(<<<'application.fields'>>>);
@ -26,15 +26,12 @@ interface PhabricatorCustomFieldInterface {
return <<<<'YourApplicationHereCustomField'>>>>; return <<<<'YourApplicationHereCustomField'>>>>;
} }
public function getCustomFields($role) { public function getCustomFields() {
if (idx($this->customFields, $role) === null) { return $this->assertAttached($this->customFields);
PhabricatorCustomField::raiseUnattachedException($this, $role);
}
return $this->customFields[$role];
} }
public function attachCustomFields($role, PhabricatorCustomFieldList $list) { public function attachCustomFields(PhabricatorCustomFieldAttachment $fields) {
$this->customFields[$role] = $list; $this->customFields = $fields;
return $this; return $this;
} }