ServerInfo.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Commands;
  11. class ServerInfo extends Command
  12. {
  13. public function getId()
  14. {
  15. return 'INFO';
  16. }
  17. protected function onPrefixKeys(Array $arguments, $prefix)
  18. {
  19. /* NOOP */
  20. }
  21. protected function canBeHashed()
  22. {
  23. return false;
  24. }
  25. public function parseResponse($data)
  26. {
  27. $info = array();
  28. $infoLines = explode("\r\n", $data, -1);
  29. foreach ($infoLines as $row) {
  30. @list($k, $v) = explode(':', $row);
  31. if ($row === '' || !isset($v)) {
  32. continue;
  33. }
  34. if (!preg_match('/^db\d+$/', $k)) {
  35. if ($k === 'allocation_stats') {
  36. $info[$k] = $this->parseAllocationStats($v);
  37. continue;
  38. }
  39. $info[$k] = $v;
  40. }
  41. else {
  42. $info[$k] = $this->parseDatabaseStats($v);
  43. }
  44. }
  45. return $info;
  46. }
  47. protected function parseDatabaseStats($str)
  48. {
  49. $db = array();
  50. foreach (explode(',', $str) as $dbvar) {
  51. list($dbvk, $dbvv) = explode('=', $dbvar);
  52. $db[trim($dbvk)] = $dbvv;
  53. }
  54. return $db;
  55. }
  56. protected function parseAllocationStats($str)
  57. {
  58. $stats = array();
  59. foreach (explode(',', $str) as $kv) {
  60. @list($size, $objects, $extra) = explode('=', $kv);
  61. // hack to prevent incorrect values when parsing the >=256 key
  62. if (isset($extra)) {
  63. $size = ">=$objects";
  64. $objects = $extra;
  65. }
  66. $stats[$size] = $objects;
  67. }
  68. return $stats;
  69. }
  70. }