mirror of
https://github.com/newnius/YAO-portal.git
synced 2025-12-16 09:46:44 +00:00
init & add agent & add job
This commit is contained in:
24
predis/src/Protocol/ProtocolException.php
Normal file
24
predis/src/Protocol/ProtocolException.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
|
||||
/**
|
||||
* Exception used to indentify errors encountered while parsing the Redis wire
|
||||
* protocol.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ProtocolException extends CommunicationException
|
||||
{
|
||||
}
|
||||
41
predis/src/Protocol/ProtocolProcessorInterface.php
Normal file
41
predis/src/Protocol/ProtocolProcessorInterface.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol;
|
||||
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
|
||||
/**
|
||||
* Defines a pluggable protocol processor capable of serializing commands and
|
||||
* deserializing responses into PHP objects directly from a connection.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
interface ProtocolProcessorInterface
|
||||
{
|
||||
/**
|
||||
* Writes a request over a connection to Redis.
|
||||
*
|
||||
* @param CompositeConnectionInterface $connection Redis connection.
|
||||
* @param CommandInterface $command Command instance.
|
||||
*/
|
||||
public function write(CompositeConnectionInterface $connection, CommandInterface $command);
|
||||
|
||||
/**
|
||||
* Reads a response from a connection to Redis.
|
||||
*
|
||||
* @param CompositeConnectionInterface $connection Redis connection.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function read(CompositeConnectionInterface $connection);
|
||||
}
|
||||
31
predis/src/Protocol/RequestSerializerInterface.php
Normal file
31
predis/src/Protocol/RequestSerializerInterface.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol;
|
||||
|
||||
use Predis\Command\CommandInterface;
|
||||
|
||||
/**
|
||||
* Defines a pluggable serializer for Redis commands.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
interface RequestSerializerInterface
|
||||
{
|
||||
/**
|
||||
* Serializes a Redis command.
|
||||
*
|
||||
* @param CommandInterface $command Redis command.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function serialize(CommandInterface $command);
|
||||
}
|
||||
32
predis/src/Protocol/ResponseReaderInterface.php
Normal file
32
predis/src/Protocol/ResponseReaderInterface.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol;
|
||||
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
|
||||
/**
|
||||
* Defines a pluggable reader capable of parsing responses returned by Redis and
|
||||
* deserializing them to PHP objects.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
interface ResponseReaderInterface
|
||||
{
|
||||
/**
|
||||
* Reads a response from a connection to Redis.
|
||||
*
|
||||
* @param CompositeConnectionInterface $connection Redis connection.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function read(CompositeConnectionInterface $connection);
|
||||
}
|
||||
107
predis/src/Protocol/Text/CompositeProtocolProcessor.php
Normal file
107
predis/src/Protocol/Text/CompositeProtocolProcessor.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol\Text;
|
||||
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
use Predis\Protocol\ProtocolProcessorInterface;
|
||||
use Predis\Protocol\RequestSerializerInterface;
|
||||
use Predis\Protocol\ResponseReaderInterface;
|
||||
|
||||
/**
|
||||
* Composite protocol processor for the standard Redis wire protocol using
|
||||
* pluggable handlers to serialize requests and deserialize responses.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class CompositeProtocolProcessor implements ProtocolProcessorInterface
|
||||
{
|
||||
/*
|
||||
* @var RequestSerializerInterface
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
/*
|
||||
* @var ResponseReaderInterface
|
||||
*/
|
||||
protected $reader;
|
||||
|
||||
/**
|
||||
* @param RequestSerializerInterface $serializer Request serializer.
|
||||
* @param ResponseReaderInterface $reader Response reader.
|
||||
*/
|
||||
public function __construct(
|
||||
RequestSerializerInterface $serializer = null,
|
||||
ResponseReaderInterface $reader = null
|
||||
) {
|
||||
$this->setRequestSerializer($serializer ?: new RequestSerializer());
|
||||
$this->setResponseReader($reader ?: new ResponseReader());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function write(CompositeConnectionInterface $connection, CommandInterface $command)
|
||||
{
|
||||
$connection->writeBuffer($this->serializer->serialize($command));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read(CompositeConnectionInterface $connection)
|
||||
{
|
||||
return $this->reader->read($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the request serializer used by the protocol processor.
|
||||
*
|
||||
* @param RequestSerializerInterface $serializer Request serializer.
|
||||
*/
|
||||
public function setRequestSerializer(RequestSerializerInterface $serializer)
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the request serializer used by the protocol processor.
|
||||
*
|
||||
* @return RequestSerializerInterface
|
||||
*/
|
||||
public function getRequestSerializer()
|
||||
{
|
||||
return $this->serializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the response reader used by the protocol processor.
|
||||
*
|
||||
* @param ResponseReaderInterface $reader Response reader.
|
||||
*/
|
||||
public function setResponseReader(ResponseReaderInterface $reader)
|
||||
{
|
||||
$this->reader = $reader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Response reader used by the protocol processor.
|
||||
*
|
||||
* @return ResponseReaderInterface
|
||||
*/
|
||||
public function getResponseReader()
|
||||
{
|
||||
return $this->reader;
|
||||
}
|
||||
}
|
||||
55
predis/src/Protocol/Text/Handler/BulkResponse.php
Normal file
55
predis/src/Protocol/Text/Handler/BulkResponse.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol\Text\Handler;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
|
||||
/**
|
||||
* Handler for the bulk response type in the standard Redis wire protocol.
|
||||
* It translates the payload to a string or a NULL.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class BulkResponse implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(CompositeConnectionInterface $connection, $payload)
|
||||
{
|
||||
$length = (int) $payload;
|
||||
|
||||
if ("$length" !== $payload) {
|
||||
CommunicationException::handle(new ProtocolException(
|
||||
$connection, "Cannot parse '$payload' as a valid length for a bulk response."
|
||||
));
|
||||
}
|
||||
|
||||
if ($length >= 0) {
|
||||
return substr($connection->readBuffer($length + 2), 0, -2);
|
||||
}
|
||||
|
||||
if ($length == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
CommunicationException::handle(new ProtocolException(
|
||||
$connection, "Value '$payload' is not a valid length for a bulk response."
|
||||
));
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
34
predis/src/Protocol/Text/Handler/ErrorResponse.php
Normal file
34
predis/src/Protocol/Text/Handler/ErrorResponse.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol\Text\Handler;
|
||||
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
use Predis\Response\Error;
|
||||
|
||||
/**
|
||||
* Handler for the error response type in the standard Redis wire protocol.
|
||||
* It translates the payload to a complex response object for Predis.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ErrorResponse implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(CompositeConnectionInterface $connection, $payload)
|
||||
{
|
||||
return new Error($payload);
|
||||
}
|
||||
}
|
||||
46
predis/src/Protocol/Text/Handler/IntegerResponse.php
Normal file
46
predis/src/Protocol/Text/Handler/IntegerResponse.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol\Text\Handler;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
|
||||
/**
|
||||
* Handler for the integer response type in the standard Redis wire protocol.
|
||||
* It translates the payload an integer or NULL.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class IntegerResponse implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(CompositeConnectionInterface $connection, $payload)
|
||||
{
|
||||
if (is_numeric($payload)) {
|
||||
$integer = (int) $payload;
|
||||
return $integer == $payload ? $integer : $payload;
|
||||
}
|
||||
|
||||
if ($payload !== 'nil') {
|
||||
CommunicationException::handle(new ProtocolException(
|
||||
$connection, "Cannot parse '$payload' as a valid numeric response."
|
||||
));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
68
predis/src/Protocol/Text/Handler/MultiBulkResponse.php
Normal file
68
predis/src/Protocol/Text/Handler/MultiBulkResponse.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol\Text\Handler;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
|
||||
/**
|
||||
* Handler for the multibulk response type in the standard Redis wire protocol.
|
||||
* It returns multibulk responses as PHP arrays.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class MultiBulkResponse implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(CompositeConnectionInterface $connection, $payload)
|
||||
{
|
||||
$length = (int) $payload;
|
||||
|
||||
if ("$length" !== $payload) {
|
||||
CommunicationException::handle(new ProtocolException(
|
||||
$connection, "Cannot parse '$payload' as a valid length of a multi-bulk response."
|
||||
));
|
||||
}
|
||||
|
||||
if ($length === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$list = array();
|
||||
|
||||
if ($length > 0) {
|
||||
$handlersCache = array();
|
||||
$reader = $connection->getProtocol()->getResponseReader();
|
||||
|
||||
for ($i = 0; $i < $length; ++$i) {
|
||||
$header = $connection->readLine();
|
||||
$prefix = $header[0];
|
||||
|
||||
if (isset($handlersCache[$prefix])) {
|
||||
$handler = $handlersCache[$prefix];
|
||||
} else {
|
||||
$handler = $reader->getHandler($prefix);
|
||||
$handlersCache[$prefix] = $handler;
|
||||
}
|
||||
|
||||
$list[$i] = $handler->handle($connection, substr($header, 1));
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol\Text\Handler;
|
||||
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
|
||||
/**
|
||||
* Defines a pluggable handler used to parse a particular type of response.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
interface ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Deserializes a response returned by Redis and reads more data from the
|
||||
* connection if needed.
|
||||
*
|
||||
* @param CompositeConnectionInterface $connection Redis connection.
|
||||
* @param string $payload String payload.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(CompositeConnectionInterface $connection, $payload);
|
||||
}
|
||||
35
predis/src/Protocol/Text/Handler/StatusResponse.php
Normal file
35
predis/src/Protocol/Text/Handler/StatusResponse.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol\Text\Handler;
|
||||
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
use Predis\Response\Status;
|
||||
|
||||
/**
|
||||
* Handler for the status response type in the standard Redis wire protocol. It
|
||||
* translates certain classes of status response to PHP objects or just returns
|
||||
* the payload as a string.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class StatusResponse implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(CompositeConnectionInterface $connection, $payload)
|
||||
{
|
||||
return Status::get($payload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol\Text\Handler;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
use Predis\Response\Iterator\MultiBulk as MultiBulkIterator;
|
||||
|
||||
/**
|
||||
* Handler for the multibulk response type in the standard Redis wire protocol.
|
||||
* It returns multibulk responses as iterators that can stream bulk elements.
|
||||
*
|
||||
* Streamable multibulk responses are not globally supported by the abstractions
|
||||
* built-in into Predis, such as transactions or pipelines. Use them with care!
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class StreamableMultiBulkResponse implements ResponseHandlerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(CompositeConnectionInterface $connection, $payload)
|
||||
{
|
||||
$length = (int) $payload;
|
||||
|
||||
if ("$length" != $payload) {
|
||||
CommunicationException::handle(new ProtocolException(
|
||||
$connection, "Cannot parse '$payload' as a valid length for a multi-bulk response."
|
||||
));
|
||||
}
|
||||
|
||||
return new MultiBulkIterator($connection, $length);
|
||||
}
|
||||
}
|
||||
123
predis/src/Protocol/Text/ProtocolProcessor.php
Normal file
123
predis/src/Protocol/Text/ProtocolProcessor.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol\Text;
|
||||
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\CommunicationException;
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
use Predis\Protocol\ProtocolProcessorInterface;
|
||||
use Predis\Response\Error as ErrorResponse;
|
||||
use Predis\Response\Iterator\MultiBulk as MultiBulkIterator;
|
||||
use Predis\Response\Status as StatusResponse;
|
||||
|
||||
/**
|
||||
* Protocol processor for the standard Redis wire protocol.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ProtocolProcessor implements ProtocolProcessorInterface
|
||||
{
|
||||
protected $mbiterable;
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->mbiterable = false;
|
||||
$this->serializer = new RequestSerializer();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function write(CompositeConnectionInterface $connection, CommandInterface $command)
|
||||
{
|
||||
$request = $this->serializer->serialize($command);
|
||||
$connection->writeBuffer($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read(CompositeConnectionInterface $connection)
|
||||
{
|
||||
$chunk = $connection->readLine();
|
||||
$prefix = $chunk[0];
|
||||
$payload = substr($chunk, 1);
|
||||
|
||||
switch ($prefix) {
|
||||
case '+':
|
||||
return new StatusResponse($payload);
|
||||
|
||||
case '$':
|
||||
$size = (int) $payload;
|
||||
if ($size === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
return substr($connection->readBuffer($size + 2), 0, -2);
|
||||
|
||||
case '*':
|
||||
$count = (int) $payload;
|
||||
|
||||
if ($count === -1) {
|
||||
return;
|
||||
}
|
||||
if ($this->mbiterable) {
|
||||
return new MultiBulkIterator($connection, $count);
|
||||
}
|
||||
|
||||
$multibulk = array();
|
||||
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$multibulk[$i] = $this->read($connection);
|
||||
}
|
||||
|
||||
return $multibulk;
|
||||
|
||||
case ':':
|
||||
$integer = (int) $payload;
|
||||
return $integer == $payload ? $integer : $payload;
|
||||
|
||||
case '-':
|
||||
return new ErrorResponse($payload);
|
||||
|
||||
default:
|
||||
CommunicationException::handle(new ProtocolException(
|
||||
$connection, "Unknown response prefix: '$prefix'."
|
||||
));
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables returning multibulk responses as specialized PHP
|
||||
* iterators used to stream bulk elements of a multibulk response instead
|
||||
* returning a plain array.
|
||||
*
|
||||
* Streamable multibulk responses are not globally supported by the
|
||||
* abstractions built-in into Predis, such as transactions or pipelines.
|
||||
* Use them with care!
|
||||
*
|
||||
* @param bool $value Enable or disable streamable multibulk responses.
|
||||
*/
|
||||
public function useIterableMultibulk($value)
|
||||
{
|
||||
$this->mbiterable = (bool) $value;
|
||||
}
|
||||
}
|
||||
46
predis/src/Protocol/Text/RequestSerializer.php
Normal file
46
predis/src/Protocol/Text/RequestSerializer.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol\Text;
|
||||
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Protocol\RequestSerializerInterface;
|
||||
|
||||
/**
|
||||
* Request serializer for the standard Redis wire protocol.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class RequestSerializer implements RequestSerializerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function serialize(CommandInterface $command)
|
||||
{
|
||||
$commandID = $command->getId();
|
||||
$arguments = $command->getArguments();
|
||||
|
||||
$cmdlen = strlen($commandID);
|
||||
$reqlen = count($arguments) + 1;
|
||||
|
||||
$buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandID}\r\n";
|
||||
|
||||
foreach ($arguments as $argument) {
|
||||
$arglen = strlen($argument);
|
||||
$buffer .= "\${$arglen}\r\n{$argument}\r\n";
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
||||
116
predis/src/Protocol/Text/ResponseReader.php
Normal file
116
predis/src/Protocol/Text/ResponseReader.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Protocol\Text;
|
||||
|
||||
use Predis\CommunicationException;
|
||||
use Predis\Connection\CompositeConnectionInterface;
|
||||
use Predis\Protocol\ProtocolException;
|
||||
use Predis\Protocol\ResponseReaderInterface;
|
||||
|
||||
/**
|
||||
* Response reader for the standard Redis wire protocol.
|
||||
*
|
||||
* @link http://redis.io/topics/protocol
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ResponseReader implements ResponseReaderInterface
|
||||
{
|
||||
protected $handlers;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->handlers = $this->getDefaultHandlers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default handlers for the supported type of responses.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getDefaultHandlers()
|
||||
{
|
||||
return array(
|
||||
'+' => new Handler\StatusResponse(),
|
||||
'-' => new Handler\ErrorResponse(),
|
||||
':' => new Handler\IntegerResponse(),
|
||||
'$' => new Handler\BulkResponse(),
|
||||
'*' => new Handler\MultiBulkResponse(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the handler for the specified prefix identifying the response type.
|
||||
*
|
||||
* @param string $prefix Identifier of the type of response.
|
||||
* @param Handler\ResponseHandlerInterface $handler Response handler.
|
||||
*/
|
||||
public function setHandler($prefix, Handler\ResponseHandlerInterface $handler)
|
||||
{
|
||||
$this->handlers[$prefix] = $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the response handler associated to a certain type of response.
|
||||
*
|
||||
* @param string $prefix Identifier of the type of response.
|
||||
*
|
||||
* @return Handler\ResponseHandlerInterface
|
||||
*/
|
||||
public function getHandler($prefix)
|
||||
{
|
||||
if (isset($this->handlers[$prefix])) {
|
||||
return $this->handlers[$prefix];
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read(CompositeConnectionInterface $connection)
|
||||
{
|
||||
$header = $connection->readLine();
|
||||
|
||||
if ($header === '') {
|
||||
$this->onProtocolError($connection, 'Unexpected empty reponse header.');
|
||||
}
|
||||
|
||||
$prefix = $header[0];
|
||||
|
||||
if (!isset($this->handlers[$prefix])) {
|
||||
$this->onProtocolError($connection, "Unknown response prefix: '$prefix'.");
|
||||
}
|
||||
|
||||
$payload = $this->handlers[$prefix]->handle($connection, substr($header, 1));
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles protocol errors generated while reading responses from a
|
||||
* connection.
|
||||
*
|
||||
* @param CompositeConnectionInterface $connection Redis connection that generated the error.
|
||||
* @param string $message Error message.
|
||||
*/
|
||||
protected function onProtocolError(CompositeConnectionInterface $connection, $message)
|
||||
{
|
||||
CommunicationException::handle(
|
||||
new ProtocolException($connection, $message)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user