Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/config/option/PhabricatorApplicationConfigOptions.php
12256 views
1
<?php
2
3
abstract class PhabricatorApplicationConfigOptions extends Phobject {
4
5
abstract public function getName();
6
abstract public function getDescription();
7
abstract public function getGroup();
8
abstract public function getOptions();
9
10
public function getIcon() {
11
return 'fa-sliders';
12
}
13
14
public function validateOption(PhabricatorConfigOption $option, $value) {
15
if ($value === $option->getDefault()) {
16
return;
17
}
18
19
if ($value === null) {
20
return;
21
}
22
23
$type = $option->newOptionType();
24
if ($type) {
25
try {
26
$type->validateStoredValue($option, $value);
27
$this->didValidateOption($option, $value);
28
} catch (PhabricatorConfigValidationException $ex) {
29
throw $ex;
30
} catch (Exception $ex) {
31
// If custom validators threw exceptions other than validation
32
// exceptions, convert them to validation exceptions so we repair the
33
// configuration and raise an error.
34
throw new PhabricatorConfigValidationException($ex->getMessage());
35
}
36
37
return;
38
}
39
40
if ($option->isCustomType()) {
41
try {
42
return $option->getCustomObject()->validateOption($option, $value);
43
} catch (Exception $ex) {
44
throw new PhabricatorConfigValidationException($ex->getMessage());
45
}
46
} else {
47
throw new Exception(
48
pht(
49
'Unknown configuration option type "%s".',
50
$option->getType()));
51
}
52
53
$this->didValidateOption($option, $value);
54
}
55
56
protected function didValidateOption(
57
PhabricatorConfigOption $option,
58
$value) {
59
// Hook for subclasses to do complex validation.
60
return;
61
}
62
63
/**
64
* Hook to render additional hints based on, e.g., the viewing user, request,
65
* or other context. For example, this is used to show workspace IDs when
66
* configuring `asana.workspace-id`.
67
*
68
* @param PhabricatorConfigOption Option being rendered.
69
* @param AphrontRequest Active request.
70
* @return wild Additional contextual description
71
* information.
72
*/
73
public function renderContextualDescription(
74
PhabricatorConfigOption $option,
75
AphrontRequest $request) {
76
return null;
77
}
78
79
public function getKey() {
80
$class = get_class($this);
81
$matches = null;
82
if (preg_match('/^Phabricator(.*)ConfigOptions$/', $class, $matches)) {
83
return strtolower($matches[1]);
84
}
85
return strtolower(get_class($this));
86
}
87
88
final protected function newOption($key, $type, $default) {
89
return id(new PhabricatorConfigOption())
90
->setKey($key)
91
->setType($type)
92
->setDefault($default)
93
->setGroup($this);
94
}
95
96
final public static function loadAll($external_only = false) {
97
$symbols = id(new PhutilSymbolLoader())
98
->setAncestorClass(__CLASS__)
99
->setConcreteOnly(true)
100
->selectAndLoadSymbols();
101
102
$groups = array();
103
foreach ($symbols as $symbol) {
104
if ($external_only && $symbol['library'] == 'phabricator') {
105
continue;
106
}
107
108
$obj = newv($symbol['name'], array());
109
$key = $obj->getKey();
110
if (isset($groups[$key])) {
111
$pclass = get_class($groups[$key]);
112
$nclass = $symbol['name'];
113
114
throw new Exception(
115
pht(
116
"Multiple %s subclasses have the same key ('%s'): %s, %s.",
117
__CLASS__,
118
$key,
119
$pclass,
120
$nclass));
121
}
122
$groups[$key] = $obj;
123
}
124
125
return $groups;
126
}
127
128
final public static function loadAllOptions($external_only = false) {
129
$groups = self::loadAll($external_only);
130
131
$options = array();
132
foreach ($groups as $group) {
133
foreach ($group->getOptions() as $option) {
134
$key = $option->getKey();
135
if (isset($options[$key])) {
136
throw new Exception(
137
pht(
138
"Multiple %s subclasses contain an option named '%s'!",
139
__CLASS__,
140
$key));
141
}
142
$options[$key] = $option;
143
}
144
}
145
146
return $options;
147
}
148
149
/**
150
* Deformat a HEREDOC for use in remarkup by converting line breaks to
151
* spaces.
152
*/
153
final protected function deformat($string) {
154
return preg_replace('/(?<=\S)\n(?=\S)/', ' ', $string);
155
}
156
157
}
158
159