@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * Load custom field data from storage.
5 *
6 * This query loads the data directly into the field objects and does not
7 * return it to the caller. It can bulk load data for any list of fields,
8 * even if they have different objects or object types.
9 */
10final class PhabricatorCustomFieldStorageQuery extends Phobject {
11
12 private $fieldMap = array();
13 private $storageSources = array();
14
15 /**
16 * @param array<PhabricatorCustomField> $fields
17 */
18 public function addFields(array $fields) {
19 assert_instances_of($fields, PhabricatorCustomField::class);
20
21 foreach ($fields as $field) {
22 $this->addField($field);
23 }
24
25 return $this;
26 }
27
28 public function addField(PhabricatorCustomField $field) {
29 $role_storage = PhabricatorCustomField::ROLE_STORAGE;
30
31 if (!$field->shouldEnableForRole($role_storage)) {
32 return $this;
33 }
34
35 $storage = $field->newStorageObject();
36 $source_key = $storage->getStorageSourceKey();
37
38 $this->fieldMap[$source_key][] = $field;
39
40 if (empty($this->storageSources[$source_key])) {
41 $this->storageSources[$source_key] = $storage;
42 }
43
44 return $this;
45 }
46
47 public function execute() {
48 foreach ($this->storageSources as $source_key => $storage) {
49 $fields = idx($this->fieldMap, $source_key, array());
50 $this->loadFieldsFromStorage($storage, $fields);
51 }
52 }
53
54 private function loadFieldsFromStorage($storage, array $fields) {
55 // Only try to load fields which have a persisted object.
56 $loadable = array();
57 foreach ($fields as $key => $field) {
58 $object = $field->getObject();
59 $phid = $object->getPHID();
60 if (!$phid) {
61 continue;
62 }
63
64 $loadable[$key] = $field;
65 }
66
67 if ($loadable) {
68 $data = $storage->loadStorageSourceData($loadable);
69 } else {
70 $data = array();
71 }
72
73 foreach ($fields as $key => $field) {
74 if (array_key_exists($key, $data)) {
75 $value = $data[$key];
76 $field->setValueFromStorage($value);
77 $field->didSetValueFromStorage();
78 } else if (isset($loadable[$key])) {
79 // NOTE: We set this only if the object exists. Otherwise, we allow
80 // the field to retain any default value it may have.
81 $field->setValueFromStorage(null);
82 $field->didSetValueFromStorage();
83 }
84 }
85 }
86
87}