ServerInfo.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/info
  13. *
  14. * @author Daniele Alessandri <suppakilla@gmail.com>
  15. */
  16. class ServerInfo extends Command
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getId()
  22. {
  23. return 'INFO';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function parseResponse($data)
  29. {
  30. $info = array();
  31. $infoLines = preg_split('/\r?\n/', $data);
  32. foreach ($infoLines as $row) {
  33. if (strpos($row, ':') === false) {
  34. continue;
  35. }
  36. list($k, $v) = $this->parseRow($row);
  37. $info[$k] = $v;
  38. }
  39. return $info;
  40. }
  41. /**
  42. * Parses a single row of the response and returns the key-value pair.
  43. *
  44. * @param string $row Single row of the response.
  45. *
  46. * @return array
  47. */
  48. protected function parseRow($row)
  49. {
  50. list($k, $v) = explode(':', $row, 2);
  51. if (preg_match('/^db\d+$/', $k)) {
  52. $v = $this->parseDatabaseStats($v);
  53. }
  54. return array($k, $v);
  55. }
  56. /**
  57. * Extracts the statistics of each logical DB from the string buffer.
  58. *
  59. * @param string $str Response buffer.
  60. *
  61. * @return array
  62. */
  63. protected function parseDatabaseStats($str)
  64. {
  65. $db = array();
  66. foreach (explode(',', $str) as $dbvar) {
  67. list($dbvk, $dbvv) = explode('=', $dbvar);
  68. $db[trim($dbvk)] = $dbvv;
  69. }
  70. return $db;
  71. }
  72. /**
  73. * Parses the response and extracts the allocation statistics.
  74. *
  75. * @param string $str Response buffer.
  76. *
  77. * @return array
  78. */
  79. protected function parseAllocationStats($str)
  80. {
  81. $stats = array();
  82. foreach (explode(',', $str) as $kv) {
  83. @list($size, $objects, $extra) = explode('=', $kv);
  84. // hack to prevent incorrect values when parsing the >=256 key
  85. if (isset($extra)) {
  86. $size = ">=$objects";
  87. $objects = $extra;
  88. }
  89. $stats[$size] = $objects;
  90. }
  91. return $stats;
  92. }
  93. }