Skip to content
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

[11.x] Introduce "Context" #49730

Merged
merged 32 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3a9df3f
Add "context"
timacdonald Jan 18, 2024
ab89cde
Inject event dispatcher
timacdonald Feb 14, 2024
d6058d0
Use a scoped instance
timacdonald Feb 14, 2024
5cbddd1
Allow app to be refreshed
timacdonald Feb 15, 2024
d423af9
lint
timacdonald Feb 15, 2024
7d1bda4
Update facade
timacdonald Feb 15, 2024
90e498e
Resolve using current app instance
timacdonald Feb 15, 2024
0a5e0e4
serialization
timacdonald Feb 28, 2024
3f2c93a
serialization
timacdonald Feb 28, 2024
1a7914b
lint
timacdonald Feb 28, 2024
0caf265
tests
timacdonald Feb 28, 2024
869602c
wip
timacdonald Feb 28, 2024
9559ef8
report
timacdonald Feb 28, 2024
6250e6d
facade
timacdonald Feb 28, 2024
2f942f9
Typo
timacdonald Feb 28, 2024
570b5e7
formatting
timacdonald Feb 28, 2024
8a5d3ef
Octane fix
timacdonald Feb 28, 2024
19498dd
lint
timacdonald Feb 28, 2024
a4721e5
Reduce API
timacdonald Feb 28, 2024
d7a4fc8
lint
timacdonald Feb 28, 2024
c1cdffa
Update tests/Integration/Log/ContextIntegrationTest.php
timacdonald Feb 29, 2024
152382e
Update tests/Integration/Log/ContextIntegrationTest.php
timacdonald Feb 29, 2024
ccb4106
wip
crynobone Feb 29, 2024
e4f0641
wip
crynobone Feb 29, 2024
88215cd
wip
crynobone Feb 29, 2024
d9be3ac
formatting
taylorotwell Mar 8, 2024
8d5afa3
add alias
taylorotwell Mar 8, 2024
d229f1d
formatting
taylorotwell Mar 8, 2024
e40d4fe
formatting
taylorotwell Mar 8, 2024
593fc4c
check for function
taylorotwell Mar 8, 2024
88295f8
add null hydration test
taylorotwell Mar 8, 2024
f97a4d1
rename events
taylorotwell Mar 8, 2024
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
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 @@ -287,6 +288,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);
});
Comment on lines +38 to +40

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @timacdonald, JobRetryRequested has a different interface for the payload.

for example, spatie/laravel-multitenacy has covered this here

protected function getEventPayload($event): ?array
    {
        return match (true) {
            $event instanceof JobProcessing => $event->job->payload(),
            $event instanceof JobRetryRequested => $event->payload(),
            default => null,
        };
    }

Am I missing something or this scenario was not mapped out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What issue are you seeing in particular?

}
}
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
Loading