Differential D10168 Diff 24459 src/infrastructure/daemon/bot/adapter/PhabricatorBotBaseStreamingProtocolAdapter.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/daemon/bot/adapter/PhabricatorBotBaseStreamingProtocolAdapter.php
<?php | <?php | ||||
abstract class PhabricatorBotBaseStreamingProtocolAdapter | abstract class PhabricatorBotBaseStreamingProtocolAdapter | ||||
extends PhabricatorBaseProtocolAdapter { | extends PhabricatorBaseProtocolAdapter { | ||||
protected $readHandles; | |||||
protected $multiHandle; | |||||
protected $authtoken; | |||||
private $readBuffers; | private $readBuffers; | ||||
private $authtoken; | |||||
private $server; | private $server; | ||||
private $readHandles; | |||||
private $multiHandle; | |||||
private $active; | private $active; | ||||
private $inRooms = array(); | private $inRooms = array(); | ||||
public function getServiceName() { | public function getServiceName() { | ||||
$uri = new PhutilURI($this->server); | $uri = new PhutilURI($this->server); | ||||
return $uri->getDomain(); | return $uri->getDomain(); | ||||
} | } | ||||
Show All 14 Lines | public function connect() { | ||||
$this->multiHandle = curl_multi_init(); | $this->multiHandle = curl_multi_init(); | ||||
$this->readHandles = array(); | $this->readHandles = array(); | ||||
foreach ($rooms as $room_id) { | foreach ($rooms as $room_id) { | ||||
$this->joinRoom($room_id); | $this->joinRoom($room_id); | ||||
// Set up the curl stream for reading | // Set up the curl stream for reading | ||||
$url = $this->buildStreamingUrl($room_id); | $url = $this->buildStreamingUrl($room_id); | ||||
$this->readHandle[$url] = curl_init(); | $ch = $this->readHandles[$url] = curl_init(); | ||||
curl_setopt($this->readHandle[$url], CURLOPT_URL, $url); | |||||
curl_setopt($this->readHandle[$url], CURLOPT_RETURNTRANSFER, true); | curl_setopt($ch, CURLOPT_URL, $url); | ||||
curl_setopt($this->readHandle[$url], CURLOPT_FOLLOWLOCATION, 1); | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |||||
mholden: //This introduction of the $ch variable just seemed like a good opportunity for code cleanup.// | |||||
curl_setopt( | curl_setopt( | ||||
$this->readHandle[$url], | $ch, | ||||
CURLOPT_USERPWD, | CURLOPT_USERPWD, | ||||
$this->authtoken.':x'); | $this->authtoken.':x'); | ||||
curl_setopt( | curl_setopt( | ||||
$this->readHandle[$url], | $ch, | ||||
CURLOPT_HTTPHEADER, | CURLOPT_HTTPHEADER, | ||||
array('Content-type: application/json')); | array('Content-type: application/json')); | ||||
curl_setopt( | curl_setopt( | ||||
$this->readHandle[$url], | $ch, | ||||
CURLOPT_WRITEFUNCTION, | CURLOPT_WRITEFUNCTION, | ||||
array($this, 'read')); | array($this, 'read')); | ||||
curl_setopt($this->readHandle[$url], CURLOPT_BUFFERSIZE, 128); | curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); | ||||
curl_setopt($this->readHandle[$url], CURLOPT_TIMEOUT, 0); | curl_setopt($ch, CURLOPT_TIMEOUT, 0); | ||||
curl_multi_add_handle($this->multiHandle, $this->readHandle[$url]); | curl_multi_add_handle($this->multiHandle, $ch); | ||||
// Initialize read buffer | // Initialize read buffer | ||||
$this->readBuffers[$url] = ''; | $this->readBuffers[$url] = ''; | ||||
} | } | ||||
$this->active = null; | $this->active = null; | ||||
$this->blockingMultiExec(); | $this->blockingMultiExec(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | protected function performPost($endpoint, $data = null) { | ||||
if (strlen($output)) { | if (strlen($output)) { | ||||
return json_decode($output, true); | return json_decode($output, true); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
protected function getAuthorizationHeader() { | protected function getAuthorizationHeader() { | ||||
return 'Basic '.base64_encode($this->authtoken.':x'); | return 'Basic '.$this->getEncodedAuthToken(); | ||||
} | |||||
protected function getEncodedAuthToken() { | |||||
return base64_encode($this->authtoken.':x'); | |||||
} | } | ||||
abstract protected function buildStreamingUrl($channel); | abstract protected function buildStreamingUrl($channel); | ||||
abstract protected function processMessage($raw_object); | abstract protected function processMessage($raw_object); | ||||
} | } |
This introduction of the $ch variable just seemed like a good opportunity for code cleanup.