From 9258b84613b67d52e27dde7f103b17cca39e7d77 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Thu, 29 Jun 2017 22:59:36 +0200 Subject: [PATCH] Middleware 4.0 --- composer.json | 2 +- composer.lock | 182 +++++++++++++++++++++++++++-- src/AcceptJsonMiddleware.php | 8 +- src/JsonDecodeMiddleware.php | 18 ++- src/JsonEncodeMiddleware.php | 18 ++- tests/AcceptJsonMiddlewareTest.php | 2 +- tests/JsonDecodeMiddlewareTest.php | 12 +- tests/JsonEncodeMiddlewareTest.php | 12 +- 8 files changed, 198 insertions(+), 56 deletions(-) diff --git a/composer.json b/composer.json index 630889c..5d506d1 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "require": { "php": "^7.0", "api-clients/json": "^1.0", - "api-clients/middleware": "^2.0", + "api-clients/middleware": "^4.0", "clue/buzz-react": "^1.1", "psr/http-message": "^1.0", "ringcentral/psr7": "^1.2", diff --git a/composer.lock b/composer.lock index 0c6f45a..5f4b9a4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "7db0eccf7ecc306845f8831c89959986", + "content-hash": "ed6c33b92f90f81f72fc63655be494a2", "packages": [ { "name": "api-clients/json", @@ -48,34 +48,36 @@ }, { "name": "api-clients/middleware", - "version": "2.0.1", + "version": "4.0.0", "source": { "type": "git", "url": "https://github.com/php-api-clients/middleware.git", - "reference": "e25852d9d4d3092cc657b7660a413f6e73ea8c4d" + "reference": "328f8e17bbf5a51107de0d1b5987c04757f66499" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-api-clients/middleware/zipball/e25852d9d4d3092cc657b7660a413f6e73ea8c4d", - "reference": "e25852d9d4d3092cc657b7660a413f6e73ea8c4d", + "url": "https://api.github.com/repos/php-api-clients/middleware/zipball/328f8e17bbf5a51107de0d1b5987c04757f66499", + "reference": "328f8e17bbf5a51107de0d1b5987c04757f66499", "shasum": "" }, "require": { + "doctrine/annotations": "^1.4", "php": "^7.0", "psr/http-message": "^1.0", - "react/promise": "^2.4" + "react/promise": "^2.4", + "wyrihaximus/doctrine-annotation-autoloader": "^1.0" }, "require-dev": { - "api-clients/test-utilities": "^3.0", - "container-interop/container-interop": "^1.1", - "guzzlehttp/psr7": "^1.3" + "api-clients/test-utilities": "^4.1", + "guzzlehttp/psr7": "^1.3", + "psr/container": "^1.0" }, "suggest": { "api-clients/middleware-cache": "Cache requests with different strategies", "api-clients/middleware-log": "Log requests and their context", "api-clients/middleware-oauth1": "Sign requests with oauth1", "api-clients/middleware-pool": "Pool the maximum concurrent requests", - "container-interop/container-interop": "Add a ContainerInterface implementation to use the ContainerLocator" + "psr/container": "Add a ContainerInterface implementation to use the ContainerLocator" }, "type": "library", "autoload": { @@ -94,7 +96,7 @@ } ], "description": "Request middleware", - "time": "2017-04-08T07:00:13+00:00" + "time": "2017-06-28T19:24:19+00:00" }, { "name": "clue/buzz-react", @@ -186,6 +188,128 @@ "description": "JSON encoding and decoding that throws exceptions on failure", "time": "2016-05-04T15:52:54+00:00" }, + { + "name": "doctrine/annotations", + "version": "v1.4.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/annotations.git", + "reference": "54cacc9b81758b14e3ce750f205a393d52339e97" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/annotations/zipball/54cacc9b81758b14e3ce750f205a393d52339e97", + "reference": "54cacc9b81758b14e3ce750f205a393d52339e97", + "shasum": "" + }, + "require": { + "doctrine/lexer": "1.*", + "php": "^5.6 || ^7.0" + }, + "require-dev": { + "doctrine/cache": "1.*", + "phpunit/phpunit": "^5.7" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Common\\Annotations\\": "lib/Doctrine/Common/Annotations" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Docblock Annotations Parser", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "annotations", + "docblock", + "parser" + ], + "time": "2017-02-24T16:22:25+00:00" + }, + { + "name": "doctrine/lexer", + "version": "v1.0.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/lexer.git", + "reference": "83893c552fd2045dd78aef794c31e694c37c0b8c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/lexer/zipball/83893c552fd2045dd78aef794c31e694c37c0b8c", + "reference": "83893c552fd2045dd78aef794c31e694c37c0b8c", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\Lexer\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "lexer", + "parser" + ], + "time": "2014-09-09T13:34:57+00:00" + }, { "name": "evenement/evenement", "version": "v2.0.0", @@ -797,6 +921,42 @@ ], "time": "2016-03-25T17:36:49+00:00" }, + { + "name": "wyrihaximus/doctrine-annotation-autoloader", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/WyriHaximus/php-doctrine-annotation-autoloader.git", + "reference": "9670f84b7d3ca53723556988080ebc939da345af" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/WyriHaximus/php-doctrine-annotation-autoloader/zipball/9670f84b7d3ca53723556988080ebc939da345af", + "reference": "9670f84b7d3ca53723556988080ebc939da345af", + "shasum": "" + }, + "require": { + "doctrine/annotations": "^1.4" + }, + "type": "library", + "autoload": { + "files": [ + "src/bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Cees-Jan Kiewiet", + "email": "ceesjank@gmail.com" + } + ], + "description": "Request middleware", + "time": "2017-06-19T15:02:15+00:00" + }, { "name": "wyrihaximus/ticking-promise", "version": "1.6.1", diff --git a/src/AcceptJsonMiddleware.php b/src/AcceptJsonMiddleware.php index c194c58..0f286ef 100644 --- a/src/AcceptJsonMiddleware.php +++ b/src/AcceptJsonMiddleware.php @@ -14,15 +14,17 @@ class AcceptJsonMiddleware implements MiddlewareInterface { use PostTrait; use ErrorTrait; - use DefaultPriorityTrait; /** * @param RequestInterface $request * @param array $options * @return CancellablePromiseInterface */ - public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface - { + public function pre( + RequestInterface $request, + string $transactionId, + array $options = [] + ): CancellablePromiseInterface { return resolve($request->withAddedHeader('Accept', 'application/json')); } } diff --git a/src/JsonDecodeMiddleware.php b/src/JsonDecodeMiddleware.php index 2dd8742..77d5428 100644 --- a/src/JsonDecodeMiddleware.php +++ b/src/JsonDecodeMiddleware.php @@ -2,6 +2,7 @@ namespace ApiClients\Middleware\Json; +use ApiClients\Foundation\Middleware\Annotation\First; use ApiClients\Foundation\Middleware\ErrorTrait; use ApiClients\Foundation\Middleware\MiddlewareInterface; use ApiClients\Foundation\Middleware\PreTrait; @@ -31,21 +32,18 @@ public function __construct(JsonDecodeService $jsonDecodeService) $this->jsonDecodeService = $jsonDecodeService; } - /** - * @return int - */ - public function priority(): int - { - return Priority::FIRST; - } - /** * @param ResponseInterface $response * @param array $options * @return CancellablePromiseInterface + * + * @First() */ - public function post(ResponseInterface $response, array $options = []): CancellablePromiseInterface - { + public function post( + ResponseInterface $response, + string $transactionId, + array $options = [] + ): CancellablePromiseInterface { if ($response->getBody() instanceof ReadableStreamInterface) { return resolve($response); } diff --git a/src/JsonEncodeMiddleware.php b/src/JsonEncodeMiddleware.php index 161acac..223c5c1 100644 --- a/src/JsonEncodeMiddleware.php +++ b/src/JsonEncodeMiddleware.php @@ -2,6 +2,7 @@ namespace ApiClients\Middleware\Json; +use ApiClients\Foundation\Middleware\Annotation\First; use ApiClients\Foundation\Middleware\ErrorTrait; use ApiClients\Foundation\Middleware\MiddlewareInterface; use ApiClients\Foundation\Middleware\PostTrait; @@ -30,21 +31,18 @@ public function __construct(JsonEncodeService $jsonEncodeService) $this->jsonEncodeService = $jsonEncodeService; } - /** - * @return int - */ - public function priority(): int - { - return Priority::FIRST; - } - /** * @param RequestInterface $request * @param array $options * @return CancellablePromiseInterface + * + * @First() */ - public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface - { + public function pre( + RequestInterface $request, + string $transactionId, + array $options = [] + ): CancellablePromiseInterface { if (!($request->getBody() instanceof JsonStream)) { return resolve($request); } diff --git a/tests/AcceptJsonMiddlewareTest.php b/tests/AcceptJsonMiddlewareTest.php index e8c7be5..9bf7a05 100644 --- a/tests/AcceptJsonMiddlewareTest.php +++ b/tests/AcceptJsonMiddlewareTest.php @@ -15,7 +15,7 @@ public function testPre() $middleware = new AcceptJsonMiddleware(); $request = new Request('GET', 'https://example.com', [], ''); - $modifiedRequest = await($middleware->pre($request), Factory::create()); + $modifiedRequest = await($middleware->pre($request, 'abc'), Factory::create()); self::assertSame( [ 'Host' => ['example.com'], diff --git a/tests/JsonDecodeMiddlewareTest.php b/tests/JsonDecodeMiddlewareTest.php index 8ee733d..6ca2af8 100644 --- a/tests/JsonDecodeMiddlewareTest.php +++ b/tests/JsonDecodeMiddlewareTest.php @@ -22,7 +22,7 @@ public function testPost() $response = new Response(200, [], '[]'); $body = await( - $middleware->post($response), + $middleware->post($response, 'abc'), $loop )->getBody(); @@ -44,17 +44,9 @@ public function testPostNoJson() self::assertSame( $response, await( - $middleware->post($response), + $middleware->post($response, 'abc'), $loop ) ); } - - public function testPriority() - { - $loop = Factory::create(); - $service = new JsonDecodeService($loop); - $middleware = new JsonDecodeMiddleware($service); - self::assertSame(1000, $middleware->priority()); - } } diff --git a/tests/JsonEncodeMiddlewareTest.php b/tests/JsonEncodeMiddlewareTest.php index 35b312b..0b1490b 100644 --- a/tests/JsonEncodeMiddlewareTest.php +++ b/tests/JsonEncodeMiddlewareTest.php @@ -21,7 +21,7 @@ public function testPre() $stream = new JsonStream([]); $request = new Request('GET', 'https://example.com', [], $stream); - $modifiedRequest = await($middleware->pre($request), $loop); + $modifiedRequest = await($middleware->pre($request, 'abc'), $loop); self::assertSame( '[]', (string) $modifiedRequest->getBody() @@ -42,17 +42,9 @@ public function testPreNoJson() self::assertSame( $request, await( - $middleware->pre($request), + $middleware->pre($request, 'abc'), $loop ) ); } - - public function testPriority() - { - $loop = Factory::create(); - $service = new JsonEncodeService($loop); - $middleware = new JsonEncodeMiddleware($service); - self::assertSame(1000, $middleware->priority()); - } }