Differential D12770 Diff 30708 src/applications/conduit/controller/PhabricatorConduitConsoleController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/conduit/controller/PhabricatorConduitConsoleController.php
| <?php | <?php | ||||
| final class PhabricatorConduitConsoleController | final class PhabricatorConduitConsoleController | ||||
| extends PhabricatorConduitController { | extends PhabricatorConduitController { | ||||
| private $method; | |||||
| public function shouldAllowPublic() { | public function shouldAllowPublic() { | ||||
| return true; | return true; | ||||
| } | } | ||||
| public function willProcessRequest(array $data) { | public function handleRequest(AphrontRequest $request) { | ||||
| $this->method = $data['method']; | $viewer = $this->getViewer(); | ||||
| } | $method_name = $request->getURIData('method'); | ||||
| public function processRequest() { | |||||
| $request = $this->getRequest(); | |||||
| $viewer = $request->getUser(); | |||||
| $method = id(new PhabricatorConduitMethodQuery()) | $method = id(new PhabricatorConduitMethodQuery()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->withMethods(array($this->method)) | ->withMethods(array($method_name)) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| if (!$method) { | if (!$method) { | ||||
| return new Aphront404Response(); | return new Aphront404Response(); | ||||
| } | } | ||||
| $can_call_method = false; | $call_uri = '/api/'.$method->getAPIMethodName(); | ||||
| $status = $method->getMethodStatus(); | $status = $method->getMethodStatus(); | ||||
| $reason = $method->getMethodStatusDescription(); | $reason = $method->getMethodStatusDescription(); | ||||
| $errors = array(); | $errors = array(); | ||||
| switch ($status) { | switch ($status) { | ||||
| case ConduitAPIMethod::METHOD_STATUS_DEPRECATED: | case ConduitAPIMethod::METHOD_STATUS_DEPRECATED: | ||||
| $reason = nonempty($reason, pht('This method is deprecated.')); | $reason = nonempty($reason, pht('This method is deprecated.')); | ||||
| $errors[] = pht('Deprecated Method: %s', $reason); | $errors[] = pht('Deprecated Method: %s', $reason); | ||||
| break; | break; | ||||
| case ConduitAPIMethod::METHOD_STATUS_UNSTABLE: | case ConduitAPIMethod::METHOD_STATUS_UNSTABLE: | ||||
| $reason = nonempty( | $reason = nonempty( | ||||
| $reason, | $reason, | ||||
| pht( | pht( | ||||
| 'This method is new and unstable. Its interface is subject '. | 'This method is new and unstable. Its interface is subject '. | ||||
| 'to change.')); | 'to change.')); | ||||
| $errors[] = pht('Unstable Method: %s', $reason); | $errors[] = pht('Unstable Method: %s', $reason); | ||||
| break; | break; | ||||
| } | } | ||||
| $error_types = $method->getErrorTypes(); | $form = id(new AphrontFormView()) | ||||
| $error_types['ERR-CONDUIT-CORE'] = pht('See error message for details.'); | ->setAction($call_uri) | ||||
| $error_description = array(); | |||||
| foreach ($error_types as $error => $meaning) { | |||||
| $error_description[] = hsprintf( | |||||
| '<li><strong>%s:</strong> %s</li>', | |||||
| $error, | |||||
| $meaning); | |||||
| } | |||||
| $error_description = phutil_tag('ul', array(), $error_description); | |||||
| $form = new AphrontFormView(); | |||||
| $form | |||||
| ->setUser($request->getUser()) | ->setUser($request->getUser()) | ||||
| ->setAction('/api/'.$this->method) | ->appendRemarkupInstructions( | ||||
| ->appendChild( | pht( | ||||
| id(new AphrontFormStaticControl()) | 'Enter parameters using **JSON**. For instance, to enter a '. | ||||
| ->setLabel('Description') | 'list, type: `["apple", "banana", "cherry"]`')); | ||||
| ->setValue($method->getMethodDescription())) | |||||
| ->appendChild( | |||||
| id(new AphrontFormStaticControl()) | |||||
| ->setLabel('Returns') | |||||
| ->setValue($method->getReturnType())) | |||||
| ->appendChild( | |||||
| id(new AphrontFormMarkupControl()) | |||||
| ->setLabel('Errors') | |||||
| ->setValue($error_description)) | |||||
| ->appendChild(hsprintf( | |||||
| '<p class="aphront-form-instructions">Enter parameters using '. | |||||
| '<strong>JSON</strong>. For instance, to enter a list, type: '. | |||||
| '<tt>["apple", "banana", "cherry"]</tt>')); | |||||
| $params = $method->getParamTypes(); | $params = $method->getParamTypes(); | ||||
| foreach ($params as $param => $desc) { | foreach ($params as $param => $desc) { | ||||
| $form->appendChild( | $form->appendChild( | ||||
| id(new AphrontFormTextControl()) | id(new AphrontFormTextControl()) | ||||
| ->setLabel($param) | ->setLabel($param) | ||||
| ->setName("params[{$param}]") | ->setName("params[{$param}]") | ||||
| ->setCaption($desc)); | ->setCaption($desc)); | ||||
| Show All 22 Lines | if ($must_login) { | ||||
| ->setValue(pht('Call Method'))); | ->setValue(pht('Call Method'))); | ||||
| } | } | ||||
| $header = id(new PHUIHeaderView()) | $header = id(new PHUIHeaderView()) | ||||
| ->setUser($viewer) | ->setUser($viewer) | ||||
| ->setHeader($method->getAPIMethodName()); | ->setHeader($method->getAPIMethodName()); | ||||
| $form_box = id(new PHUIObjectBoxView()) | $form_box = id(new PHUIObjectBoxView()) | ||||
| ->setHeader($header) | ->setHeaderText(pht('Call Method')) | ||||
| ->setFormErrors($errors) | |||||
| ->appendChild($form); | ->appendChild($form); | ||||
| $content = array(); | $content = array(); | ||||
| $properties = $this->buildMethodProperties($method); | |||||
| $info_box = id(new PHUIObjectBoxView()) | |||||
| ->setHeaderText(pht('API Method: %s', $method->getAPIMethodName())) | |||||
| ->setFormErrors($errors) | |||||
| ->appendChild($properties); | |||||
| $content[] = $info_box; | |||||
| $content[] = $form_box; | |||||
| $content[] = $this->renderExampleBox($method, null); | |||||
| $query = $method->newQueryObject(); | $query = $method->newQueryObject(); | ||||
| if ($query) { | if ($query) { | ||||
| $orders = $query->getBuiltinOrders(); | $orders = $query->getBuiltinOrders(); | ||||
| $rows = array(); | $rows = array(); | ||||
| foreach ($orders as $key => $order) { | foreach ($orders as $key => $order) { | ||||
| $rows[] = array( | $rows[] = array( | ||||
| $key, | $key, | ||||
| ▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | public function handleRequest(AphrontRequest $request) { | ||||
| } | } | ||||
| $crumbs = $this->buildApplicationCrumbs(); | $crumbs = $this->buildApplicationCrumbs(); | ||||
| $crumbs->addTextCrumb($method->getAPIMethodName()); | $crumbs->addTextCrumb($method->getAPIMethodName()); | ||||
| return $this->buildApplicationPage( | return $this->buildApplicationPage( | ||||
| array( | array( | ||||
| $crumbs, | $crumbs, | ||||
| $form_box, | |||||
| $content, | $content, | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'title' => $method->getAPIMethodName(), | 'title' => $method->getAPIMethodName(), | ||||
| )); | )); | ||||
| } | } | ||||
| private function buildMethodProperties(ConduitAPIMethod $method) { | |||||
| $viewer = $this->getViewer(); | |||||
| $view = id(new PHUIPropertyListView()); | |||||
| $view->addProperty( | |||||
| pht('Returns'), | |||||
| $method->getReturnType()); | |||||
| $error_types = $method->getErrorTypes(); | |||||
| $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); | |||||
| } | |||||
| $error_description = phutil_tag('ul', array(), $error_description); | |||||
| $view->addProperty( | |||||
| pht('Errors'), | |||||
| $error_description); | |||||
| $description = $method->getMethodDescription(); | |||||
| $description = PhabricatorMarkupEngine::renderOneObject( | |||||
| id(new PhabricatorMarkupOneOff())->setContent($description), | |||||
| 'default', | |||||
| $viewer); | |||||
| $view->addSectionHeader(pht('Description')); | |||||
| $view->addTextContent($description); | |||||
| return $view; | |||||
| } | |||||
| } | } | ||||