Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/conduit/controller/PhabricatorConduitConsoleController.php
12256 views
1
<?php
2
3
final class PhabricatorConduitConsoleController
4
extends PhabricatorConduitController {
5
6
public function shouldAllowPublic() {
7
return true;
8
}
9
10
public function handleRequest(AphrontRequest $request) {
11
$viewer = $this->getViewer();
12
$method_name = $request->getURIData('method');
13
14
$method = id(new PhabricatorConduitMethodQuery())
15
->setViewer($viewer)
16
->withMethods(array($method_name))
17
->executeOne();
18
if (!$method) {
19
return new Aphront404Response();
20
}
21
22
$method->setViewer($viewer);
23
24
$call_uri = '/api/'.$method->getAPIMethodName();
25
26
$errors = array();
27
28
$form = id(new AphrontFormView())
29
->setAction($call_uri)
30
->setUser($request->getUser())
31
->appendRemarkupInstructions(
32
pht(
33
'Enter parameters using **JSON**. For instance, to enter a '.
34
'list, type: `%s`',
35
'["apple", "banana", "cherry"]'));
36
37
$params = $method->getParamTypes();
38
foreach ($params as $param => $desc) {
39
$form->appendChild(
40
id(new AphrontFormTextControl())
41
->setLabel($param)
42
->setName("params[{$param}]")
43
->setCaption($desc));
44
}
45
46
$must_login = !$viewer->isLoggedIn() &&
47
$method->shouldRequireAuthentication();
48
if ($must_login) {
49
$errors[] = pht(
50
'Login Required: This method requires authentication. You must '.
51
'log in before you can make calls to it.');
52
} else {
53
$form
54
->appendChild(
55
id(new AphrontFormSelectControl())
56
->setLabel(pht('Output Format'))
57
->setName('output')
58
->setOptions(
59
array(
60
'human' => pht('Human Readable'),
61
'json' => pht('JSON'),
62
)))
63
->appendChild(
64
id(new AphrontFormSubmitControl())
65
->addCancelButton($this->getApplicationURI())
66
->setValue(pht('Call Method')));
67
}
68
69
$header = id(new PHUIHeaderView())
70
->setUser($viewer)
71
->setHeader($method->getAPIMethodName())
72
->setHeaderIcon('fa-tty');
73
74
$form_box = id(new PHUIObjectBoxView())
75
->setHeaderText(pht('Call Method'))
76
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
77
->setForm($form);
78
79
$properties = $this->buildMethodProperties($method);
80
81
$info_box = id(new PHUIObjectBoxView())
82
->setHeaderText(pht('API Method: %s', $method->getAPIMethodName()))
83
->setFormErrors($errors)
84
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
85
->appendChild($properties);
86
87
$crumbs = $this->buildApplicationCrumbs();
88
$crumbs->addTextCrumb($method->getAPIMethodName());
89
$crumbs->setBorder(true);
90
91
$documentation_pages = $method->getDocumentationPages($viewer);
92
93
$documentation_view = $this->newDocumentationView(
94
$method,
95
$documentation_pages);
96
97
$view = id(new PHUITwoColumnView())
98
->setHeader($header)
99
->setFooter(array(
100
101
id(new PhabricatorAnchorView())
102
->setAnchorName('overview'),
103
$info_box,
104
105
id(new PhabricatorAnchorView())
106
->setAnchorName('documentation'),
107
$documentation_view,
108
109
id(new PhabricatorAnchorView())
110
->setAnchorName('call'),
111
$form_box,
112
113
id(new PhabricatorAnchorView())
114
->setAnchorName('examples'),
115
$this->renderExampleBox($method, null),
116
));
117
118
$title = $method->getAPIMethodName();
119
120
$nav = $this->newNavigationView($method, $documentation_pages);
121
122
return $this->newPage()
123
->setTitle($title)
124
->setCrumbs($crumbs)
125
->setNavigation($nav)
126
->appendChild($view);
127
}
128
129
private function newDocumentationView(
130
ConduitAPIMethod $method,
131
array $documentation_pages) {
132
assert_instances_of($documentation_pages, 'ConduitAPIDocumentationPage');
133
134
$viewer = $this->getViewer();
135
136
$description_properties = id(new PHUIPropertyListView());
137
138
$description_properties->addTextContent(
139
new PHUIRemarkupView($viewer, $method->getMethodDescription()));
140
141
$description_box = id(new PHUIObjectBoxView())
142
->setHeaderText(pht('Method Description'))
143
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
144
->appendChild($description_properties);
145
146
$view = array();
147
$view[] = $description_box;
148
149
foreach ($documentation_pages as $page) {
150
$view[] = $page->newView();
151
}
152
153
return $view;
154
}
155
156
private function newNavigationView(
157
ConduitAPIMethod $method,
158
array $documentation_pages) {
159
assert_instances_of($documentation_pages, 'ConduitAPIDocumentationPage');
160
161
$console_uri = urisprintf(
162
'/method/%s/',
163
$method->getAPIMethodName());
164
$console_uri = $this->getApplicationURI($console_uri);
165
$console_uri = new PhutilURI($console_uri);
166
167
$nav = id(new AphrontSideNavFilterView())
168
->setBaseURI($console_uri);
169
170
$nav->selectFilter(null);
171
172
$nav->newLink('overview')
173
->setHref('#overview')
174
->setName(pht('Overview'))
175
->setIcon('fa-list');
176
177
$nav->newLink('documentation')
178
->setHref('#documentation')
179
->setName(pht('Documentation'))
180
->setIcon('fa-book');
181
182
foreach ($documentation_pages as $page) {
183
$nav->newLink($page->getAnchor())
184
->setHref('#'.$page->getAnchor())
185
->setName($page->getName())
186
->setIcon($page->getIconIcon())
187
->setIndented(true);
188
}
189
190
$nav->newLink('call')
191
->setHref('#call')
192
->setName(pht('Call Method'))
193
->setIcon('fa-play');
194
195
$nav->newLink('examples')
196
->setHref('#examples')
197
->setName(pht('Examples'))
198
->setIcon('fa-folder-open-o');
199
200
return $nav;
201
}
202
203
private function buildMethodProperties(ConduitAPIMethod $method) {
204
$viewer = $this->getViewer();
205
206
$view = id(new PHUIPropertyListView());
207
208
$status = $method->getMethodStatus();
209
$reason = $method->getMethodStatusDescription();
210
211
switch ($status) {
212
case ConduitAPIMethod::METHOD_STATUS_UNSTABLE:
213
$stability_icon = 'fa-exclamation-triangle yellow';
214
$stability_label = pht('Unstable Method');
215
$stability_info = nonempty(
216
$reason,
217
pht(
218
'This method is new and unstable. Its interface is subject '.
219
'to change.'));
220
break;
221
case ConduitAPIMethod::METHOD_STATUS_DEPRECATED:
222
$stability_icon = 'fa-exclamation-triangle red';
223
$stability_label = pht('Deprecated Method');
224
$stability_info = nonempty($reason, pht('This method is deprecated.'));
225
break;
226
case ConduitAPIMethod::METHOD_STATUS_FROZEN:
227
$stability_icon = 'fa-archive grey';
228
$stability_label = pht('Frozen Method');
229
$stability_info = nonempty(
230
$reason,
231
pht('This method is frozen and will eventually be deprecated.'));
232
break;
233
default:
234
$stability_label = null;
235
break;
236
}
237
238
if ($stability_label) {
239
$view->addProperty(
240
pht('Stability'),
241
array(
242
id(new PHUIIconView())->setIcon($stability_icon),
243
' ',
244
phutil_tag('strong', array(), $stability_label.':'),
245
' ',
246
$stability_info,
247
));
248
}
249
250
$view->addProperty(
251
pht('Returns'),
252
$method->getReturnType());
253
254
$error_types = $method->getErrorTypes();
255
$error_types['ERR-CONDUIT-CORE'] = pht('See error message for details.');
256
$error_description = array();
257
foreach ($error_types as $error => $meaning) {
258
$error_description[] = hsprintf(
259
'<li><strong>%s:</strong> %s</li>',
260
$error,
261
$meaning);
262
}
263
$error_description = phutil_tag('ul', array(), $error_description);
264
265
$view->addProperty(
266
pht('Errors'),
267
$error_description);
268
269
$scope = $method->getRequiredScope();
270
switch ($scope) {
271
case ConduitAPIMethod::SCOPE_ALWAYS:
272
$oauth_icon = 'fa-globe green';
273
$oauth_description = pht(
274
'OAuth clients may always call this method.');
275
break;
276
case ConduitAPIMethod::SCOPE_NEVER:
277
$oauth_icon = 'fa-ban red';
278
$oauth_description = pht(
279
'OAuth clients may never call this method.');
280
break;
281
default:
282
$oauth_icon = 'fa-unlock-alt blue';
283
$oauth_description = pht(
284
'OAuth clients may call this method after requesting access to '.
285
'the "%s" scope.',
286
$scope);
287
break;
288
}
289
290
$view->addProperty(
291
pht('OAuth Scope'),
292
array(
293
id(new PHUIIconView())->setIcon($oauth_icon),
294
' ',
295
$oauth_description,
296
));
297
298
return $view;
299
}
300
301
302
}
303
304