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