@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 PhabricatorChartEngine
4 extends Phobject {
5
6 private $viewer;
7 private $engineParameters = array();
8
9 const KEY_ENGINE = 'engineKey';
10 const KEY_PARAMETERS = 'engineParameters';
11
12 final public function setViewer(PhabricatorUser $viewer) {
13 $this->viewer = $viewer;
14 return $this;
15 }
16
17 final public function getViewer() {
18 return $this->viewer;
19 }
20
21 final protected function setEngineParameter($key, $value) {
22 $this->engineParameters[$key] = $value;
23 return $this;
24 }
25
26 final protected function getEngineParameter($key, $default = null) {
27 return idx($this->engineParameters, $key, $default);
28 }
29
30 final protected function getEngineParameters() {
31 return $this->engineParameters;
32 }
33
34 final public static function newFromChart(PhabricatorFactChart $chart) {
35 $engine_key = $chart->getChartParameter(self::KEY_ENGINE);
36
37 $engine_map = self::getAllChartEngines();
38 if (!isset($engine_map[$engine_key])) {
39 throw new Exception(
40 pht(
41 'Chart uses unknown engine key ("%s") and can not be rendered.',
42 $engine_key));
43 }
44
45 return clone id($engine_map[$engine_key]);
46 }
47
48 /**
49 * Load all available chart engines.
50 * @return list<PhabricatorChartEngine> All available chart engines.
51 */
52 final public static function getAllChartEngines() {
53 return id(new PhutilClassMapQuery())
54 ->setAncestorClass(self::class)
55 ->setUniqueMethod('getChartEngineKey')
56 ->execute();
57 }
58
59 final public function getChartEngineKey() {
60 return $this->getPhobjectClassConstant('CHARTENGINEKEY', 32);
61 }
62
63 final public function buildChart(PhabricatorFactChart $chart) {
64 $map = $chart->getChartParameter(self::KEY_PARAMETERS, array());
65 return $this->newChart($chart, $map);
66 }
67
68 abstract protected function newChart(PhabricatorFactChart $chart, array $map);
69
70 final public function newStoredChart() {
71 $viewer = $this->getViewer();
72
73 $parameters = $this->getEngineParameters();
74
75 $chart = id(new PhabricatorFactChart())
76 ->setChartParameter(self::KEY_ENGINE, $this->getChartEngineKey())
77 ->setChartParameter(self::KEY_PARAMETERS, $this->getEngineParameters());
78
79 $rendering_engine = id(new PhabricatorChartRenderingEngine())
80 ->setViewer($viewer)
81 ->setChart($chart);
82
83 return $rendering_engine->getStoredChart();
84 }
85
86 final public function buildChartPanel() {
87 $chart = $this->newStoredChart();
88
89 $panel_type = id(new PhabricatorDashboardChartPanelType())
90 ->getPanelTypeKey();
91
92 $chart_panel = id(new PhabricatorDashboardPanel())
93 ->setPanelType($panel_type)
94 ->setProperty('chartKey', $chart->getChartKey());
95
96 return $chart_panel;
97 }
98
99 final protected function newFunction($name /* , ... */) {
100 $argv = func_get_args();
101 return id(new PhabricatorComposeChartFunction())
102 ->setArguments($argv);
103 }
104
105}