Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Fix failed returning Responsable from Middleware #24164

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Illuminate/Pipeline/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -96,7 +97,9 @@ public function via($method)
public function then(Closure $destination)
{
$pipeline = array_reduce(
array_reverse($this->pipes), $this->carry(), $this->prepareDestination($destination)
array_reverse($this->pipes),
$this->carry(),
$this->prepareDestination($destination)
);

return $pipeline($this->passable);
Expand Down Expand Up @@ -145,9 +148,13 @@ protected function carry()
$parameters = [$passable, $stack];
}

return method_exists($pipe, $this->method)
? $pipe->{$this->method}(...$parameters)
: $pipe(...$parameters);
$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;
};
};
}
Expand Down
39 changes: 37 additions & 2 deletions tests/Pipeline/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Contracts\Support\Responsable;

class PipelineTest extends TestCase
{
Expand All @@ -26,8 +27,7 @@ public function testPipelineBasicUsage()
$this->assertEquals('foo', $_SERVER['__test.pipe.one']);
$this->assertEquals('foo', $_SERVER['__test.pipe.two']);

unset($_SERVER['__test.pipe.one']);
unset($_SERVER['__test.pipe.two']);
unset($_SERVER['__test.pipe.one'], $_SERVER['__test.pipe.two']);
}

public function testPipelineUsageWithObjects()
Expand Down Expand Up @@ -62,6 +62,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) {
Expand Down Expand Up @@ -160,6 +177,14 @@ public function differentMethod($piped, $next)
}
}

class PipeResponsable implements Responsable
{
public function toResponse($request)
{
return 'bar';
}
}

class PipelineTestPipeTwo
{
public function __invoke($piped, $next)
Expand All @@ -170,6 +195,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)
Expand Down