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