KeyPrefixProcessor.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\Command\Processor;
  11. use Predis\Command\CommandInterface;
  12. use Predis\Command\PrefixableCommandInterface;
  13. /**
  14. * Command processor that is used to prefix the keys contained in the arguments
  15. * of a Redis command.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class KeyPrefixProcessor implements CommandProcessorInterface
  20. {
  21. private $prefix;
  22. /**
  23. * @param string $prefix Prefix for the keys.
  24. */
  25. public function __construct($prefix)
  26. {
  27. $this->setPrefix($prefix);
  28. }
  29. /**
  30. * Sets a prefix that is applied to all the keys.
  31. *
  32. * @param string $prefix Prefix for the keys.
  33. */
  34. public function setPrefix($prefix)
  35. {
  36. $this->prefix = $prefix;
  37. }
  38. /**
  39. * Gets the current prefix.
  40. *
  41. * @return string
  42. */
  43. public function getPrefix()
  44. {
  45. return $this->prefix;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function process(CommandInterface $command)
  51. {
  52. if ($command instanceof PrefixableCommandInterface) {
  53. $command->prefixKeys($this->prefix);
  54. }
  55. }
  56. }