Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / conduit / query / PhabricatorConduitMethodQuery.php
blobeb61d8c48072fc4ff292395eae2985e3041b4a00
1 <?php
3 final class PhabricatorConduitMethodQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $isDeprecated;
7 private $isStable;
8 private $isUnstable;
9 private $applicationNames;
10 private $nameContains;
11 private $methods;
12 private $isInternal;
14 public function withMethods(array $methods) {
15 $this->methods = $methods;
16 return $this;
19 public function withNameContains($name_contains) {
20 $this->nameContains = $name_contains;
21 return $this;
24 public function withIsStable($is_stable) {
25 $this->isStable = $is_stable;
26 return $this;
29 public function withIsUnstable($is_unstable) {
30 $this->isUnstable = $is_unstable;
31 return $this;
34 public function withIsDeprecated($is_deprecated) {
35 $this->isDeprecated = $is_deprecated;
36 return $this;
39 public function withIsInternal($is_internal) {
40 $this->isInternal = $is_internal;
41 return $this;
44 protected function loadPage() {
45 $methods = $this->getAllMethods();
46 $methods = $this->filterMethods($methods);
47 return $methods;
50 private function getAllMethods() {
51 return id(new PhutilClassMapQuery())
52 ->setAncestorClass('ConduitAPIMethod')
53 ->setSortMethod('getSortOrder')
54 ->execute();
57 private function filterMethods(array $methods) {
58 foreach ($methods as $key => $method) {
59 $application = $method->getApplication();
60 if (!$application) {
61 continue;
63 if (!$application->isInstalled()) {
64 unset($methods[$key]);
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,
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]);
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]);
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]);
106 if ($this->isInternal !== null) {
107 foreach ($methods as $key => $method) {
108 if ($method->isInternalAPI() !== $this->isInternal) {
109 unset($methods[$key]);
114 return $methods;
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;
124 $application_phids[] = $application->getPHID();
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();
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;
145 if (empty($applications[$application->getPHID()])) {
146 $this->didRejectResult($method);
147 unset($methods[$key]);
151 return $methods;
154 public function getQueryApplicationClass() {
155 return 'PhabricatorConduitApplication';