mirror of
https://github.com/predis/predis.git
synced 2026-08-25 16:09:37 +00:00
c8e20e428a
It was too broad in concept and not really that useful, so for now we assume that only our base Predis\Profile\RedisProfile class can handle command processing and we will decide later if we want to add a couple of more methods to Predis\Profile\ProfileInterface.
146 lines
3.4 KiB
PHP
146 lines
3.4 KiB
PHP
<?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\Profile;
|
|
|
|
use InvalidArgumentException;
|
|
use ReflectionClass;
|
|
use Predis\ClientException;
|
|
use Predis\Command\Processor\CommandProcessorInterface;
|
|
|
|
/**
|
|
* Base class that implements common functionalities of server profiles.
|
|
*
|
|
* @author Daniele Alessandri <suppakilla@gmail.com>
|
|
*/
|
|
abstract class RedisProfile implements ProfileInterface
|
|
{
|
|
private $commands;
|
|
private $processor;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->commands = $this->getSupportedCommands();
|
|
}
|
|
|
|
/**
|
|
* Returns a map of all the commands supported by the profile and their
|
|
* actual PHP classes.
|
|
*
|
|
* @return array
|
|
*/
|
|
protected abstract function getSupportedCommands();
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function supportsCommand($commandID)
|
|
{
|
|
return isset($this->commands[strtolower($commandID)]);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function supportsCommands(array $commandIDs)
|
|
{
|
|
foreach ($commandIDs as $commandID) {
|
|
if (!$this->supportsCommand($commandID)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Returns the FQN of the class that represent the specified command ID
|
|
* registered in the current server profile.
|
|
*
|
|
* @param string $commandID Command ID.
|
|
* @return string
|
|
*/
|
|
public function getCommandClass($commandID)
|
|
{
|
|
if (isset($this->commands[$commandID = strtolower($commandID)])) {
|
|
return $this->commands[$commandID];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function createCommand($commandID, $arguments = array())
|
|
{
|
|
$commandID = strtolower($commandID);
|
|
|
|
if (!isset($this->commands[$commandID])) {
|
|
throw new ClientException("'$commandID' is not a registered Redis command");
|
|
}
|
|
|
|
$commandClass = $this->commands[$commandID];
|
|
$command = new $commandClass();
|
|
$command->setArguments($arguments);
|
|
|
|
if (isset($this->processor)) {
|
|
$this->processor->process($command);
|
|
}
|
|
|
|
return $command;
|
|
}
|
|
|
|
/**
|
|
* Defines a new commands in the server profile.
|
|
*
|
|
* @param string $commandID Command ID.
|
|
* @param string $commandClass FQN of a class implementing Predis\Command\CommandInterface.
|
|
*/
|
|
public function defineCommand($commandID, $commandClass)
|
|
{
|
|
$reflection = new ReflectionClass($commandClass);
|
|
|
|
if (!$reflection->isSubclassOf('Predis\Command\CommandInterface')) {
|
|
throw new InvalidArgumentException("Cannot register '$commandClass' as it is not a valid Redis command");
|
|
}
|
|
|
|
$this->commands[strtolower($commandID)] = $commandClass;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function setProcessor(CommandProcessorInterface $processor = null)
|
|
{
|
|
$this->processor = $processor;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getProcessor()
|
|
{
|
|
return $this->processor;
|
|
}
|
|
|
|
/**
|
|
* Returns the version of server profile as its string representation.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function __toString()
|
|
{
|
|
return $this->getVersion();
|
|
}
|
|
}
|