@@ -15,72 +15,88 @@ class Recorder implements Countable, Iterator
1515 *
1616 * @var int
1717 */
18- const MAX_ITEMS = 25 ;
18+ private $ maxBreadcrumbs = 50 ;
1919
2020 /**
2121 * The recorded breadcrumbs.
2222 *
2323 * @var \Bugsnag\Breadcrumbs\Breadcrumb[]
2424 */
25- protected $ breadcrumbs = [];
25+ private $ breadcrumbs = [];
2626
2727 /**
28- * The head position.
28+ * The iteration position.
2929 *
3030 * @var int
3131 */
32- protected $ head = 0 ;
32+ private $ position = 0 ;
3333
3434 /**
35- * The pointer position .
35+ * Record a breadcrumb .
3636 *
37- * @var int
37+ * @param \Bugsnag\Breadcrumbs\Breadcrumb $breadcrumb
38+ *
39+ * @return void
3840 */
39- protected $ pointer = 0 ;
41+ public function record (Breadcrumb $ breadcrumb )
42+ {
43+ $ this ->breadcrumbs [] = $ breadcrumb ;
44+
45+ // drop the oldest breadcrumb if we're over the max
46+ if ($ this ->count () > $ this ->maxBreadcrumbs ) {
47+ array_shift ($ this ->breadcrumbs );
48+ }
49+ }
4050
4151 /**
42- * The iteration position .
52+ * Clear all recorded breadcrumbs .
4353 *
44- * @var int
54+ * @return void
4555 */
46- protected $ position = 0 ;
56+ public function clear ()
57+ {
58+ $ this ->position = 0 ;
59+ $ this ->breadcrumbs = [];
60+ }
4761
4862 /**
49- * Record a breadcrumb .
63+ * Set the maximum number of breadcrumbs that are allowed to be stored .
5064 *
51- * We're recording a maximum of 25 breadcrumbs. Once we've recorded 25, we
52- * start wrapping back around and replacing the earlier ones. In order to
53- * indicate the start of the list, we advance a head pointer.
65+ * This must be an integer between 0 and 100 (inclusive).
5466 *
55- * @param \Bugsnag\Breadcrumbs\Breadcrumb $breadcrumb
67+ * @param int $maxBreadcrumbs
5668 *
5769 * @return void
5870 */
59- public function record ( Breadcrumb $ breadcrumb )
71+ public function setMaxBreadcrumbs ( $ maxBreadcrumbs )
6072 {
61- // advance the head by one if we've caught up
62- if ($ this ->breadcrumbs && $ this ->pointer === $ this ->head ) {
63- $ this ->head = ($ this ->head + 1 ) % static ::MAX_ITEMS ;
73+ if (!is_int ($ maxBreadcrumbs ) || $ maxBreadcrumbs < 0 || $ maxBreadcrumbs > 100 ) {
74+ error_log (
75+ 'Bugsnag Warning: maxBreadcrumbs should be an integer between 0 and 100 (inclusive) '
76+ );
77+
78+ return ;
6479 }
6580
66- // record the new breadcrumb
67- $ this ->breadcrumbs [$ this ->pointer ] = $ breadcrumb ;
81+ $ this ->maxBreadcrumbs = $ maxBreadcrumbs ;
6882
69- // advance the pointer so we set the next breadcrumb in the next slot
70- $ this ->pointer = ($ this ->pointer + 1 ) % static ::MAX_ITEMS ;
83+ // drop the oldest breadcrumbs if we're over the max
84+ if ($ this ->count () > $ this ->maxBreadcrumbs ) {
85+ $ this ->breadcrumbs = array_slice (
86+ $ this ->breadcrumbs ,
87+ $ this ->count () - $ this ->maxBreadcrumbs
88+ );
89+ }
7190 }
7291
7392 /**
74- * Clear all recorded breadcrumbs.
93+ * Get the maximum number of breadcrumbs that are allowed to be stored .
7594 *
76- * @return void
95+ * @return int
7796 */
78- public function clear ()
97+ public function getMaxBreadcrumbs ()
7998 {
80- $ this ->head = 0 ;
81- $ this ->pointer = 0 ;
82- $ this ->position = 0 ;
83- $ this ->breadcrumbs = [];
99+ return $ this ->maxBreadcrumbs ;
84100 }
85101
86102 /**
@@ -102,7 +118,7 @@ public function count()
102118 #[\ReturnTypeWillChange]
103119 public function current ()
104120 {
105- return $ this ->breadcrumbs [( $ this ->head + $ this -> position ) % static :: MAX_ITEMS ];
121+ return $ this ->breadcrumbs [$ this ->position ];
106122 }
107123
108124 /**
0 commit comments