Skip to content

Commit b6304b2

Browse files
committed
Add guzzle middleware
1 parent d6b3023 commit b6304b2

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ var_dump($holder->getInitiator());
2525
$holder->setInitiator(new InitiatorDTO(...));
2626
```
2727

28+
You must always resolve InitiatorHolder from the service container instead of `InitiatorHolder::getInstance`.
29+
This is made forLaravel Octane compatibility.
30+
2831
### HTTP Requests
2932

3033
#### Setting initiator
@@ -47,13 +50,13 @@ Add `Ensi\LaravelInitiatorPropagation\ParseInitiatorHeaderMiddleware` to `app/Ht
4750
This middleware parses `X-Initiator` HTTP header, deserializes it into `InitiatorDTO` object and places it to the `InitiatorHolder` singleton.
4851

4952
#### 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.
53+
The package provides a `Ensi\LaravelInitiatorPropagation\PropagateInitiatorLaravelGuzzleMiddleware` Guzzle Middleware that converts ` resolve(InitiatorHolder::class)->getInitiator()` back to `X-Inititator` header and sets this header for all outcomming guzzle request.
5154

5255
You can add it to your guzzle stack like this:
5356

5457
```php
5558
$handlerStack = new HandlerStack(Utils::chooseHandler());
56-
$handlerStack->push(new PropagateInitiatorGuzzleMiddleware());
59+
$handlerStack->push(new PropagateInitiatorLaravelGuzzleMiddleware());
5760
```
5861

5962
### CLI
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Ensi\LaravelInitiatorPropagation;
4+
5+
use Ensi\InitiatorPropagation\Config;
6+
use Ensi\InitiatorPropagation\InitiatorHolder;
7+
use Illuminate\Container\Container;
8+
use Psr\Http\Message\RequestInterface;
9+
10+
class PropagateInitiatorLaravelGuzzleMiddleware
11+
{
12+
public function __invoke(callable $handler): callable
13+
{
14+
return function (RequestInterface $request, $options) use ($handler) {
15+
$inititiator = Container::getInstance()->make(InitiatorHolder::class)->getInitiator();
16+
17+
return $handler(
18+
$inititiator ? $request->withHeader(Config::REQUEST_HEADER, $inititiator->serialize()) : $request,
19+
$options
20+
);
21+
};
22+
}
23+
}

0 commit comments

Comments
 (0)