PHP code example of abdulsalam / laravel-context-flow

1. Go to this page and download the library: Download abdulsalam/laravel-context-flow library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

abdulsalam / laravel-context-flow example snippets


use Illuminate\Support\Facades\Context;

Context::add('tenant_id', $tenant->id);
Context::add('actor_id', auth()->id());
Context::add('locale', app()->getLocale());

CreateWithdrawalJob::dispatch($withdrawal);

Context::get('tenant_id');
Context::get('actor_id');

use Abdulsalam\LaravelContextFlow\Facades\ContextFlow;

ContextFlow::correlationId();
ContextFlow::executionId();
ContextFlow::causationId();
ContextFlow::requestId();

// config/context-flow.php
'keys' => [
    'tenant_id' => [
        'targets' => ['queue', 'http.internal'],
        'accept_from' => ['internal'],
        'priority' => 500,
        'max_bytes' => 128,
    ],

    'actor_id' => [
        'targets' => ['queue', 'http.internal'],
        'accept_from' => ['internal'],
        'priority' => 500,
        'max_bytes' => 128,
    ],

    'locale' => [
        'targets' => ['queue', 'http.internal'],
        'accept_from' => ['internal', 'public'],
        'priority' => 200,
        'max_bytes' => 32,
    ],
],

'http' => [
    'trusted_hosts' => [
        '*.internal.example.com',
        '*.svc.cluster.local',
    ],
],

Http::post('https://wallet.internal.example.com/withdrawals', [
    'amount' => 100,
]);

Http::post('https://api.stripe.com/...');

'security' => [
    'default_remote_policy' => 'deny',
],

'http' => [
    'accept_public_correlation_id' => false,
],

use Abdulsalam\LaravelContextFlow\Contracts\ContextEnricher;

final class AuthContextEnricher implements ContextEnricher
{
    public function enrich(): array
    {
        return [
            'actor_id' => auth()->id(),
        ];
    }
}

'enrichers' => [
    App\Context\AuthContextEnricher::class,
],

Route::middleware(['auth:sanctum', 'context-flow.enrich'])->group(function () {
    // ...
});

'queue' => [
    'unregistered_keys' => 'preserve',
],

'unregistered_keys' => 'drop',

'debug_payload' => [
    'targets' => [],
    'accept_from' => [],
],

ContextFlow::withoutPropagation(function () {
    Http::post('https://example.com');
    SensitiveJob::dispatch();
});

ContextFlow::only(['locale'], function () {
    SomeJob::dispatch();
});

Log::info('withdrawal created');

Abdulsalam\LaravelContextFlow\Contracts\ExceptionContextReporter

'limits' => [
    'max_keys' => 32,
    'key_bytes' => 128,
    'value_bytes' => 1024,
    'http_total_bytes' => 4096,
    'overflow' => 'drop_low_priority',
],

use Abdulsalam\LaravelContextFlow\Testing\InteractsWithContextFlow;

class WithdrawalTest extends TestCase
{
    use InteractsWithContextFlow;

    public function test_context(): void
    {
        // ...
        $this->assertCorrelationIdExists();
        $this->assertContextValue('tenant_id', 12);
    }
}

$this->app->bind(
    \Abdulsalam\LaravelContextFlow\Contracts\PropagationPolicy::class,
    App\Context\StrictPropagationPolicy::class,
);

$this->app->bind(
    \Abdulsalam\LaravelContextFlow\Contracts\TrustResolver::class,
    App\Context\ServiceDiscoveryTrustResolver::class,
);
bash
php artisan vendor:publish --tag=context-flow-config
bash
php artisan context-flow:inspect
bash
php artisan context-flow:doctor
bash
composer install
composer test
php artisan context-flow:doctor