Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/customfield/query/PhabricatorCustomFieldStorageQuery.php
13419 views
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
*/
10
final class PhabricatorCustomFieldStorageQuery extends Phobject {
11
12
private $fieldMap = array();
13
private $storageSources = array();
14
15
public function addFields(array $fields) {
16
assert_instances_of($fields, 'PhabricatorCustomField');
17
18
foreach ($fields as $field) {
19
$this->addField($field);
20
}
21
22
return $this;
23
}
24
25
public function addField(PhabricatorCustomField $field) {
26
$role_storage = PhabricatorCustomField::ROLE_STORAGE;
27
28
if (!$field->shouldEnableForRole($role_storage)) {
29
return $this;
30
}
31
32
$storage = $field->newStorageObject();
33
$source_key = $storage->getStorageSourceKey();
34
35
$this->fieldMap[$source_key][] = $field;
36
37
if (empty($this->storageSources[$source_key])) {
38
$this->storageSources[$source_key] = $storage;
39
}
40
41
return $this;
42
}
43
44
public function execute() {
45
foreach ($this->storageSources as $source_key => $storage) {
46
$fields = idx($this->fieldMap, $source_key, array());
47
$this->loadFieldsFromStorage($storage, $fields);
48
}
49
}
50
51
private function loadFieldsFromStorage($storage, array $fields) {
52
// Only try to load fields which have a persisted object.
53
$loadable = array();
54
foreach ($fields as $key => $field) {
55
$object = $field->getObject();
56
$phid = $object->getPHID();
57
if (!$phid) {
58
continue;
59
}
60
61
$loadable[$key] = $field;
62
}
63
64
if ($loadable) {
65
$data = $storage->loadStorageSourceData($loadable);
66
} else {
67
$data = array();
68
}
69
70
foreach ($fields as $key => $field) {
71
if (array_key_exists($key, $data)) {
72
$value = $data[$key];
73
$field->setValueFromStorage($value);
74
$field->didSetValueFromStorage();
75
} else if (isset($loadable[$key])) {
76
// NOTE: We set this only if the object exists. Otherwise, we allow
77
// the field to retain any default value it may have.
78
$field->setValueFromStorage(null);
79
$field->didSetValueFromStorage();
80
}
81
}
82
}
83
84
}
85
86