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\Commands;
  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. protected function canBeHashed()
  28. {
  29. return false;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function parseResponse($data)
  35. {
  36. $info = array();
  37. $infoLines = preg_split('/\r?\n/', $data);
  38. foreach ($infoLines as $row) {
  39. @list($k, $v) = explode(':', $row);
  40. if ($row === '' || !isset($v)) {
  41. continue;
  42. }
  43. if (!preg_match('/^db\d+$/', $k)) {
  44. if ($k === 'allocation_stats') {
  45. $info[$k] = $this->parseAllocationStats($v);
  46. continue;
  47. }
  48. $info[$k] = $v;
  49. }
  50. else {
  51. $info[$k] = $this->parseDatabaseStats($v);
  52. }
  53. }
  54. return $info;
  55. }
  56. /**
  57. * Parses the reply buffer and extracts the statistics of each logical DB.
  58. *
  59. * @param string $str Reply buffer.
  60. * @return array
  61. */
  62. protected function parseDatabaseStats($str)
  63. {
  64. $db = array();
  65. foreach (explode(',', $str) as $dbvar) {
  66. list($dbvk, $dbvv) = explode('=', $dbvar);
  67. $db[trim($dbvk)] = $dbvv;
  68. }
  69. return $db;
  70. }
  71. /**
  72. * Parses the reply buffer and extracts the allocation statistics.
  73. *
  74. * @param string $str Reply buffer.
  75. * @return array
  76. */
  77. protected function parseAllocationStats($str)
  78. {
  79. $stats = array();
  80. foreach (explode(',', $str) as $kv) {
  81. @list($size, $objects, $extra) = explode('=', $kv);
  82. // hack to prevent incorrect values when parsing the >=256 key
  83. if (isset($extra)) {
  84. $size = ">=$objects";
  85. $objects = $extra;
  86. }
  87. $stats[$size] = $objects;
  88. }
  89. return $stats;
  90. }
  91. }