Skip to content

[11.x] Add pull methods to Context #50904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Illuminate/Log/Context/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,34 @@ public function getHidden($key, $default = null)
return $this->hidden[$key] ?? value($default);
}

/**
* Retrieve the given key's value and then forget it.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function pull($key, $default = null)
{
return tap($this->get($key, $default), function () use ($key) {
$this->forget($key);
});
}

/**
* Retrieve the given key's hidden value and then forget it.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function pullHidden($key, $default = null)
{
return tap($this->getHidden($key, $default), function () use ($key) {
$this->forgetHidden($key);
});
}

/**
* Retrieve only the values of the given keys.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Support/Facades/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* @method static array allHidden()
* @method static mixed get(string $key, mixed $default = null)
* @method static mixed getHidden(string $key, mixed $default = null)
* @method static mixed pull(string $key, mixed $default = null)
* @method static mixed pullHidden(string $key, mixed $default = null)
* @method static array only(array $keys)
* @method static array onlyHidden(array $keys)
* @method static \Illuminate\Log\Context\Repository add(string|array $key, mixed $value = null)
Expand Down
15 changes: 14 additions & 1 deletion tests/Log/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function test_hydrating_null_triggers_hydrating_event()
$this->assertTrue($called);
}

public function test_it_can_serilize_values()
public function test_it_can_serialize_values()
{
Context::add([
'string' => 'string',
Expand Down Expand Up @@ -354,6 +354,19 @@ public function test_it_can_add_hidden()
Context::pushHidden('foo', 2);
}

public function test_it_can_pull()
{
Context::add('foo', 'data');

$this->assertSame('data', Context::pull('foo'));
$this->assertNull(Context::get('foo'));

Context::addHidden('foo', 'data');

$this->assertSame('data', Context::pullHidden('foo'));
$this->assertNull(Context::getHidden('foo'));
}

public function test_it_adds_context_to_logged_exceptions()
{
$path = storage_path('logs/laravel.log');
Expand Down