RedisProfile.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\ProcessorInterface;
  15. /**
  16. * Base class implementing common functionalities for Redis server profiles.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. abstract class RedisProfile implements ProfileInterface
  21. {
  22. private $commands;
  23. private $processor;
  24. /**
  25. *
  26. */
  27. public function __construct()
  28. {
  29. $this->commands = $this->getSupportedCommands();
  30. }
  31. /**
  32. * Returns a map of all the commands supported by the profile and their
  33. * actual PHP classes.
  34. *
  35. * @return array
  36. */
  37. abstract protected function getSupportedCommands();
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function supportsCommand($commandID)
  42. {
  43. return isset($this->commands[strtoupper($commandID)]);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function supportsCommands(array $commandIDs)
  49. {
  50. foreach ($commandIDs as $commandID) {
  51. if (!$this->supportsCommand($commandID)) {
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. /**
  58. * Returns the fully-qualified name of a class representing the specified
  59. * command ID registered in the current server profile.
  60. *
  61. * @param string $commandID Command ID.
  62. * @return string
  63. */
  64. public function getCommandClass($commandID)
  65. {
  66. if (isset($this->commands[$commandID = strtoupper($commandID)])) {
  67. return $this->commands[$commandID];
  68. }
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function createCommand($commandID, $arguments = array())
  74. {
  75. $commandID = strtoupper($commandID);
  76. if (!isset($this->commands[$commandID])) {
  77. throw new ClientException("Command '$commandID' is not a registered Redis command.");
  78. }
  79. $commandClass = $this->commands[$commandID];
  80. $command = new $commandClass();
  81. $command->setArguments($arguments);
  82. if (isset($this->processor)) {
  83. $this->processor->process($command);
  84. }
  85. return $command;
  86. }
  87. /**
  88. * Defines a new command in the server profile.
  89. *
  90. * @param string $commandID Command ID.
  91. * @param string $class Fully-qualified name of a Predis\Command\CommandInterface.
  92. */
  93. public function defineCommand($commandID, $class)
  94. {
  95. $reflection = new ReflectionClass($class);
  96. if (!$reflection->isSubclassOf('Predis\Command\CommandInterface')) {
  97. throw new InvalidArgumentException("The class '$class' is not a valid command class.");
  98. }
  99. $this->commands[strtoupper($commandID)] = $class;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function setProcessor(ProcessorInterface $processor = null)
  105. {
  106. $this->processor = $processor;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getProcessor()
  112. {
  113. return $this->processor;
  114. }
  115. /**
  116. * Returns the version of server profile as its string representation.
  117. *
  118. * @return string
  119. */
  120. public function __toString()
  121. {
  122. return $this->getVersion();
  123. }
  124. }