RedisProfile.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Profile;
  11. use InvalidArgumentException;
  12. use ReflectionClass;
  13. use Predis\ClientException;
  14. use Predis\Command\Processor\CommandProcessingInterface;
  15. use Predis\Command\Processor\CommandProcessorInterface;
  16. /**
  17. * Base class that implements common functionalities of server profiles.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. abstract class RedisProfile implements ProfileInterface, CommandProcessingInterface
  22. {
  23. private $commands;
  24. private $processor;
  25. /**
  26. *
  27. */
  28. public function __construct()
  29. {
  30. $this->commands = $this->getSupportedCommands();
  31. }
  32. /**
  33. * Returns a map of all the commands supported by the profile and their
  34. * actual PHP classes.
  35. *
  36. * @return array
  37. */
  38. protected abstract function getSupportedCommands();
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function supportsCommand($commandID)
  43. {
  44. return isset($this->commands[strtolower($commandID)]);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function supportsCommands(array $commandIDs)
  50. {
  51. foreach ($commandIDs as $commandID) {
  52. if (!$this->supportsCommand($commandID)) {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. /**
  59. * Returns the FQN of the class that represent the specified command ID
  60. * registered in the current server profile.
  61. *
  62. * @param string $commandID Command ID.
  63. * @return string
  64. */
  65. public function getCommandClass($commandID)
  66. {
  67. if (isset($this->commands[$commandID = strtolower($commandID)])) {
  68. return $this->commands[$commandID];
  69. }
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function createCommand($commandID, $arguments = array())
  75. {
  76. $commandID = strtolower($commandID);
  77. if (!isset($this->commands[$commandID])) {
  78. throw new ClientException("'$commandID' is not a registered Redis command");
  79. }
  80. $commandClass = $this->commands[$commandID];
  81. $command = new $commandClass();
  82. $command->setArguments($arguments);
  83. if (isset($this->processor)) {
  84. $this->processor->process($command);
  85. }
  86. return $command;
  87. }
  88. /**
  89. * Defines a new commands in the server profile.
  90. *
  91. * @param string $commandID Command ID.
  92. * @param string $commandClass FQN of a class implementing Predis\Command\CommandInterface.
  93. */
  94. public function defineCommand($commandID, $commandClass)
  95. {
  96. $reflection = new ReflectionClass($commandClass);
  97. if (!$reflection->isSubclassOf('Predis\Command\CommandInterface')) {
  98. throw new InvalidArgumentException("Cannot register '$commandClass' as it is not a valid Redis command");
  99. }
  100. $this->commands[strtolower($commandID)] = $commandClass;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function setProcessor(CommandProcessorInterface $processor = null)
  106. {
  107. $this->processor = $processor;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function getProcessor()
  113. {
  114. return $this->processor;
  115. }
  116. /**
  117. * Returns the version of server profile as its string representation.
  118. *
  119. * @return string
  120. */
  121. public function __toString()
  122. {
  123. return $this->getVersion();
  124. }
  125. }