@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 PHUICurtainExtension extends Phobject {
4
5 private $viewer;
6
7 public function setViewer(PhabricatorUser $viewer) {
8 $this->viewer = $viewer;
9 return $this;
10 }
11
12 public function getViewer() {
13 return $this->viewer;
14 }
15
16 abstract public function shouldEnableForObject($object);
17 abstract public function getExtensionApplication();
18
19 public function buildCurtainPanels($object) {
20 $panel = $this->buildCurtainPanel($object);
21
22 if ($panel !== null) {
23 return array($panel);
24 }
25
26 return array();
27 }
28
29 public function buildCurtainPanel($object) {
30 throw new PhutilMethodNotImplementedException();
31 }
32
33 final public function getExtensionKey() {
34 return $this->getPhobjectClassConstant('EXTENSIONKEY');
35 }
36
37 final public static function getAllExtensions() {
38 return id(new PhutilClassMapQuery())
39 ->setAncestorClass(self::class)
40 ->setUniqueMethod('getExtensionKey')
41 ->execute();
42 }
43
44 protected function newPanel() {
45 return new PHUICurtainPanelView();
46 }
47
48 final public static function buildExtensionPanels(
49 PhabricatorUser $viewer,
50 $object) {
51
52 $extensions = self::getAllExtensions();
53 foreach ($extensions as $extension) {
54 $extension->setViewer($viewer);
55 }
56
57 foreach ($extensions as $key => $extension) {
58 $application = $extension->getExtensionApplication();
59 if (!($application instanceof PhabricatorApplication)) {
60 throw new Exception(
61 pht(
62 'Curtain extension ("%s", of class "%s") did not return an '.
63 'application from method "%s". This method must return an '.
64 'object of class "%s".',
65 $key,
66 get_class($extension),
67 'getExtensionApplication()',
68 'PhabricatorApplication'));
69 }
70
71 $has_application = PhabricatorApplication::isClassInstalledForViewer(
72 get_class($application),
73 $viewer);
74
75 if (!$has_application) {
76 unset($extensions[$key]);
77 }
78 }
79
80 foreach ($extensions as $key => $extension) {
81 if (!$extension->shouldEnableForObject($object)) {
82 unset($extensions[$key]);
83 }
84 }
85
86 $result = array();
87
88 foreach ($extensions as $key => $extension) {
89 $panels = $extension->buildCurtainPanels($object);
90 if (!is_array($panels)) {
91 throw new Exception(
92 pht(
93 'Curtain extension ("%s", of class "%s") did not return a list of '.
94 'curtain panels from method "%s". This method must return an '.
95 'array, and each value in the array must be a "%s" object.',
96 $key,
97 get_class($extension),
98 'buildCurtainPanels()',
99 'PHUICurtainPanelView'));
100 }
101
102 foreach ($panels as $panel_key => $panel) {
103 if (!($panel instanceof PHUICurtainPanelView)) {
104 throw new Exception(
105 pht(
106 'Curtain extension ("%s", of class "%s") returned a list of '.
107 'curtain panels from "%s" that contains an invalid value: '.
108 'a value (with key "%s") is not an object of class "%s". '.
109 'Each item in the returned array must be a panel.',
110 $key,
111 get_class($extension),
112 'buildCurtainPanels()',
113 $panel_key,
114 'PHUICurtainPanelView'));
115 }
116
117 $result[] = $panel;
118 }
119 }
120
121 return $result;
122 }
123
124}