ServerSentinel.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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;
  11. /**
  12. * @link http://redis.io/topics/sentinel
  13. *
  14. * @author Daniele Alessandri <suppakilla@gmail.com>
  15. */
  16. class ServerSentinel extends Command
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getId()
  22. {
  23. return 'SENTINEL';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function parseResponse($data)
  29. {
  30. switch (strtolower($this->getArgument(0))) {
  31. case 'masters':
  32. case 'slaves':
  33. return self::processMastersOrSlaves($data);
  34. default:
  35. return $data;
  36. }
  37. }
  38. /**
  39. * Returns a processed response to SENTINEL MASTERS or SENTINEL SLAVES.
  40. *
  41. * @param array $servers List of Redis servers.
  42. *
  43. * @return array
  44. */
  45. protected static function processMastersOrSlaves(array $servers)
  46. {
  47. foreach ($servers as $idx => $node) {
  48. $processed = array();
  49. $count = count($node);
  50. for ($i = 0; $i < $count; ++$i) {
  51. $processed[$node[$i]] = $node[++$i];
  52. }
  53. $servers[$idx] = $processed;
  54. }
  55. return $servers;
  56. }
  57. }