Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/contentsource/PhabricatorContentSource.php
12241 views
1
<?php
2
3
abstract class PhabricatorContentSource extends Phobject {
4
5
private $source;
6
private $params = array();
7
8
abstract public function getSourceName();
9
abstract public function getSourceDescription();
10
11
final public function getSourceTypeConstant() {
12
return $this->getPhobjectClassConstant('SOURCECONST', 32);
13
}
14
15
final public static function getAllContentSources() {
16
return id(new PhutilClassMapQuery())
17
->setAncestorClass(__CLASS__)
18
->setUniqueMethod('getSourceTypeConstant')
19
->execute();
20
}
21
22
/**
23
* Construct a new content source object.
24
*
25
* @param const The source type constant to build a source for.
26
* @param array Source parameters.
27
* @param bool True to suppress errors and force construction of a source
28
* even if the source type is not valid.
29
* @return PhabricatorContentSource New source object.
30
*/
31
final public static function newForSource(
32
$source,
33
array $params = array(),
34
$force = false) {
35
36
$map = self::getAllContentSources();
37
if (isset($map[$source])) {
38
$obj = clone $map[$source];
39
} else {
40
if ($force) {
41
$obj = new PhabricatorUnknownContentSource();
42
} else {
43
throw new Exception(
44
pht(
45
'Content source type "%s" is unknown.',
46
$source));
47
}
48
}
49
50
$obj->source = $source;
51
$obj->params = $params;
52
53
return $obj;
54
}
55
56
public static function newFromSerialized($serialized) {
57
$dict = json_decode($serialized, true);
58
if (!is_array($dict)) {
59
$dict = array();
60
}
61
62
$source = idx($dict, 'source');
63
$params = idx($dict, 'params');
64
if (!is_array($params)) {
65
$params = array();
66
}
67
68
return self::newForSource($source, $params, true);
69
}
70
71
public static function newFromRequest(AphrontRequest $request) {
72
return self::newForSource(
73
PhabricatorWebContentSource::SOURCECONST);
74
}
75
76
final public function serialize() {
77
return phutil_json_encode(
78
array(
79
'source' => $this->getSource(),
80
'params' => $this->params,
81
));
82
}
83
84
final public function getSource() {
85
return $this->source;
86
}
87
88
final public function getContentSourceParameter($key, $default = null) {
89
return idx($this->params, $key, $default);
90
}
91
92
}
93
94