Skip to content

Commit 1b453a8

Browse files
[11.x] Add pull methods to Context (#50904)
* [11.x] Add pull and pullHidden methods to Context * [11.x] fix typo in testcase name --------- Co-authored-by: René Geuze <rene@ef2.nl>
1 parent 95aa2fe commit 1b453a8

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/Illuminate/Log/Context/Repository.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,34 @@ public function getHidden($key, $default = null)
119119
return $this->hidden[$key] ?? value($default);
120120
}
121121

122+
/**
123+
* Retrieve the given key's value and then forget it.
124+
*
125+
* @param string $key
126+
* @param mixed $default
127+
* @return mixed
128+
*/
129+
public function pull($key, $default = null)
130+
{
131+
return tap($this->get($key, $default), function () use ($key) {
132+
$this->forget($key);
133+
});
134+
}
135+
136+
/**
137+
* Retrieve the given key's hidden value and then forget it.
138+
*
139+
* @param string $key
140+
* @param mixed $default
141+
* @return mixed
142+
*/
143+
public function pullHidden($key, $default = null)
144+
{
145+
return tap($this->getHidden($key, $default), function () use ($key) {
146+
$this->forgetHidden($key);
147+
});
148+
}
149+
122150
/**
123151
* Retrieve only the values of the given keys.
124152
*

src/Illuminate/Support/Facades/Context.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* @method static array allHidden()
1010
* @method static mixed get(string $key, mixed $default = null)
1111
* @method static mixed getHidden(string $key, mixed $default = null)
12+
* @method static mixed pull(string $key, mixed $default = null)
13+
* @method static mixed pullHidden(string $key, mixed $default = null)
1214
* @method static array only(array $keys)
1315
* @method static array onlyHidden(array $keys)
1416
* @method static \Illuminate\Log\Context\Repository add(string|array $key, mixed $value = null)

tests/Log/ContextTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function test_hydrating_null_triggers_hydrating_event()
114114
$this->assertTrue($called);
115115
}
116116

117-
public function test_it_can_serilize_values()
117+
public function test_it_can_serialize_values()
118118
{
119119
Context::add([
120120
'string' => 'string',
@@ -354,6 +354,19 @@ public function test_it_can_add_hidden()
354354
Context::pushHidden('foo', 2);
355355
}
356356

357+
public function test_it_can_pull()
358+
{
359+
Context::add('foo', 'data');
360+
361+
$this->assertSame('data', Context::pull('foo'));
362+
$this->assertNull(Context::get('foo'));
363+
364+
Context::addHidden('foo', 'data');
365+
366+
$this->assertSame('data', Context::pullHidden('foo'));
367+
$this->assertNull(Context::getHidden('foo'));
368+
}
369+
357370
public function test_it_adds_context_to_logged_exceptions()
358371
{
359372
$path = storage_path('logs/laravel.log');

0 commit comments

Comments
 (0)