Skip to content

Commit

Permalink
[11.x] Add a new helper for context (#50878)
Browse files Browse the repository at this point in the history
* Add a new helper for context

* Formatting

* Clean

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
michaelnabil230 and taylorotwell authored Apr 1, 2024
1 parent c215697 commit 9983d7b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Foundation\Mix;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Log\Context\Repository as ContextRepository;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Date;
Expand Down Expand Up @@ -288,6 +289,24 @@ function config_path($path = '')
}
}

if (! function_exists('context')) {
/**
* Get / set the specified context value.
*
* @param array|string|null $key
* @param mixed $default
* @return mixed|\Illuminate\Log\Context\Repository
*/
function context($key, $default = null)
{
return match (true) {
is_null($key) => app(ContextRepository::class),
is_array($key) => app(ContextRepository::class)->add($key),
default => app(ContextRepository::class)->get($key, $default),
};
}
}

if (! function_exists('cookie')) {
/**
* Create a new cookie instance.
Expand Down

0 comments on commit 9983d7b

Please sign in to comment.