Changeset View
Changeset View
Standalone View
Standalone View
src/conduit/ArcanistConduitEngine.php
| <?php | <?php | ||||
| final class ArcanistConduitEngine | final class ArcanistConduitEngine | ||||
| extends Phobject { | extends Phobject { | ||||
| private $conduitURI; | private $conduitURI; | ||||
| private $conduitToken; | private $conduitToken; | ||||
| private $conduitTimeout; | private $conduitTimeout; | ||||
| private $client; | private $client; | ||||
| private $callKey = 0; | |||||
| private $activeFutures = array(); | |||||
| private $resolvedFutures = array(); | |||||
| public function isCallable() { | public function isCallable() { | ||||
| return ($this->conduitURI !== null); | return ($this->conduitURI !== null); | ||||
| } | } | ||||
| public function setConduitURI($conduit_uri) { | public function setConduitURI($conduit_uri) { | ||||
| $this->conduitURI = $conduit_uri; | $this->conduitURI = $conduit_uri; | ||||
| return $this; | return $this; | ||||
| Show All 21 Lines | public function getConduitTimeout() { | ||||
| return $this->conduitTimeout; | return $this->conduitTimeout; | ||||
| } | } | ||||
| public function newCall($method, array $parameters) { | public function newCall($method, array $parameters) { | ||||
| if ($this->conduitURI == null) { | if ($this->conduitURI == null) { | ||||
| $this->raiseURIException(); | $this->raiseURIException(); | ||||
| } | } | ||||
| $next_key = ++$this->callKey; | |||||
| return id(new ArcanistConduitCall()) | return id(new ArcanistConduitCall()) | ||||
| ->setKey($next_key) | |||||
| ->setEngine($this) | ->setEngine($this) | ||||
| ->setMethod($method) | ->setMethod($method) | ||||
| ->setParameters($parameters); | ->setParameters($parameters); | ||||
| } | } | ||||
| public function resolveCall($method, array $parameters) { | public function resolveCall($method, array $parameters) { | ||||
| return $this->newCall($method, $parameters)->resolve(); | return $this->newCall($method, $parameters)->resolve(); | ||||
| } | } | ||||
| public function newFuture(ArcanistConduitCall $call) { | public function newFuture(ArcanistConduitCall $call) { | ||||
| $method = $call->getMethod(); | $method = $call->getMethod(); | ||||
| $parameters = $call->getParameters(); | $parameters = $call->getParameters(); | ||||
| $future = $this->getClient()->callMethod($method, $parameters); | $future = $this->getClient()->callMethod($method, $parameters); | ||||
| $this->activeFutures[$call->getKey()] = $future; | |||||
| return $future; | return $future; | ||||
| } | } | ||||
| private function getClient() { | private function getClient() { | ||||
| if (!$this->client) { | if (!$this->client) { | ||||
| $conduit_uri = $this->getConduitURI(); | $conduit_uri = $this->getConduitURI(); | ||||
| $client = new ConduitClient($conduit_uri); | $client = new ConduitClient($conduit_uri); | ||||
| $timeout = $this->getConduitTimeout(); | $timeout = $this->getConduitTimeout(); | ||||
| if ($timeout) { | if ($timeout) { | ||||
| $client->setTimeout($timeout); | $client->setTimeout($timeout); | ||||
| } | } | ||||
| $token = $this->getConduitToken(); | $token = $this->getConduitToken(); | ||||
| if ($token) { | if ($token) { | ||||
| $client->setConduitToken($this->getConduitToken()); | $client->setConduitToken($this->getConduitToken()); | ||||
| } | } | ||||
| } | } | ||||
| return $client; | return $client; | ||||
| } | } | ||||
| public function resolveFuture($key) { | |||||
| if (isset($this->resolvedFutures[$key])) { | |||||
| return; | |||||
| } | |||||
| if (!isset($this->activeFutures[$key])) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'No future with key "%s" is present in pool.', | |||||
| $key)); | |||||
| } | |||||
| $iterator = new FutureIterator($this->activeFutures); | |||||
| foreach ($iterator as $future_key => $future) { | |||||
| $this->resolvedFutures[$future_key] = $future; | |||||
| unset($this->activeFutures[$future_key]); | |||||
| if ($future_key == $key) { | |||||
| break; | |||||
| } | |||||
| } | |||||
| return; | |||||
| } | |||||
| private function raiseURIException() { | private function raiseURIException() { | ||||
| $list = id(new PhutilConsoleList()) | $list = id(new PhutilConsoleList()) | ||||
| ->addItem( | ->addItem( | ||||
| pht( | pht( | ||||
| 'Run in a working copy with "phabricator.uri" set in ".arcconfig".')) | 'Run in a working copy with "phabricator.uri" set in ".arcconfig".')) | ||||
| ->addItem( | ->addItem( | ||||
| pht( | pht( | ||||
| 'Set a default URI with `arc set-config phabricator.uri <uri>`.')) | 'Set a default URI with `arc set-config phabricator.uri <uri>`.')) | ||||
| Show All 15 Lines | |||||