ServerClient.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/commands/client-list
  13. * @link http://redis.io/commands/client-kill
  14. * @link http://redis.io/commands/client-getname
  15. * @link http://redis.io/commands/client-setname
  16. * @author Daniele Alessandri <suppakilla@gmail.com>
  17. */
  18. class ServerClient extends Command
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function getId()
  24. {
  25. return 'CLIENT';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function parseResponse($data)
  31. {
  32. $args = array_change_key_case($this->getArguments(), CASE_UPPER);
  33. switch (strtoupper($args[0])) {
  34. case 'LIST':
  35. return $this->parseClientList($data);
  36. case 'KILL':
  37. case 'GETNAME':
  38. case 'SETNAME':
  39. default:
  40. return $data;
  41. }
  42. }
  43. /**
  44. * Parses the response to CLIENT LIST and returns a structured list.
  45. *
  46. * @param string $data Response buffer.
  47. * @return array
  48. */
  49. protected function parseClientList($data)
  50. {
  51. $clients = array();
  52. foreach (explode("\n", $data, -1) as $clientData) {
  53. $client = array();
  54. foreach (explode(' ', $clientData) as $kv) {
  55. @list($k, $v) = explode('=', $kv);
  56. $client[$k] = $v;
  57. }
  58. $clients[] = $client;
  59. }
  60. return $clients;
  61. }
  62. }