Skip to content

Commit

Permalink
[11.x] Introduce "Context" (#49730)
Browse files Browse the repository at this point in the history
Context allows you to track current and historic "context" (information about the world) throughout a single request / command and across logical boundaries, such as queued jobs.
  • Loading branch information
timacdonald authored Mar 8, 2024
1 parent 5e87357 commit abfd659
Show file tree
Hide file tree
Showing 12 changed files with 1,163 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/facades.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
Illuminate\\Support\\Facades\\Bus \
Illuminate\\Support\\Facades\\Cache \
Illuminate\\Support\\Facades\\Config \
Illuminate\\Support\\Facades\\Context \
Illuminate\\Support\\Facades\\Cookie \
Illuminate\\Support\\Facades\\Crypt \
Illuminate\\Support\\Facades\\DB \
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
use Illuminate\Foundation\Events\LocaleUpdated;
use Illuminate\Http\Request;
use Illuminate\Log\Context\ContextServiceProvider;
use Illuminate\Log\LogServiceProvider;
use Illuminate\Routing\RoutingServiceProvider;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -288,6 +289,7 @@ protected function registerBaseServiceProviders()
{
$this->register(new EventServiceProvider($this));
$this->register(new LogServiceProvider($this));
$this->register(new ContextServiceProvider($this));
$this->register(new RoutingServiceProvider($this));
}

Expand Down
42 changes: 42 additions & 0 deletions src/Illuminate/Log/Context/ContextServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Illuminate\Log\Context;

use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Queue;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\ServiceProvider;

class ContextServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->scoped(Repository::class);
}

/**
* Boot the application services.
*
* @return void
*/
public function boot()
{
Queue::createPayloadUsing(function ($connection, $queue, $payload) {
$context = Context::dehydrate();

return $context === null ? $payload : [
...$payload,
'illuminate:log:context' => $context,
];
});

$this->app['events']->listen(function (JobProcessing $event) {
Context::hydrate($event->job->payload()['illuminate:log:context'] ?? null);
});
}
}
23 changes: 23 additions & 0 deletions src/Illuminate/Log/Context/Events/ContextDehydrating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Illuminate\Log\Context\Events;

class ContextDehydrating
{
/**
* The context instance.
*
* @var \Illuminate\Log\Context\Repository
*/
public $context;

/**
* Create a new event instance.
*
* @param \Illuminate\Log\Context\Repository $context
*/
public function __construct($context)
{
$this->context = $context;
}
}
23 changes: 23 additions & 0 deletions src/Illuminate/Log/Context/Events/ContextHydrated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Illuminate\Log\Context\Events;

class ContextHydrated
{
/**
* The context instance.
*
* @var \Illuminate\Log\Context\Repository
*/
public $context;

/**
* Create a new event instance.
*
* @param \Illuminate\Log\Context\Repository $context
*/
public function __construct($context)
{
$this->context = $context;
}
}
Loading

0 comments on commit abfd659

Please sign in to comment.