[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/conduit/ssh/ -> ConduitSSHWorkflow.php (source)

   1  <?php
   2  
   3  final class ConduitSSHWorkflow extends PhabricatorSSHWorkflow {
   4  
   5    public function didConstruct() {
   6      $this->setName('conduit');
   7      $this->setArguments(
   8        array(
   9          array(
  10            'name'      => 'method',
  11            'wildcard'  => true,
  12          ),
  13        ));
  14    }
  15  
  16    public function execute(PhutilArgumentParser $args) {
  17      $time_start = microtime(true);
  18  
  19      $methodv = $args->getArg('method');
  20      if (!$methodv) {
  21        throw new Exception('No Conduit method provided.');
  22      } else if (count($methodv) > 1) {
  23        throw new Exception('Too many Conduit methods provided.');
  24      }
  25  
  26      $method = head($methodv);
  27  
  28      $json = $this->readAllInput();
  29      $raw_params = json_decode($json, true);
  30      if (!is_array($raw_params)) {
  31        throw new Exception('Invalid JSON input.');
  32      }
  33  
  34      $params = idx($raw_params, 'params', '[]');
  35      $params = json_decode($params, true);
  36      $metadata = idx($params, '__conduit__', array());
  37      unset($params['__conduit__']);
  38  
  39      $call = null;
  40      $error_code = null;
  41      $error_info = null;
  42  
  43      try {
  44        $call = new ConduitCall($method, $params);
  45        $call->setUser($this->getUser());
  46  
  47        $result = $call->execute();
  48      } catch (ConduitException $ex) {
  49        $result = null;
  50        $error_code = $ex->getMessage();
  51        if ($ex->getErrorDescription()) {
  52          $error_info = $ex->getErrorDescription();
  53        } else if ($call) {
  54          $error_info = $call->getErrorDescription($error_code);
  55        }
  56      }
  57  
  58      $response = id(new ConduitAPIResponse())
  59        ->setResult($result)
  60        ->setErrorCode($error_code)
  61        ->setErrorInfo($error_info);
  62  
  63      $json_out = json_encode($response->toDictionary());
  64      $json_out = $json_out."\n";
  65  
  66      $this->getIOChannel()->write($json_out);
  67  
  68      // NOTE: Flush here so we can get an accurate result for the duration,
  69      // if the response is large and the receiver is slow to read it.
  70      $this->getIOChannel()->flush();
  71  
  72      $time_end = microtime(true);
  73  
  74      $connection_id = idx($metadata, 'connectionID');
  75      $log = id(new PhabricatorConduitMethodCallLog())
  76        ->setCallerPHID($this->getUser()->getPHID())
  77        ->setConnectionID($connection_id)
  78        ->setMethod($method)
  79        ->setError((string)$error_code)
  80        ->setDuration(1000000 * ($time_end - $time_start))
  81        ->save();
  82    }
  83  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1