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