Breadcrumbs.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of Raven.
  4. *
  5. * (c) Sentry Team
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Raven Breadcrumbs
  12. *
  13. * @package raven
  14. */
  15. class Raven_Breadcrumbs
  16. {
  17. public $count;
  18. public $pos;
  19. public $size;
  20. /**
  21. * @var array[]
  22. */
  23. public $buffer;
  24. public function __construct($size = 100)
  25. {
  26. $this->size = $size;
  27. $this->reset();
  28. }
  29. public function reset()
  30. {
  31. $this->count = 0;
  32. $this->pos = 0;
  33. $this->buffer = array();
  34. }
  35. public function record($crumb)
  36. {
  37. if (empty($crumb['timestamp'])) {
  38. $crumb['timestamp'] = microtime(true);
  39. }
  40. $this->buffer[$this->pos] = $crumb;
  41. $this->pos = ($this->pos + 1) % $this->size;
  42. $this->count++;
  43. }
  44. /**
  45. * @return array[]
  46. */
  47. public function fetch()
  48. {
  49. $results = array();
  50. for ($i = 0; $i <= ($this->size - 1); $i++) {
  51. $idx = ($this->pos + $i) % $this->size;
  52. if (isset($this->buffer[$idx])) {
  53. $results[] = $this->buffer[$idx];
  54. }
  55. }
  56. return $results;
  57. }
  58. public function is_empty()
  59. {
  60. return $this->count === 0;
  61. }
  62. public function to_json()
  63. {
  64. return array(
  65. 'values' => $this->fetch(),
  66. );
  67. }
  68. }