From d36bafdddc3f1300ed0672be1386d0e7723607d3 Mon Sep 17 00:00:00 2001 From: Tetiana Blindaruk Date: Mon, 14 May 2018 01:23:57 +0300 Subject: [PATCH] Fix: Failed returning Responsable from Middleware - This is a fix for issue #24156 which states that. When we return Implementation of Responsable from middleware it fails. --- src/Illuminate/Pipeline/Pipeline.php | 7 +++++- tests/Pipeline/PipelineTest.php | 36 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Pipeline/Pipeline.php b/src/Illuminate/Pipeline/Pipeline.php index e227a583bef8..fc39023f05c6 100644 --- a/src/Illuminate/Pipeline/Pipeline.php +++ b/src/Illuminate/Pipeline/Pipeline.php @@ -5,6 +5,7 @@ use Closure; use RuntimeException; use Illuminate\Contracts\Container\Container; +use Illuminate\Contracts\Support\Responsable; use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract; class Pipeline implements PipelineContract @@ -145,9 +146,13 @@ protected function carry() $parameters = [$passable, $stack]; } - return method_exists($pipe, $this->method) + $response = method_exists($pipe, $this->method) ? $pipe->{$this->method}(...$parameters) : $pipe(...$parameters); + + return $response instanceof Responsable + ? $response->toResponse($this->container->make('Illuminate\Http\Request')) + : $response; }; }; } diff --git a/tests/Pipeline/PipelineTest.php b/tests/Pipeline/PipelineTest.php index 070171094a1a..f89c2a8ebfdf 100644 --- a/tests/Pipeline/PipelineTest.php +++ b/tests/Pipeline/PipelineTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use Illuminate\Pipeline\Pipeline; +use Illuminate\Contracts\Support\Responsable; class PipelineTest extends TestCase { @@ -62,6 +63,23 @@ function ($piped) { unset($_SERVER['__test.pipe.one']); } + public function testPipelineUsageWithResponsableObjects() + { + $result = (new Pipeline(new \Illuminate\Container\Container)) + ->send('foo') + ->through([new PipelineTestPipeResponsable]) + ->then( + function ($piped) { + return $piped; + } + ); + + $this->assertEquals('bar', $result); + $this->assertEquals('foo', $_SERVER['__test.pipe.responsable']); + + unset($_SERVER['__test.pipe.responsable']); + } + public function testPipelineUsageWithCallable() { $function = function ($piped, $next) { @@ -160,6 +178,14 @@ public function differentMethod($piped, $next) } } +class PipeResponsable implements Responsable +{ + public function toResponse($request) + { + return 'bar'; + } +} + class PipelineTestPipeTwo { public function __invoke($piped, $next) @@ -170,6 +196,16 @@ public function __invoke($piped, $next) } } +class PipelineTestPipeResponsable +{ + public function handle($piped, $next) + { + $_SERVER['__test.pipe.responsable'] = $piped; + + return new PipeResponsable; + } +} + class PipelineTestParameterPipe { public function handle($piped, $next, $parameter1 = null, $parameter2 = null)