ServerInfo.php 2.3 KB

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