diff --git a/composer.json b/composer.json index 4ad948e..4d1dc8a 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,10 @@ "autoload": { "psr-4": { "ApiClients\\Foundation\\Middleware\\": "src/" - } + }, + "files": [ + "src/bootstrap.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/src/Annotation/Early.php b/src/Annotation/Early.php new file mode 100644 index 0000000..f6def53 --- /dev/null +++ b/src/Annotation/Early.php @@ -0,0 +1,20 @@ +annotationReader->getMethodAnnotation($methodReflection, PriorityAnnotation::class); + $annotations = $this->annotationReader->getMethodAnnotations($methodReflection); + + foreach ($annotations as $annotation) { + if (!is_subclass_of($annotation, PriorityInterface::class)) { + continue; + } - if ($annotation !== null && - get_class($annotation) === PriorityAnnotation::class - ) { return $annotation->priority(); } diff --git a/src/bootstrap.php b/src/bootstrap.php new file mode 100644 index 0000000..cdec6d0 --- /dev/null +++ b/src/bootstrap.php @@ -0,0 +1,7 @@ +getCalls(), $middlewareTwo->getCalls()); + $calls = array_merge_recursive( + $middlewareOne->getCalls(), + $middlewareTwo->getCalls(), + $middlewareThree->getCalls() + ); ksort($calls); self::assertSame([ + ThreeMiddleware::class . ':pre', TwoMiddleware::class . ':pre', OneMiddleware::class . ':pre', OneMiddleware::class . ':post', TwoMiddleware::class . ':post', + ThreeMiddleware::class . ':post', OneMiddleware::class . ':error', + ThreeMiddleware::class . ':error', TwoMiddleware::class . ':error', ], array_values($calls)); } diff --git a/tests/TestMiddlewares/ThreeMiddleware.php b/tests/TestMiddlewares/ThreeMiddleware.php new file mode 100644 index 0000000..932a6aa --- /dev/null +++ b/tests/TestMiddlewares/ThreeMiddleware.php @@ -0,0 +1,81 @@ +calls; + } + + /** + * @param RequestInterface $request + * @param array $options + * @return CancellablePromiseInterface + * @First() + */ + public function pre( + RequestInterface $request, + array $options = [], + string $transactionId = null + ): CancellablePromiseInterface { + usleep(100); + $this->calls[(string)microtime(true)] = __CLASS__ . ':pre'; + + return resolve($request); + } + + /** + * @param ResponseInterface $response + * @param array $options + * @return CancellablePromiseInterface + * @Late() + */ + public function post( + ResponseInterface $response, + array $options = [], + string $transactionId = null + ): CancellablePromiseInterface { + usleep(100); + $this->calls[(string)microtime(true)] = __CLASS__ . ':post'; + + return resolve($response); + } + + /** + * @param Throwable $throwable + * @param array $options + * @return CancellablePromiseInterface + * @Early() + */ + public function error( + Throwable $throwable, + array $options = [], + string $transactionId = null + ): CancellablePromiseInterface { + usleep(100); + $this->calls[(string)microtime(true)] = __CLASS__ . ':error'; + + return reject($throwable); + } +}