@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3abstract 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(self::class)
18 ->setUniqueMethod('getSourceTypeConstant')
19 ->execute();
20 }
21
22 /**
23 * Construct a new content source object.
24 *
25 * @param string $source The source type constant to build a source for.
26 * @param array $params (optional) Source parameters.
27 * @param bool $force (optional) True to suppress errors and force
28 * construction of a source 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 /**
85 * Get the internal source name
86 *
87 * This is usually coming from a SOURCECONST constant.
88 *
89 * @return string|null
90 */
91 final public function getSource() {
92 return $this->source;
93 }
94
95 final public function getContentSourceParameter($key, $default = null) {
96 return idx($this->params, $key, $default);
97 }
98
99}