ServerInfo.php 2.2 KB

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