Path: blob/master/src/applications/conduit/ssh/ConduitSSHWorkflow.php
12256 views
<?php12final class ConduitSSHWorkflow extends PhabricatorSSHWorkflow {34protected function didConstruct() {5$this->setName('conduit');6$this->setArguments(7array(8array(9'name' => 'method',10'wildcard' => true,11),12));13}1415public function execute(PhutilArgumentParser $args) {16$time_start = microtime(true);1718$methodv = $args->getArg('method');19if (!$methodv) {20throw new Exception(pht('No Conduit method provided.'));21} else if (count($methodv) > 1) {22throw new Exception(pht('Too many Conduit methods provided.'));23}2425$method = head($methodv);2627$json = $this->readAllInput();28$raw_params = null;29try {30$raw_params = phutil_json_decode($json);31} catch (PhutilJSONParserException $ex) {32throw new PhutilProxyException(33pht('Invalid JSON input.'),34$ex);35}3637$params = idx($raw_params, 'params', '[]');38$params = phutil_json_decode($params);39$metadata = idx($params, '__conduit__', array());40unset($params['__conduit__']);4142$call = null;43$error_code = null;44$error_info = null;4546try {47$call = new ConduitCall($method, $params);48$call->setUser($this->getSSHUser());4950$result = $call->execute();51} catch (ConduitException $ex) {52$result = null;53$error_code = $ex->getMessage();54if ($ex->getErrorDescription()) {55$error_info = $ex->getErrorDescription();56} else if ($call) {57$error_info = $call->getErrorDescription($error_code);58}59}6061$response = id(new ConduitAPIResponse())62->setResult($result)63->setErrorCode($error_code)64->setErrorInfo($error_info);6566$json_out = json_encode($response->toDictionary());67$json_out = $json_out."\n";6869$this->getIOChannel()->write($json_out);7071// NOTE: Flush here so we can get an accurate result for the duration,72// if the response is large and the receiver is slow to read it.73$this->getIOChannel()->flush();7475$connection_id = idx($metadata, 'connectionID');76$log = id(new PhabricatorConduitMethodCallLog())77->setCallerPHID($this->getSSHUser()->getPHID())78->setConnectionID($connection_id)79->setMethod($method)80->setError((string)$error_code)81->setDuration(phutil_microseconds_since($time_start))82->save();83}84}858687