diff --git a/src/Illuminate/Pipeline/Pipeline.php b/src/Illuminate/Pipeline/Pipeline.php index e227a583bef8..630948bdf506 100644 --- a/src/Illuminate/Pipeline/Pipeline.php +++ b/src/Illuminate/Pipeline/Pipeline.php @@ -4,7 +4,9 @@ use Closure; use RuntimeException; +use Illuminate\Http\Request; use Illuminate\Contracts\Container\Container; +use Illuminate\Contracts\Support\Responsable; use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract; class Pipeline implements PipelineContract @@ -145,9 +147,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(Request::class)) + : $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)