From bf1c1cb28095a2915a6f15142dc0fcfe02b0b9d8 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Tue, 2 Nov 2021 18:39:55 +0100 Subject: [PATCH] feat: implement interceptors in the runtime library Signed-off-by: Mark Sagi-Kazar --- lib/src/Interceptor.php | 23 +++++++++++++ lib/src/InterceptorChain.php | 33 +++++++++++++++++++ lib/src/Method.php | 20 +++++++++++ lib/tests/InterceptorChainTest.php | 53 ++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 lib/src/Interceptor.php create mode 100644 lib/src/InterceptorChain.php create mode 100644 lib/src/Method.php create mode 100644 lib/tests/InterceptorChainTest.php diff --git a/lib/src/Interceptor.php b/lib/src/Interceptor.php new file mode 100644 index 0000000..2d6cc0d --- /dev/null +++ b/lib/src/Interceptor.php @@ -0,0 +1,23 @@ +interceptors = $interceptors; + } + + /** + * {@inheritdoc} + */ + public function intercept(Method $method): Method + { + foreach ($this->interceptors as $interceptor) { + $method = $interceptor->intercept($method); + } + + return $method; + } +} diff --git a/lib/src/Method.php b/lib/src/Method.php new file mode 100644 index 0000000..cc24a5d --- /dev/null +++ b/lib/src/Method.php @@ -0,0 +1,20 @@ +interceptor1 = $this->prophesize(Interceptor::class); + $this->interceptor2 = $this->prophesize(Interceptor::class); + + $this->interceptor = new InterceptorChain($this->interceptor1->reveal(), $this->interceptor2->reveal()); + } + + /** + * @test + */ + public function it_intercepts_a_method(): void + { + $method1 = $this->prophesize(Method::class)->reveal(); + $method2 = $this->prophesize(Method::class)->reveal(); + + $this->interceptor1->intercept($method1)->willReturn($method2); + $this->interceptor2->intercept($method2)->willReturn($method2); + + self::assertSame($method2, $this->interceptor->intercept($method1)); + } +}