|
4 | 4 | [](https://github.com/ensi-platform/laravel-initiator-propagation/actions/workflows/run-tests.yml) |
5 | 5 | [](https://packagist.org/packages/ensi/laravel-initiator-propagation) |
6 | 6 |
|
7 | | -Generates Laravel application code from Open Api Specification files |
| 7 | +Provides a bunch of ready-to use middleware to integrate [ensi/initiator-propagation](https://github.com/ensi-platform/php-initiator-propagation/) in Laravel application. |
| 8 | +You are free to replace any of them with your own implementations. |
8 | 9 |
|
9 | 10 | ## Installation |
10 | 11 |
|
11 | 12 | You can install the package via composer: |
12 | 13 |
|
13 | 14 | `composer require ensi/laravel-initiator-propagation` |
14 | 15 |
|
15 | | -You can publish config file like this: |
| 16 | +Publish config file like this: |
16 | 17 |
|
17 | 18 | `php artisan vendor:publish --provider="Ensi\LaravelInitiatorPropagation\LaravelInitiatorPropagationServiceProvider"` |
18 | 19 |
|
19 | 20 | ## Basic Usage |
20 | 21 |
|
21 | | -## Setting initiator |
| 22 | +```php |
| 23 | +$holder = resolve(InitiatorHolder::class); |
| 24 | +var_dump($holder->getInitiator()); |
| 25 | +$holder->setInitiator(new InitiatorDTO(...)); |
| 26 | +``` |
22 | 27 |
|
23 | | -You can use build-in `Ensi\InitiatorPropagation\ParseInitiatorHeaderLaravelMiddleware` to populate `InitiatorHolder` with data from incoming request. |
24 | | -It's also recommended to add `$this->app->instance(InitiatorHolder::class, InitiatorHolder::getInstance());` to one of your service providers to make `InitiatorHolder` singleton injectable via Laravel Service Container |
| 28 | +### HTTP Requests |
| 29 | + |
| 30 | +#### Setting initiator |
| 31 | + |
| 32 | +You typically create a new initiator when you receive a HTTP request coming from a client you do not own. E.g in an API Gateway. |
| 33 | +There is a built-in `Ensi\LaravelInitiatorPropagation\SetInitiatorHttpMiddleware` for that. |
| 34 | +It creates an `InitiatorDTO` and places it to the `InitiatorHolder` singleton. |
| 35 | +- `userId` and `entrypoint` are set from request. |
| 36 | +- `app` is set according to config options. |
| 37 | +- `userType` is set from the package config. `userType` is empty for a not authenticated user. |
| 38 | +- `correlationId` and `startedAt` are set from request headers according to config options or generated from scratch. |
| 39 | +- `realUserId` and `realUserType` are left empty strings. |
| 40 | + |
| 41 | +Be sure to add the midlleware AFTER Laravel middleware that sets authenticated user. |
| 42 | +In practice it likely means that you have to place the middleare at the very bottom of `middlewareGroups` in `app/Http/Kernel` |
| 43 | + |
| 44 | +#### Parsing incoming initiator |
| 45 | + |
| 46 | +Add `Ensi\LaravelInitiatorPropagation\ParseInitiatorHeaderMiddleware` to `app/Http/Kernel` middleware property. |
| 47 | +This middleware parses `X-Initiator` HTTP header, deserializes it into `InitiatorDTO` object and places it to the `InitiatorHolder` singleton. |
| 48 | + |
| 49 | +#### Propagating initiator to outcomming HTTP request |
| 50 | +The package provides a `Ensi\InitiatorPropagation\PropagateInitiatorGuzzleMiddleware` Guzzle Middleware that converts ` resolve(InitiatorHolder::class)->getInitiator()` back to `X-Inititator` header and sets this header for all outcomming guzzle request. |
| 51 | + |
| 52 | +You can add it to your guzzle stack like this: |
| 53 | + |
| 54 | +```php |
| 55 | +$handlerStack = new HandlerStack(Utils::chooseHandler()); |
| 56 | +$handlerStack->push(new PropagateInitiatorGuzzleMiddleware()); |
| 57 | +``` |
| 58 | + |
| 59 | +### CLI |
| 60 | + |
| 61 | +#### Artisan Commands |
| 62 | + |
| 63 | +There is a custom artisan `Ensi\LaravelInitiatorPropagation\SetInitiatorArtisanMiddleware` that sets new initiator in every artisan command that you run. |
| 64 | +You can add it to the `app\Console\Kernel` like that: |
| 65 | + |
| 66 | +```php |
| 67 | +public function bootstrap() |
| 68 | +{ |
| 69 | + parent::bootstrap(); |
| 70 | + (new SetInitiatorArtisanMiddleware())->handle(); |
| 71 | +} |
| 72 | +``` |
| 73 | +This middleware sets artisan command name (including argument, excluding options) as `$initiatorDTO->entrypoint`. |
| 74 | +If your custom artisan command makes guzzle HTTP requests to other apps the `PropagateInitiatorGuzzleMiddleware` uses this initiator. |
| 75 | +This middleware also works fine for [Laravel Task Scheduling](https://laravel.com/docs/latest/scheduling). |
| 76 | + |
| 77 | +#### Queue Jobs |
| 78 | + |
| 79 | +You typically want to persist initiator between incoming HTTP request and queued job. |
| 80 | +The package can help you here aswell. Unfortunately you need to touch a given job: |
| 81 | + |
| 82 | +```php |
| 83 | +use Ensi\LaravelInitiatorPropagation\Job; |
| 84 | + |
| 85 | +// Extend the job from package |
| 86 | +class TestJob extends Job implements ShouldQueue |
| 87 | +{ |
| 88 | + public function __construct(protected Customer $customer) |
| 89 | + { |
| 90 | + // Do not forget to call parent constuctor |
| 91 | + parent::__construct(); |
| 92 | + } |
| 93 | + |
| 94 | + public function handle() |
| 95 | + { |
| 96 | + // Initiator is automatically persisted to InitiatorHolder via job middleware in parent class, |
| 97 | + // You do not need to persist it manually |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +#### Laravel Queable Actions |
| 103 | + |
| 104 | +If you use [spatie/laravel-queueable-action](https://github.com/spatie/laravel-queueable-action) package to dispatch actions instead of jobs you do not need to mess with every job separately. |
| 105 | + |
| 106 | +Just publish `laravel-queueable-action` config and set the special Job class there: |
| 107 | + |
| 108 | +```php |
| 109 | +'job_class' => \Ensi\InitiatorPropagation\ActionJob::class, |
| 110 | +``` |
25 | 111 |
|
26 | 112 | ## Contributing |
27 | 113 |
|
|
0 commit comments