ServerInfo.php 2.4 KB

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