2011-01-24 18:00:29 +01:00
|
|
|
<?php
|
|
|
|
|
2012-03-10 00:46:25 +01:00
|
|
|
final class PhabricatorConduitConsoleController
|
2011-01-24 18:00:29 +01:00
|
|
|
extends PhabricatorConduitController {
|
|
|
|
|
|
|
|
private $method;
|
|
|
|
|
2014-04-22 00:32:48 +02:00
|
|
|
public function shouldAllowPublic() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-24 18:00:29 +01:00
|
|
|
public function willProcessRequest(array $data) {
|
2012-04-27 07:25:05 +02:00
|
|
|
$this->method = $data['method'];
|
2011-01-24 18:00:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
2011-01-31 03:52:29 +01:00
|
|
|
|
|
|
|
$request = $this->getRequest();
|
2013-07-01 21:36:34 +02:00
|
|
|
$viewer = $request->getUser();
|
2011-01-31 03:52:29 +01:00
|
|
|
|
2013-07-01 21:36:34 +02:00
|
|
|
$method = id(new PhabricatorConduitMethodQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withMethods(array($this->method))
|
|
|
|
->executeOne();
|
|
|
|
|
|
|
|
if (!$method) {
|
2012-04-27 07:25:05 +02:00
|
|
|
return new Aphront404Response();
|
2011-01-24 18:00:29 +01:00
|
|
|
}
|
|
|
|
|
2014-04-22 00:32:48 +02:00
|
|
|
$can_call_method = false;
|
|
|
|
|
2013-07-01 21:36:34 +02:00
|
|
|
$status = $method->getMethodStatus();
|
|
|
|
$reason = $method->getMethodStatusDescription();
|
2014-04-22 00:32:48 +02:00
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
switch ($status) {
|
|
|
|
case ConduitAPIMethod::METHOD_STATUS_DEPRECATED:
|
|
|
|
$reason = nonempty($reason, pht('This method is deprecated.'));
|
|
|
|
$errors[] = pht('Deprecated Method: %s', $reason);
|
|
|
|
break;
|
|
|
|
case ConduitAPIMethod::METHOD_STATUS_UNSTABLE:
|
|
|
|
$reason = nonempty(
|
|
|
|
$reason,
|
|
|
|
pht(
|
|
|
|
'This method is new and unstable. Its interface is subject '.
|
|
|
|
'to change.'));
|
|
|
|
$errors[] = pht('Unstable Method: %s', $reason);
|
|
|
|
break;
|
2012-04-18 23:25:27 +02:00
|
|
|
}
|
2011-01-24 18:00:29 +01:00
|
|
|
|
2013-07-01 21:36:34 +02:00
|
|
|
$error_types = $method->defineErrorTypes();
|
2015-01-06 23:41:28 +01:00
|
|
|
$error_types['ERR-CONDUIT-CORE'] = pht('See error message for details.');
|
|
|
|
$error_description = array();
|
|
|
|
foreach ($error_types as $error => $meaning) {
|
|
|
|
$error_description[] = hsprintf(
|
|
|
|
'<li><strong>%s:</strong> %s</li>',
|
|
|
|
$error,
|
|
|
|
$meaning);
|
2011-01-24 18:00:29 +01:00
|
|
|
}
|
2015-01-06 23:41:28 +01:00
|
|
|
$error_description = phutil_tag('ul', array(), $error_description);
|
2011-01-24 18:00:29 +01:00
|
|
|
|
|
|
|
$form = new AphrontFormView();
|
|
|
|
$form
|
2011-01-31 03:52:29 +01:00
|
|
|
->setUser($request->getUser())
|
2011-01-24 18:00:29 +01:00
|
|
|
->setAction('/api/'.$this->method)
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormStaticControl())
|
|
|
|
->setLabel('Description')
|
2013-07-01 21:36:34 +02:00
|
|
|
->setValue($method->getMethodDescription()))
|
2011-01-24 18:00:29 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormStaticControl())
|
|
|
|
->setLabel('Returns')
|
2013-07-01 21:36:34 +02:00
|
|
|
->setValue($method->defineReturnType()))
|
2011-01-24 18:00:29 +01:00
|
|
|
->appendChild(
|
2011-01-24 20:36:53 +01:00
|
|
|
id(new AphrontFormMarkupControl())
|
2011-01-24 18:00:29 +01:00
|
|
|
->setLabel('Errors')
|
|
|
|
->setValue($error_description))
|
2013-02-07 23:39:04 +01:00
|
|
|
->appendChild(hsprintf(
|
2011-01-24 18:00:29 +01:00
|
|
|
'<p class="aphront-form-instructions">Enter parameters using '.
|
|
|
|
'<strong>JSON</strong>. For instance, to enter a list, type: '.
|
2013-02-07 23:39:04 +01:00
|
|
|
'<tt>["apple", "banana", "cherry"]</tt>'));
|
2011-01-24 18:00:29 +01:00
|
|
|
|
2013-07-01 21:36:34 +02:00
|
|
|
$params = $method->defineParamTypes();
|
2011-01-24 18:00:29 +01:00
|
|
|
foreach ($params as $param => $desc) {
|
|
|
|
$form->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setLabel($param)
|
|
|
|
->setName("params[{$param}]")
|
2013-02-05 22:23:05 +01:00
|
|
|
->setCaption($desc));
|
2011-01-24 18:00:29 +01:00
|
|
|
}
|
|
|
|
|
2014-04-22 00:32:48 +02:00
|
|
|
$must_login = !$viewer->isLoggedIn() &&
|
|
|
|
$method->shouldRequireAuthentication();
|
|
|
|
if ($must_login) {
|
|
|
|
$errors[] = pht(
|
|
|
|
'Login Required: This method requires authentication. You must '.
|
|
|
|
'log in before you can make calls to it.');
|
|
|
|
} else {
|
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSelectControl())
|
|
|
|
->setLabel('Output Format')
|
|
|
|
->setName('output')
|
|
|
|
->setOptions(
|
|
|
|
array(
|
|
|
|
'human' => 'Human Readable',
|
|
|
|
'json' => 'JSON',
|
|
|
|
)))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
|
|
|
->addCancelButton($this->getApplicationURI())
|
|
|
|
->setValue(pht('Call Method')));
|
|
|
|
}
|
|
|
|
|
|
|
|
$header = id(new PHUIHeaderView())
|
|
|
|
->setUser($viewer)
|
|
|
|
->setHeader($method->getAPIMethodName());
|
2011-01-24 18:00:29 +01:00
|
|
|
|
2013-09-25 20:23:29 +02:00
|
|
|
$form_box = id(new PHUIObjectBoxView())
|
2014-04-22 00:32:48 +02:00
|
|
|
->setHeader($header)
|
|
|
|
->setFormErrors($errors)
|
2013-08-27 00:45:58 +02:00
|
|
|
->setForm($form);
|
|
|
|
|
2013-07-01 21:36:34 +02:00
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
2013-12-19 02:47:34 +01:00
|
|
|
$crumbs->addTextCrumb($method->getAPIMethodName());
|
2011-01-24 18:00:29 +01:00
|
|
|
|
2013-07-01 21:36:34 +02:00
|
|
|
return $this->buildApplicationPage(
|
2012-04-18 23:25:27 +02:00
|
|
|
array(
|
2013-07-01 21:36:34 +02:00
|
|
|
$crumbs,
|
2013-08-27 00:45:58 +02:00
|
|
|
$form_box,
|
2012-04-18 23:25:27 +02:00
|
|
|
),
|
2011-01-24 18:00:29 +01:00
|
|
|
array(
|
2013-07-01 21:36:34 +02:00
|
|
|
'title' => $method->getAPIMethodName(),
|
2011-01-24 18:00:29 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|