Path: blob/master/src/applications/conduit/controller/PhabricatorConduitController.php
12256 views
<?php12abstract class PhabricatorConduitController extends PhabricatorController {34protected function buildSideNavView() {5$viewer = $this->getRequest()->getUser();67$nav = new AphrontSideNavFilterView();8$nav->setBaseURI(new PhutilURI($this->getApplicationURI()));910id(new PhabricatorConduitSearchEngine())11->setViewer($viewer)12->addNavigationItems($nav->getMenu());1314$nav->addLabel('Logs');15$nav->addFilter('log', pht('Call Logs'));1617$nav->selectFilter(null);1819return $nav;20}2122public function buildApplicationMenu() {23return $this->buildSideNavView()->getMenu();24}2526protected function renderExampleBox(ConduitAPIMethod $method, $params) {27$viewer = $this->getViewer();2829$arc_example = id(new PHUIPropertyListView())30->addRawContent($this->renderExample($method, 'arc', $params));3132$curl_example = id(new PHUIPropertyListView())33->addRawContent($this->renderExample($method, 'curl', $params));3435$php_example = id(new PHUIPropertyListView())36->addRawContent($this->renderExample($method, 'php', $params));3738$panel_uri = id(new PhabricatorConduitTokensSettingsPanel())39->setViewer($viewer)40->setUser($viewer)41->getPanelURI();4243$panel_link = phutil_tag(44'a',45array(46'href' => $panel_uri,47),48pht('Conduit API Tokens'));4950$panel_link = phutil_tag('strong', array(), $panel_link);5152$messages = array(53pht(54'Use the %s panel in Settings to generate or manage API tokens.',55$panel_link),56);5758if ($params === null) {59$messages[] = pht(60'If you submit parameters, these examples will update to show '.61'exactly how to encode the parameters you submit.');62}6364$info_view = id(new PHUIInfoView())65->setErrors($messages)66->setSeverity(PHUIInfoView::SEVERITY_NOTICE);6768$tab_group = id(new PHUITabGroupView())69->addTab(70id(new PHUITabView())71->setName(pht('arc call-conduit'))72->setKey('arc')73->appendChild($arc_example))74->addTab(75id(new PHUITabView())76->setName(pht('cURL'))77->setKey('curl')78->appendChild($curl_example))79->addTab(80id(new PHUITabView())81->setName(pht('PHP'))82->setKey('php')83->appendChild($php_example));8485return id(new PHUIObjectBoxView())86->setHeaderText(pht('Examples'))87->setInfoView($info_view)88->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)89->addTabGroup($tab_group);90}9192private function renderExample(93ConduitAPIMethod $method,94$kind,95$params) {9697switch ($kind) {98case 'arc':99$example = $this->buildArcanistExample($method, $params);100break;101case 'php':102$example = $this->buildPHPExample($method, $params);103break;104case 'curl':105$example = $this->buildCURLExample($method, $params);106break;107default:108throw new Exception(pht('Conduit client "%s" is not known.', $kind));109}110111return $example;112}113114private function buildArcanistExample(115ConduitAPIMethod $method,116$params) {117118$parts = array();119120$parts[] = '$ echo ';121if ($params === null) {122$parts[] = phutil_tag('strong', array(), '<json-parameters>');123} else {124$params = $this->simplifyParams($params);125$params = id(new PhutilJSON())->encodeFormatted($params);126$params = trim($params);127$params = csprintf('%s', $params);128$parts[] = phutil_tag('strong', array('class' => 'real'), $params);129}130131$parts[] = ' | ';132$parts[] = 'arc call-conduit ';133134$parts[] = '--conduit-uri ';135$parts[] = phutil_tag(136'strong',137array('class' => 'real'),138PhabricatorEnv::getURI('/'));139$parts[] = ' ';140141$parts[] = '--conduit-token ';142$parts[] = phutil_tag('strong', array(), '<conduit-token>');143$parts[] = ' ';144$parts[] = '--';145$parts[] = ' ';146147$parts[] = $method->getAPIMethodName();148149return $this->renderExampleCode($parts);150}151152private function buildPHPExample(153ConduitAPIMethod $method,154$params) {155156$parts = array();157158$libphutil_path = 'path/to/arcanist/support/init/init-script.php';159160$parts[] = '<?php';161$parts[] = "\n\n";162163$parts[] = 'require_once ';164$parts[] = phutil_var_export($libphutil_path);165$parts[] = ";\n\n";166167$parts[] = '$api_token = "';168$parts[] = phutil_tag('strong', array(), pht('<api-token>'));169$parts[] = "\";\n";170171$parts[] = '$api_parameters = ';172if ($params === null) {173$parts[] = 'array(';174$parts[] = phutil_tag('strong', array(), pht('<parameters>'));175$parts[] = ');';176} else {177$params = $this->simplifyParams($params);178$params = phutil_var_export($params);179$parts[] = phutil_tag('strong', array('class' => 'real'), $params);180$parts[] = ';';181}182$parts[] = "\n\n";183184$parts[] = '$client = new ConduitClient(';185$parts[] = phutil_tag(186'strong',187array('class' => 'real'),188phutil_var_export(PhabricatorEnv::getURI('/')));189$parts[] = ");\n";190191$parts[] = '$client->setConduitToken($api_token);';192$parts[] = "\n\n";193194$parts[] = '$result = $client->callMethodSynchronous(';195$parts[] = phutil_tag(196'strong',197array('class' => 'real'),198phutil_var_export($method->getAPIMethodName()));199$parts[] = ', ';200$parts[] = '$api_parameters';201$parts[] = ");\n";202203$parts[] = 'print_r($result);';204205return $this->renderExampleCode($parts);206}207208private function buildCURLExample(209ConduitAPIMethod $method,210$params) {211212$call_uri = '/api/'.$method->getAPIMethodName();213214$parts = array();215216$linebreak = array('\\', phutil_tag('br'), ' ');217218$parts[] = '$ curl ';219$parts[] = phutil_tag(220'strong',221array('class' => 'real'),222csprintf('%R', PhabricatorEnv::getURI($call_uri)));223$parts[] = ' ';224$parts[] = $linebreak;225226$parts[] = '-d api.token=';227$parts[] = phutil_tag('strong', array(), 'api-token');228$parts[] = ' ';229$parts[] = $linebreak;230231if ($params === null) {232$parts[] = '-d ';233$parts[] = phutil_tag('strong', array(), 'param');234$parts[] = '=';235$parts[] = phutil_tag('strong', array(), 'value');236$parts[] = ' ';237$parts[] = $linebreak;238$parts[] = phutil_tag('strong', array(), '...');239} else {240$lines = array();241$params = $this->simplifyParams($params);242243foreach ($params as $key => $value) {244$pieces = $this->getQueryStringParts(null, $key, $value);245foreach ($pieces as $piece) {246$lines[] = array(247'-d ',248phutil_tag('strong', array('class' => 'real'), $piece),249);250}251}252253$parts[] = phutil_implode_html(array(' ', $linebreak), $lines);254}255256return $this->renderExampleCode($parts);257}258259private function renderExampleCode($example) {260require_celerity_resource('conduit-api-css');261262return phutil_tag(263'div',264array(265'class' => 'PhabricatorMonospaced conduit-api-example-code',266),267$example);268}269270private function simplifyParams(array $params) {271foreach ($params as $key => $value) {272if ($value === null) {273unset($params[$key]);274}275}276return $params;277}278279private function getQueryStringParts($prefix, $key, $value) {280if ($prefix === null) {281$head = phutil_escape_uri($key);282} else {283$head = $prefix.'['.phutil_escape_uri($key).']';284}285286if (!is_array($value)) {287return array(288$head.'='.phutil_escape_uri($value),289);290}291292$results = array();293foreach ($value as $subkey => $subvalue) {294$subparts = $this->getQueryStringParts($head, $subkey, $subvalue);295foreach ($subparts as $subpart) {296$results[] = $subpart;297}298}299300return $results;301}302303}304305306