Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function getConnection(ICommand $command)
 {
     $cmdHash = $command->getHash($this->_distributor);
     if (isset($cmdHash)) {
         return $this->_distributor->get($cmdHash);
     }
     throw new ClientException(sprintf("Cannot send '%s' commands to a cluster of connections", $command->getId()));
 }
 private function storeDebug(ICommand $command, $direction)
 {
     $firtsArg = $command->getArgument(0);
     $timestamp = round(microtime(true) - $this->tstart, 4);
     $debug = $command->getId();
     $debug .= isset($firtsArg) ? " {$firtsArg} " : ' ';
     $debug .= "{$direction} {$this}";
     $debug .= " [{$timestamp}s]";
     $this->debugBuffer[] = $debug;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function serialize(ICommand $command)
 {
     $commandId = $command->getId();
     $arguments = $command->getArguments();
     $cmdlen = strlen($commandId);
     $reqlen = count($arguments) + 1;
     $buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandId}\r\n";
     for ($i = 0; $i < $reqlen - 1; $i++) {
         $argument = $arguments[$i];
         $arglen = strlen($argument);
         $buffer .= "\${$arglen}\r\n{$argument}\r\n";
     }
     return $buffer;
 }
Ejemplo n.º 4
0
 /**
  * Checks if the specified command is supported by this connection class.
  *
  * @param ICommand $command The instance of a Redis command.
  * @return string
  */
 protected function getCommandId(ICommand $command)
 {
     switch ($commandId = $command->getId()) {
         case 'AUTH':
         case 'SELECT':
         case 'MULTI':
         case 'EXEC':
         case 'WATCH':
         case 'UNWATCH':
         case 'DISCARD':
             throw new \InvalidArgumentException("Disabled command: {$command->getId()}");
         default:
             return $commandId;
     }
 }
Ejemplo n.º 5
0
 /**
  * Checks if a SORT command is a readable operation by parsing the arguments
  * array of the specified commad instance.
  *
  * @param ICommand $command Instance of Redis command.
  * @return Boolean
  */
 private function isSortReadOnly(ICommand $command)
 {
     $arguments = $command->getArguments();
     return ($c = count($arguments)) === 1 ? true : $arguments[$c - 2] !== 'STORE';
 }
Ejemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function writeCommand(ICommand $command)
 {
     $cmdargs = $command->getArguments();
     array_unshift($cmdargs, $command->getId());
     $this->write(phpiredis_format_command($cmdargs));
 }
Ejemplo n.º 7
0
    protected function getTestCaseBuffer(ICommand $instance)
    {
        $id = $instance->getId();
        $fqn = get_class($instance);
        $class = array_pop(explode('\\', $fqn)) . "Test";
        $realm = $this->getTestRealm();
        $buffer = <<<PHP
<?php

/*
 * This file is part of the Predis package.
 *
 * (c) Daniele Alessandri <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Predis\\Commands;

use \\PHPUnit_Framework_TestCase as StandardTestCase;

/**
 * @group commands
 * @group realm-{$realm}
 */
class {$class} extends CommandTestCase
{
    /**
     * {@inheritdoc}
     */
    protected function getExpectedCommand()
    {
        return '{$fqn}';
    }

    /**
     * {@inheritdoc}
     */
    protected function getExpectedId()
    {
        return '{$id}';
    }

    /**
     * @group disconnected
     */
    public function testFilterArguments()
    {
        \$this->markTestIncomplete('This test has not been implemented yet.');

        \$arguments = array(/* add arguments */);
        \$expected = array(/* add arguments */);

        \$command = \$this->getCommand();
        \$command->setArguments(\$arguments);

        \$this->assertSame(\$expected, \$command->getArguments());
    }

    /**
     * @group disconnected
     */
    public function testParseResponse()
    {
        \$this->markTestIncomplete('This test has not been implemented yet.');

        \$raw = null;
        \$expected = null;

        \$command = \$this->getCommand();

        \$this->assertSame(\$expected, \$command->parseResponse(\$raw));
    }

PHP;
        if ($instance instanceof IPrefixable) {
            $buffer .= <<<PHP

    /**
     * @group disconnected
     */
    public function testPrefixKeys()
    {
        \$this->markTestIncomplete('This test has not been implemented yet.');

        \$arguments = array(/* add arguments */);
        \$expected = array(/* add arguments */);

        \$command = \$this->getCommandWithArgumentsArray(\$arguments);
        \$command->prefixKeys('prefix:');

        \$this->assertSame(\$expected, \$command->getArguments());
    }

PHP;
        }
        return "{$buffer}}\n";
    }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function writeCommand(ICommand $command)
 {
     $commandId = $command->getId();
     $arguments = $command->getArguments();
     $cmdlen = strlen($commandId);
     $reqlen = count($arguments) + 1;
     $buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandId}\r\n";
     for ($i = 0; $i < $reqlen - 1; $i++) {
         $argument = $arguments[$i];
         $arglen = strlen($argument);
         $buffer .= "\${$arglen}\r\n{$argument}\r\n";
     }
     $this->writeBytes($buffer);
 }
Ejemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 public function readResponse(ICommand $command)
 {
     $reply = $this->read();
     if ($reply instanceof IReplyObject) {
         return $reply;
     }
     return $command->parseResponse($reply);
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function process(ICommand $command)
 {
     $command->prefixKeys($this->_prefix);
 }
Ejemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function process(ICommand $command)
 {
     if ($command instanceof IPrefixable) {
         $command->prefixKeys($this->prefix);
     }
 }
Ejemplo n.º 12
0
 /**
  * Returns if the specified command performs a read-only operation
  * against a key stored on Redis.
  *
  * @param ICommand $command Instance of Redis command.
  * @return Boolean
  */
 protected function isReadOperation(ICommand $command)
 {
     if (isset($this->disallowed[$id = $command->getId()])) {
         throw new NotSupportedException("The command {$id} is not allowed in replication mode");
     }
     if (isset($this->readonly[$id])) {
         if (true === ($readonly = $this->readonly[$id])) {
             return true;
         }
         return $readonly($command);
     }
     if (($eval = $id === 'EVAL') || $id === 'EVALSHA') {
         $sha1 = $eval ? sha1($command->getArgument(0)) : $command->getArgument(0);
         if (isset($this->readonlySHA1[$sha1])) {
             if (true === ($readonly = $this->readonlySHA1[$sha1])) {
                 return true;
             }
             return $readonly($command);
         }
     }
     return false;
 }
Ejemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function getConnection(ICommand $command)
 {
     $cmdHash = $command->getHash($this->distributor);
     if (isset($cmdHash)) {
         return $this->distributor->get($cmdHash);
     }
     $message = sprintf("Cannot send '%s' commands to a cluster of connections", $command->getId());
     throw new NotSupportedException($message);
 }