Skip to content

Commit 55f966c

Browse files
[12.x] Add context remember functions (#56156)
* Add context remember methods * Update PHPDoc * Fix styling * Accept mixed value * Update PHPDoc * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent e6245ae commit 55f966c

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/Illuminate/Log/Context/Repository.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,42 @@ public function addHidden($key, #[\SensitiveParameter] $value = null)
249249
return $this;
250250
}
251251

252+
/**
253+
* Add a context value if it does not exist yet, and return the value.
254+
*
255+
* @param string $key
256+
* @param mixed $value
257+
* @return mixed
258+
*/
259+
public function remember($key, $value)
260+
{
261+
if ($this->has($key)) {
262+
return $this->get($key);
263+
}
264+
265+
return tap(value($value), function ($value) use ($key) {
266+
$this->add($key, $value);
267+
});
268+
}
269+
270+
/**
271+
* Add a hidden context value if it does not exist yet, and return the value.
272+
*
273+
* @param string $key
274+
* @param mixed $value
275+
* @return mixed
276+
*/
277+
public function rememberHidden($key, #[\SensitiveParameter] $value)
278+
{
279+
if ($this->hasHidden($key)) {
280+
return $this->getHidden($key);
281+
}
282+
283+
return tap(value($value), function ($value) use ($key) {
284+
$this->addHidden($key, $value);
285+
});
286+
}
287+
252288
/**
253289
* Forget the given context key.
254290
*

src/Illuminate/Support/Facades/Context.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
* @method static bool hasMacro(string $name)
4545
* @method static void flushMacros()
4646
* @method static \Illuminate\Database\Eloquent\Model restoreModel(\Illuminate\Contracts\Database\ModelIdentifier $value)
47+
* @method static mixed remember(string $key, mixed $value)
48+
* @method static mixed rememberHidden(string $key, mixed $value)
4749
*
4850
* @see \Illuminate\Log\Context\Repository
4951
*/

tests/Log/ContextTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,42 @@ public function test_it_custom_decrements_a_counter()
662662
Context::decrement('foo', 2);
663663
$this->assertSame(0, Context::get('foo'));
664664
}
665+
666+
public function test_it_remembers_a_value()
667+
{
668+
$this->assertSame(1, Context::remember('int', 1));
669+
670+
$closureRunCount = 0;
671+
$closure = function () use (&$closureRunCount) {
672+
$closureRunCount++;
673+
674+
return 'bar';
675+
};
676+
677+
$this->assertSame('bar', Context::remember('foo', $closure));
678+
$this->assertSame('bar', Context::get('foo'));
679+
680+
Context::remember('foo', $closure);
681+
$this->assertSame(1, $closureRunCount);
682+
}
683+
684+
public function test_it_remembers_a_hidden_value()
685+
{
686+
$this->assertSame(1, Context::rememberHidden('int', 1));
687+
688+
$closureRunCount = 0;
689+
$closure = function () use (&$closureRunCount) {
690+
$closureRunCount++;
691+
692+
return 'bar';
693+
};
694+
695+
$this->assertSame('bar', Context::rememberHidden('foo', $closure));
696+
$this->assertSame('bar', Context::getHidden('foo'));
697+
698+
Context::rememberHidden('foo', $closure);
699+
$this->assertSame(1, $closureRunCount);
700+
}
665701
}
666702

667703
enum Suit

0 commit comments

Comments
 (0)