Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/conduit/query/PhabricatorConduitMethodQuery.php
12262 views
1
<?php
2
3
final class PhabricatorConduitMethodQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
private $isDeprecated;
7
private $isStable;
8
private $isUnstable;
9
private $applicationNames;
10
private $nameContains;
11
private $methods;
12
private $isInternal;
13
14
public function withMethods(array $methods) {
15
$this->methods = $methods;
16
return $this;
17
}
18
19
public function withNameContains($name_contains) {
20
$this->nameContains = $name_contains;
21
return $this;
22
}
23
24
public function withIsStable($is_stable) {
25
$this->isStable = $is_stable;
26
return $this;
27
}
28
29
public function withIsUnstable($is_unstable) {
30
$this->isUnstable = $is_unstable;
31
return $this;
32
}
33
34
public function withIsDeprecated($is_deprecated) {
35
$this->isDeprecated = $is_deprecated;
36
return $this;
37
}
38
39
public function withIsInternal($is_internal) {
40
$this->isInternal = $is_internal;
41
return $this;
42
}
43
44
protected function loadPage() {
45
$methods = $this->getAllMethods();
46
$methods = $this->filterMethods($methods);
47
return $methods;
48
}
49
50
private function getAllMethods() {
51
return id(new PhutilClassMapQuery())
52
->setAncestorClass('ConduitAPIMethod')
53
->setSortMethod('getSortOrder')
54
->execute();
55
}
56
57
private function filterMethods(array $methods) {
58
foreach ($methods as $key => $method) {
59
$application = $method->getApplication();
60
if (!$application) {
61
continue;
62
}
63
if (!$application->isInstalled()) {
64
unset($methods[$key]);
65
}
66
}
67
68
$status = array(
69
ConduitAPIMethod::METHOD_STATUS_STABLE => $this->isStable,
70
ConduitAPIMethod::METHOD_STATUS_FROZEN => $this->isStable,
71
ConduitAPIMethod::METHOD_STATUS_DEPRECATED => $this->isDeprecated,
72
ConduitAPIMethod::METHOD_STATUS_UNSTABLE => $this->isUnstable,
73
);
74
75
// Only apply status filters if any of them are set.
76
if (array_filter($status)) {
77
foreach ($methods as $key => $method) {
78
$keep = idx($status, $method->getMethodStatus());
79
if (!$keep) {
80
unset($methods[$key]);
81
}
82
}
83
}
84
85
if ($this->nameContains) {
86
$needle = phutil_utf8_strtolower($this->nameContains);
87
foreach ($methods as $key => $method) {
88
$haystack = $method->getAPIMethodName();
89
$haystack = phutil_utf8_strtolower($haystack);
90
if (strpos($haystack, $needle) === false) {
91
unset($methods[$key]);
92
}
93
}
94
}
95
96
if ($this->methods) {
97
$map = array_fuse($this->methods);
98
foreach ($methods as $key => $method) {
99
$needle = $method->getAPIMethodName();
100
if (empty($map[$needle])) {
101
unset($methods[$key]);
102
}
103
}
104
}
105
106
if ($this->isInternal !== null) {
107
foreach ($methods as $key => $method) {
108
if ($method->isInternalAPI() !== $this->isInternal) {
109
unset($methods[$key]);
110
}
111
}
112
}
113
114
return $methods;
115
}
116
117
protected function willFilterPage(array $methods) {
118
$application_phids = array();
119
foreach ($methods as $method) {
120
$application = $method->getApplication();
121
if ($application === null) {
122
continue;
123
}
124
$application_phids[] = $application->getPHID();
125
}
126
127
if ($application_phids) {
128
$applications = id(new PhabricatorApplicationQuery())
129
->setParentQuery($this)
130
->setViewer($this->getViewer())
131
->withPHIDs($application_phids)
132
->execute();
133
$applications = mpull($applications, null, 'getPHID');
134
} else {
135
$applications = array();
136
}
137
138
// Remove methods which belong to an application the viewer can not see.
139
foreach ($methods as $key => $method) {
140
$application = $method->getApplication();
141
if ($application === null) {
142
continue;
143
}
144
145
if (empty($applications[$application->getPHID()])) {
146
$this->didRejectResult($method);
147
unset($methods[$key]);
148
}
149
}
150
151
return $methods;
152
}
153
154
public function getQueryApplicationClass() {
155
return 'PhabricatorConduitApplication';
156
}
157
158
}
159
160