Skip to content

Commit 4371c40

Browse files
authored
Merge pull request #45 from relayphp/fix-next-not-callable
Fix next not callable
2 parents 1735422 + 1b1ffe3 commit 4371c40

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/Runner.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Psr\Http\Message\ResponseInterface;
1515
use Psr\Http\Server\MiddlewareInterface;
1616
use Psr\Http\Server\RequestHandlerInterface;
17+
use RuntimeException;
18+
use function is_callable;
1719

1820
/**
1921
*
@@ -36,11 +38,23 @@ public function handle(ServerRequestInterface $request) : ResponseInterface
3638
if ($middleware instanceof MiddlewareInterface) {
3739
return $middleware->process($request, $this);
3840
}
39-
41+
4042
if ($middleware instanceof RequestHandlerInterface) {
4143
return $middleware->handle($request);
4244
}
45+
46+
if (is_callable($middleware)) {
47+
return $middleware($request, $this);
48+
}
4349

44-
return $middleware($request, $this);
50+
throw new RuntimeException(
51+
"Invalid middleware queue entry: {$middleware}. Middleware must either be callable or implement " .
52+
MiddlewareInterface::class . '.'
53+
);
54+
}
55+
56+
public function __invoke(ServerRequestInterface $request) : ResponseInterface
57+
{
58+
return $this->handle($request);
4559
}
4660
}

tests/RelayTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
use ArrayObject;
55
use InvalidArgumentException;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
use RuntimeException;
69
use TypeError;
710
use Zend\Diactoros\ServerRequestFactory;
811
use Zend\Diactoros\Response;
@@ -60,7 +63,7 @@ public function testTraversableQueue()
6063
public function testBadQueue()
6164
{
6265
$this->expectException(TypeError::CLASS);
63-
$relay = new Relay('bad');
66+
new Relay('bad');
6467
}
6568

6669
public function testEmptyQueue()
@@ -71,6 +74,17 @@ public function testEmptyQueue()
7174
new Relay([]);
7275
}
7376

77+
public function testQueueWithInvalidEntry()
78+
{
79+
$this->expectException(RuntimeException::CLASS);
80+
$this->expectExceptionMessage(
81+
"Invalid middleware queue entry: bad. Middleware must either be callable or implement Psr\Http\Server\MiddlewareInterface."
82+
);
83+
84+
$relay = new Relay(['bad']);
85+
$relay->handle(ServerRequestFactory::fromGlobals());
86+
}
87+
7488
public function testResolverEntries()
7589
{
7690
$queue = [
@@ -84,6 +98,7 @@ public function testResolverEntries()
8498

8599
$this->assertRelay(new Relay($queue, $resolver));
86100
}
101+
87102

88103
public function testRequestHandlerInQueue()
89104
{
@@ -96,4 +111,26 @@ public function testRequestHandlerInQueue()
96111
$requestHandler = new Relay($queue);
97112
$this->assertRelay(new Relay([$requestHandler]));
98113
}
114+
115+
public function testCallableMiddleware()
116+
{
117+
$queue = [
118+
function (
119+
ServerRequestInterface $request,
120+
callable $next
121+
) : ResponseInterface {
122+
$response = $next($request);
123+
124+
$response->getBody()->write('Hello, callable world!');
125+
126+
return $response;
127+
},
128+
$this->responder
129+
];
130+
131+
$relay = new Relay($queue);
132+
$response = $relay->handle(ServerRequestFactory::fromGlobals());
133+
134+
$this->assertEquals('Hello, callable world!', (string) $response->getBody());
135+
}
99136
}

0 commit comments

Comments
 (0)