ServerInfo.php 2.2 KB

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