Skip to content

Commit 5126072

Browse files
committed
Middleware 4.0
1 parent b0d2a60 commit 5126072

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

src/Middleware.php

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,42 @@
22

33
namespace ApiClients\Middleware\Timer\Response;
44

5-
use ApiClients\Foundation\Middleware\ErrorTrait;
5+
use ApiClients\Foundation\Middleware\Annotation\First;
6+
use ApiClients\Foundation\Middleware\Annotation\Last;
67
use ApiClients\Foundation\Middleware\MiddlewareInterface;
7-
use ApiClients\Foundation\Middleware\Priority;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
1010
use React\Promise\CancellablePromiseInterface;
11+
use Throwable;
1112
use function React\Promise\resolve;
1213

1314
final class Middleware implements MiddlewareInterface
1415
{
15-
use ErrorTrait;
16-
1716
const HEADER = 'X-Middleware-Timer-Response';
1817

1918
/**
20-
* @var float
19+
* @var float[]
2120
*/
22-
private $time;
21+
private $times;
2322

2423
/**
2524
* Return the processed $request via a fulfilled promise.
2625
* When implementing cache or other feature that returns a response, do it with a rejected promise.
2726
* If neither is possible, e.g. on some kind of failure, resolve the unaltered request.
2827
*
2928
* @param RequestInterface $request
29+
* @param string $transactionId
3030
* @param array $options
3131
* @return CancellablePromiseInterface
32+
*
33+
* @Last()
3234
*/
33-
public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface
34-
{
35-
$this->time = microtime(true);
35+
public function pre(
36+
RequestInterface $request,
37+
string $transactionId,
38+
array $options = []
39+
): CancellablePromiseInterface {
40+
$this->times[$transactionId] = microtime(true);
3641

3742
return resolve($request);
3843
}
@@ -41,24 +46,34 @@ public function pre(RequestInterface $request, array $options = []): Cancellable
4146
* Return the processed $response via a promise.
4247
*
4348
* @param ResponseInterface $response
49+
* @param string $transactionId
4450
* @param array $options
4551
* @return CancellablePromiseInterface
52+
*
53+
* @First()
4654
*/
47-
public function post(ResponseInterface $response, array $options = []): CancellablePromiseInterface
48-
{
49-
$time = microtime(true) - $this->time;
55+
public function post(
56+
ResponseInterface $response,
57+
string $transactionId,
58+
array $options = []
59+
): CancellablePromiseInterface {
60+
$time = microtime(true) - $this->times[$transactionId];
61+
unset($this->times[$transactionId]);
5062

5163
return resolve($response->withAddedHeader(self::HEADER, (string)$time));
5264
}
5365

5466
/**
55-
* Priority ranging from 0 to 1000. Where 1000 will be executed first on `pre` and 0 last on `pre`.
56-
* For `post` the order is reversed.
57-
*
58-
* @return int
67+
* @param Throwable $throwable
68+
* @param string $transactionId
69+
* @param array $options
70+
* @return CancellablePromiseInterface
5971
*/
60-
public function priority(): int
61-
{
62-
return Priority::FIRST;
72+
public function error(
73+
Throwable $throwable,
74+
string $transactionId,
75+
array $options = []
76+
): CancellablePromiseInterface {
77+
unset($this->times[$transactionId]);
6378
}
6479
}

tests/MiddlewareTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ final class MiddlewareTest extends TestCase
1313
{
1414
public function testPost()
1515
{
16+
$idA = 'Abc';
17+
$idB = 'aBc';
1618
$response = new Response(200, []);
1719
$middleware = new Middleware();
18-
$middleware->pre(new Request('GET', 'https://example.com/'));
19-
$responseObject = await($middleware->post($response), Factory::create());
20+
$middleware->pre(new Request('GET', 'https://example.com/'), $idA, []);
21+
$middleware->pre(new Request('GET', 'https://example.com/'), $idB, []);
22+
$responseObject = await($middleware->post($response, $idA, []), Factory::create());
2023
self::assertTrue((float)$responseObject->getHeaderLine(Middleware::HEADER) < 1);
2124
sleep(1);
22-
$responseObject = await($middleware->post($response), Factory::create());
25+
$responseObject = await($middleware->post($response, $idB, []), Factory::create());
2326
self::assertTrue((float)$responseObject->getHeaderLine(Middleware::HEADER) > 1);
2427
}
2528
}

0 commit comments

Comments
 (0)