ServerClient.php 1.6 KB

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