ServerInfo.php 2.5 KB

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