public function __construct(array $options = array()) { if (!function_exists('socket_create')) { throw new Rediska_Connection_Exception("Can't use socket connection: socket functions not present."); } parent::__construct($options); }
public function valid() { if ($this->_count === null) { $reply = $this->_connection->readLine(); if ($reply === null) { return false; } $type = substr($reply, 0, 1); $count = (int) substr($reply, 1); if ($type == Rediska_Connection_Exec::REPLY_MULTY_BULK) { $this->_count = $count; } else { throw new Rediska_Connection_Exec_Exception($reply); } } return $this->_current < $this->_count; }
/** * Read response from connection * * @param Rediska_Connection $connection * @return mixed */ public static function readResponseFromConnection(Rediska_Connection $connection) { $reply = $connection->readLine(); if ($reply === null) { return $reply; } $type = substr($reply, 0, 1); $data = substr($reply, 1); switch ($type) { case self::REPLY_STATUS: if ($data == 'OK') { return true; } else { return $data; } case self::REPLY_ERROR: $message = substr($data, 4); throw new Rediska_Connection_Exec_Exception($message); case self::REPLY_INTEGER: if (strpos($data, '.') !== false) { $number = (int) $data; } else { $number = (double) $data; } return $number; case self::REPLY_BULK: if ($data == '-1') { return null; } else { $length = (int) $data; return $connection->read($length); } case self::REPLY_MULTY_BULK: $count = (int) $data; $replies = array(); for ($i = 0; $i < $count; $i++) { $replies[] = self::readResponseFromConnection($connection); } return $replies; default: throw new Rediska_Connection_Exec_Exception("Invalid reply type: '{$type}'"); } }
/** * Get channels by connection * * @param $connection * @return array */ public function getChannelsByConnection(Rediska_Connection $connection) { if (!isset($this->_channelsByConnections[$connection->getAlias()])) { throw new Rediska_PubSub_Exception("Channels by this connection not present"); } return $this->_channelsByConnections[$connection->getAlias()]; }
protected function _readResponseFromConnection(Rediska_Connection $connection) { $reply = $connection->readLine(); $type = substr($reply, 0, 1); $data = substr($reply, 1); switch ($type) { case self::REPLY_STATUS: if ($data == 'OK') { return true; } else { return $data; } case self::REPLY_ERROR: $message = substr($data, 4); throw new Rediska_Command_Exception($message); case self::REPLY_INTEGER: if (strpos($data, '.') !== false) { $number = (int) $data; } else { $number = (double) $data; } if ((string) $number != $data) { throw new Rediska_Command_Exception("Can't convert data ':{$data}' to integer"); } return $number; case self::REPLY_BULK: if ($data == '-1') { return null; } else { $length = (int) $data; if ((string) $length != $data) { throw new Rediska_Command_Exception("Can't convert bulk reply header '\${$data}' to integer"); } return $connection->read($length); } case self::REPLY_MULTY_BULK: $count = (int) $data; if ((string) $count != $data) { throw new Rediska_Command_Exception("Can't convert multi-response header '{$data}' to integer"); } $replies = array(); for ($i = 0; $i < $count; $i++) { $replies[] = $this->_readResponseFromConnection($connection); } return $replies; default: throw new Rediska_Command_Exception("Invalid reply type: '{$type}'"); } }