123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- <?php
- namespace Predis;
- use InvalidArgumentException;
- use UnexpectedValueException;
- use Predis\Command\CommandInterface;
- use Predis\Command\ScriptedCommand;
- use Predis\Configuration\Options;
- use Predis\Configuration\OptionsInterface;
- use Predis\Connection\AggregatedConnectionInterface;
- use Predis\Connection\ConnectionInterface;
- use Predis\Connection\ConnectionParametersInterface;
- use Predis\Monitor;
- use Predis\Pipeline\PipelineContext;
- use Predis\Profile\ServerProfile;
- use Predis\PubSub;
- use Predis\Response;
- use Predis\Transaction;
- class Client implements ClientInterface
- {
- const VERSION = '0.9.0-dev';
- protected $connection;
- protected $options;
- private $profile;
-
- public function __construct($parameters = null, $options = null)
- {
- $this->options = $this->createOptions($options ?: array());
- $this->connection = $this->createConnection($parameters ?: array());
- $this->profile = $this->options->profile;
- }
-
- protected function createOptions($options)
- {
- if (is_array($options)) {
- return new Options($options);
- }
- if ($options instanceof OptionsInterface) {
- return $options;
- }
- throw new InvalidArgumentException("Invalid type for client options");
- }
-
- protected function createConnection($parameters)
- {
- if ($parameters instanceof ConnectionInterface) {
- return $parameters;
- }
- if ($parameters instanceof ConnectionParametersInterface || is_string($parameters)) {
- return $this->options->connections->create($parameters);
- }
- if (is_array($parameters)) {
- if (!isset($parameters[0])) {
- return $this->options->connections->create($parameters);
- }
- $options = $this->options;
- if ($options->defined('aggregate')) {
- $initializer = $this->getConnectionInitializerWrapper($options->aggregate);
- $connection = $initializer($parameters, $options);
- } else {
- if ($options->defined('replication') && $replication = $options->replication) {
- $connection = $replication;
- } else {
- $connection = $options->cluster;
- }
- $options->connections->aggregate($connection, $parameters);
- }
- return $connection;
- }
- if (is_callable($parameters)) {
- $initializer = $this->getConnectionInitializerWrapper($parameters);
- $connection = $initializer($this->options);
- return $connection;
- }
- throw new InvalidArgumentException('Invalid type for connection parameters');
- }
-
- protected function getConnectionInitializerWrapper($callable)
- {
- return function () use ($callable) {
- $connection = call_user_func_array($callable, func_get_args());
- if (!$connection instanceof ConnectionInterface) {
- throw new UnexpectedValueException('The callable connection initializer returned an invalid type');
- }
- return $connection;
- };
- }
-
- public function getProfile()
- {
- return $this->profile;
- }
-
- public function getOptions()
- {
- return $this->options;
- }
-
- public function getClientFor($connectionID)
- {
- if (!$connection = $this->getConnectionById($connectionID)) {
- throw new InvalidArgumentException("Invalid connection ID: $connectionID");
- }
- return new static($connection, $this->options);
- }
-
- public function connect()
- {
- $this->connection->connect();
- }
-
- public function disconnect()
- {
- $this->connection->disconnect();
- }
-
- public function quit()
- {
- $this->disconnect();
- }
-
- public function isConnected()
- {
- return $this->connection->isConnected();
- }
-
- public function getConnection()
- {
- return $this->connection;
- }
-
- public function getConnectionById($connectionID)
- {
- if (!$this->connection instanceof AggregatedConnectionInterface) {
- throw new NotSupportedException(
- 'Retrieving connections by ID is supported only when using aggregated connections'
- );
- }
- return $this->connection->getConnectionById($connectionID);
- }
-
- public function __call($commandID, $arguments)
- {
- $command = $this->createCommand($commandID, $arguments);
- $response = $this->executeCommand($command);
- return $response;
- }
-
- public function createCommand($commandID, $arguments = array())
- {
- return $this->profile->createCommand($commandID, $arguments);
- }
-
- public function executeCommand(CommandInterface $command)
- {
- $response = $this->connection->executeCommand($command);
- if ($response instanceof Response\ObjectInterface) {
- if ($response instanceof Response\ErrorInterface) {
- $response = $this->onResponseError($command, $response);
- }
- return $response;
- }
- return $command->parseResponse($response);
- }
-
- protected function onResponseError(CommandInterface $command, Response\ErrorInterface $response)
- {
- if ($command instanceof ScriptedCommand && $response->getErrorType() === 'NOSCRIPT') {
- $eval = $this->createCommand('eval');
- $eval->setRawArguments($command->getEvalArguments());
- $response = $this->executeCommand($eval);
- if (!$response instanceof Response\ObjectInterface) {
- $response = $command->parseResponse($response);
- }
- return $response;
- }
- if ($this->options->exceptions) {
- throw new Response\ServerException($response->getMessage());
- }
- return $response;
- }
-
- private function sharedContextFactory($initializer, $argv = null)
- {
- switch (count($argv)) {
- case 0:
- return $this->$initializer();
- case 1:
- list($arg0) = $argv;
- return is_array($arg0) ? $this->$initializer($arg0) : $this->$initializer(null, $arg0);
- case 2:
- list($arg0, $arg1) = $argv;
- return $this->$initializer($arg0, $arg1);
- default:
- return $this->$initializer($this, $argv);
- }
- }
-
- public function pipeline()
- {
- return $this->sharedContextFactory('createPipeline', func_get_args());
- }
-
- protected function createPipeline(Array $options = null, $callable = null)
- {
- $executor = isset($options['executor']) ? $options['executor'] : null;
- if (is_callable($executor)) {
- $executor = call_user_func($executor, $this, $options);
- }
- $pipeline = new PipelineContext($this, $executor);
- if (isset($callable)) {
- return $pipeline->execute($callable);
- }
- return $pipeline;
- }
-
- public function transaction()
- {
- return $this->sharedContextFactory('createTransaction', func_get_args());
- }
-
- protected function createTransaction(Array $options = null, $callable = null)
- {
- $transaction = new Transaction\MultiExec($this, $options);
- if (isset($callable)) {
- return $transaction->execute($callable);
- }
- return $transaction;
- }
-
- public function pubSubLoop()
- {
- return $this->sharedContextFactory('createPubSub', func_get_args());
- }
-
- protected function createPubSub(Array $options = null, $callable = null)
- {
- $pubsub = new PubSub\Consumer($this, $options);
- if (!isset($callable)) {
- return $pubsub;
- }
- foreach ($pubsub as $message) {
- if (call_user_func($callable, $pubsub, $message) === false) {
- $pubsub->stop();
- }
- }
- }
-
- public function monitor()
- {
- return new Monitor\Consumer($this);
- }
- }
|