ServerInfo.php 2.5 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. * @author Daniele Alessandri <suppakilla@gmail.com>
  14. */
  15. class ServerInfo extends AbstractCommand
  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 single row of the reply buffer and returns the key-value pair.
  42. *
  43. * @param string $row Single row of the reply buffer.
  44. * @return array
  45. */
  46. public function parseRow($row)
  47. {
  48. list($k, $v) = explode(':', $row, 2);
  49. if (!preg_match('/^db\d+$/', $k)) {
  50. if ($k === 'allocation_stats') {
  51. $v = $this->parseAllocationStats($v);
  52. }
  53. } else {
  54. $v = $this->parseDatabaseStats($v);
  55. }
  56. return array($k, $v);
  57. }
  58. /**
  59. * Parses the reply buffer and extracts the statistics of each logical DB.
  60. *
  61. * @param string $str Reply buffer.
  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 reply buffer and extracts the allocation statistics.
  75. *
  76. * @param string $str Reply buffer.
  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. }