File tree Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -369,6 +369,23 @@ public function popHidden($key)
369
369
return array_pop ($ this ->hidden [$ key ]);
370
370
}
371
371
372
+ /**
373
+ * Add or update a context counter.
374
+ *
375
+ * @param string $key
376
+ * @param int $increment
377
+ * @return $this
378
+ */
379
+ public function counter (string $ key , int $ increment = 1 )
380
+ {
381
+ $ this ->add (
382
+ $ key ,
383
+ (int ) $ this ->get ($ key , 0 ) + $ increment ,
384
+ );
385
+
386
+ return $ this ;
387
+ }
388
+
372
389
/**
373
390
* Determine if the given value is in the given stack.
374
391
*
Original file line number Diff line number Diff line change 25
25
* @method static mixed pop(string $key)
26
26
* @method static \Illuminate\Log\Context\Repository pushHidden(string $key, mixed ...$values)
27
27
* @method static mixed popHidden(string $key)
28
+ * @method static mixed counter(string $key, int $increment)
28
29
* @method static bool stackContains(string $key, mixed $value, bool $strict = false)
29
30
* @method static bool hiddenStackContains(string $key, mixed $value, bool $strict = false)
30
31
* @method static mixed scope(callable $callback, array $data = [], array $hidden = [])
Original file line number Diff line number Diff line change @@ -602,6 +602,24 @@ public function test_can_rebind_to_separate_class()
602
602
603
603
file_put_contents ($ path , '' );
604
604
}
605
+
606
+ public function test_it_adds_counters ()
607
+ {
608
+ Context::counter ('foo ' );
609
+ $ this ->assertSame (1 , Context::get ('foo ' ));
610
+
611
+ Context::counter ('foo ' );
612
+ $ this ->assertSame (2 , Context::get ('foo ' ));
613
+ }
614
+
615
+ public function test_it_custom_increments_counters ()
616
+ {
617
+ Context::counter ('foo ' , 2 );
618
+ $ this ->assertSame (2 , Context::get ('foo ' ));
619
+
620
+ Context::counter ('foo ' , 3 );
621
+ $ this ->assertSame (5 , Context::get ('foo ' ));
622
+ }
605
623
}
606
624
607
625
enum Suit
You can’t perform that action at this time.
0 commit comments