Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/differential/field/DifferentialCommitMessageField.php
12256 views
1
<?php
2
3
abstract class DifferentialCommitMessageField
4
extends Phobject {
5
6
private $viewer;
7
private $customFieldStorage;
8
9
final public function setViewer(PhabricatorUser $viewer) {
10
$this->viewer = $viewer;
11
return $this;
12
}
13
14
final public function getViewer() {
15
return $this->viewer;
16
}
17
18
final public function setCustomFieldStorage(array $custom_field_storage) {
19
$this->customFieldStorage = $custom_field_storage;
20
return $this;
21
}
22
23
final public function getCustomFieldStorage() {
24
return $this->customFieldStorage;
25
}
26
27
abstract public function getFieldName();
28
abstract public function getFieldOrder();
29
30
public function isFieldEnabled() {
31
return true;
32
}
33
34
public function getFieldAliases() {
35
return array();
36
}
37
38
public function validateFieldValue($value) {
39
return;
40
}
41
42
public function parseFieldValue($value) {
43
return $value;
44
}
45
46
public function isFieldEditable() {
47
return true;
48
}
49
50
public function isTemplateField() {
51
return true;
52
}
53
54
public function readFieldValueFromConduit($value) {
55
return $this->readStringFieldValueFromConduit($value);
56
}
57
58
public function readFieldValueFromObject(DifferentialRevision $revision) {
59
return null;
60
}
61
62
public function renderFieldValue($value) {
63
if ($value === null || !strlen($value)) {
64
return null;
65
}
66
67
return $value;
68
}
69
70
public function getFieldTransactions($value) {
71
if (!$this->isFieldEditable()) {
72
return array();
73
}
74
throw new PhutilMethodNotImplementedException();
75
}
76
77
final public function getCommitMessageFieldKey() {
78
return $this->getPhobjectClassConstant('FIELDKEY', 64);
79
}
80
81
final public static function newEnabledFields(PhabricatorUser $viewer) {
82
$fields = self::getAllFields();
83
84
$results = array();
85
foreach ($fields as $key => $field) {
86
$field = clone $field;
87
$field->setViewer($viewer);
88
if ($field->isFieldEnabled()) {
89
$results[$key] = $field;
90
}
91
}
92
93
return $results;
94
}
95
96
final public static function getAllFields() {
97
return id(new PhutilClassMapQuery())
98
->setAncestorClass(__CLASS__)
99
->setUniqueMethod('getCommitMessageFieldKey')
100
->setSortMethod('getFieldOrder')
101
->execute();
102
}
103
104
protected function raiseParseException($message) {
105
throw new DifferentialFieldParseException($message);
106
}
107
108
protected function raiseValidationException($message) {
109
throw new DifferentialFieldValidationException($message);
110
}
111
112
protected function parseObjectList(
113
$value,
114
array $types,
115
$allow_partial = false,
116
array $suffixes = array()) {
117
return id(new PhabricatorObjectListQuery())
118
->setViewer($this->getViewer())
119
->setAllowedTypes($types)
120
->setObjectList($value)
121
->setAllowPartialResults($allow_partial)
122
->setSuffixes($suffixes)
123
->execute();
124
}
125
126
protected function renderHandleList(array $phids, array $suffixes = array()) {
127
if (!$phids) {
128
return null;
129
}
130
131
$handles = $this->getViewer()->loadHandles($phids);
132
133
$out = array();
134
foreach ($handles as $handle) {
135
$phid = $handle->getPHID();
136
137
if ($handle->getPolicyFiltered()) {
138
$token = $phid;
139
} else if ($handle->isComplete()) {
140
$token = $handle->getCommandLineObjectName();
141
}
142
143
$suffix = idx($suffixes, $phid);
144
$token = $token.$suffix;
145
146
$out[] = $token;
147
}
148
149
return implode(', ', $out);
150
}
151
152
protected function readStringFieldValueFromConduit($value) {
153
if ($value === null) {
154
return $value;
155
}
156
157
if (!is_string($value)) {
158
throw new Exception(
159
pht(
160
'Field "%s" expects a string value, but received a value of type '.
161
'"%s".',
162
$this->getCommitMessageFieldKey(),
163
gettype($value)));
164
}
165
166
return $value;
167
}
168
169
protected function readStringListFieldValueFromConduit($value) {
170
if (!is_array($value)) {
171
throw new Exception(
172
pht(
173
'Field "%s" expects a list of strings, but received a value of type '.
174
'"%s".',
175
$this->getCommitMessageFieldKey(),
176
gettype($value)));
177
}
178
179
return $value;
180
}
181
182
protected function isCustomFieldEnabled($key) {
183
$field_list = PhabricatorCustomField::getObjectFields(
184
new DifferentialRevision(),
185
DifferentialCustomField::ROLE_DEFAULT);
186
187
$fields = $field_list->getFields();
188
return isset($fields[$key]);
189
}
190
191
}
192
193