Skip to content

Commit 7d2317f

Browse files
committed
Both Server and StreamingServer accept middleware arrays
1 parent 579d6c3 commit 7d2317f

File tree

5 files changed

+111
-11
lines changed

5 files changed

+111
-11
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,8 @@ passed explicitly.
675675

676676
### Middleware
677677

678-
As documented above, the [`StreamingServer`](#streamingserver) accepts a single
678+
As documented above, the [`Server`](#server) and advanced
679+
[`StreamingServer`](#streamingserver) accept a single
679680
request handler argument that is responsible for processing an incoming
680681
HTTP request and then creating and returning an outgoing HTTP response.
681682

@@ -704,12 +705,13 @@ required to match PHP's request behavior (see below) and otherwise actively
704705
encourages [Third-Party Middleware](#third-party-middleware) implementations.
705706

706707
In order to use middleware request handlers, simply pass an array with all
707-
callables as defined above to the [`StreamingServer`](#streamingserver).
708+
callables as defined above to the [`Server`](#server) or
709+
[`StreamingServer`](#streamingserver) respectively.
708710
The following example adds a middleware request handler that adds the current time to the request as a
709711
header (`Request-Time`) and a final request handler that always returns a 200 code without a body:
710712

711713
```php
712-
$server = new StreamingServer(array(
714+
$server = new Server(array(
713715
function (ServerRequestInterface $request, callable $next) {
714716
$request = $request->withHeader('Request-Time', time());
715717
return $next($request);
@@ -740,7 +742,7 @@ The following example shows how this middleware can be used to ensure no more
740742
than 10 handlers will be invoked at once:
741743

742744
```php
743-
$server = new StreamingServer(array(
745+
$server = new Server(array(
744746
new LimitConcurrentRequestsMiddleware(10),
745747
$handler
746748
));

src/Server.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ final class Server extends EventEmitter
4848
/**
4949
* @see StreamingServer::__construct()
5050
*/
51-
public function __construct($callback)
51+
public function __construct($requestHandler)
5252
{
53-
if (!is_callable($callback)) {
54-
throw new \InvalidArgumentException();
53+
if (!is_callable($requestHandler) && !is_array($requestHandler)) {
54+
throw new \InvalidArgumentException('Invalid request handler given');
5555
}
5656

5757
$middleware = array();
@@ -66,7 +66,14 @@ public function __construct($callback)
6666
if ($enablePostDataReading !== '') {
6767
$middleware[] = new RequestBodyParserMiddleware();
6868
}
69-
$middleware[] = $callback;
69+
70+
if (is_callable($requestHandler)) {
71+
$middleware[] = $requestHandler;
72+
} else {
73+
foreach ($requestHandler as $one) {
74+
$middleware[] = $one;
75+
}
76+
}
7077

7178
$this->streamingServer = new StreamingServer($middleware);
7279

@@ -84,6 +91,10 @@ public function listen(ServerInterface $server)
8491
$this->streamingServer->listen($server);
8592
}
8693

94+
/**
95+
* @return int
96+
* @codeCoverageIgnore
97+
*/
8798
private function getConcurrentRequestsLimit()
8899
{
89100
if (ini_get('memory_limit') == -1) {

src/StreamingServer.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ final class StreamingServer extends EventEmitter
9999
*/
100100
public function __construct($requestHandler)
101101
{
102-
if (is_array($requestHandler)) {
103-
$requestHandler = new MiddlewareRunner($requestHandler);
102+
if (!is_callable($requestHandler) && !is_array($requestHandler)) {
103+
throw new \InvalidArgumentException('Invalid request handler given');
104104
} elseif (!is_callable($requestHandler)) {
105-
throw new \InvalidArgumentException();
105+
$requestHandler = new MiddlewareRunner($requestHandler);
106106
}
107107

108108
$this->callback = $requestHandler;

tests/ServerTest.php

+65
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,71 @@ public function setUp()
4141
$this->socket = new SocketServerStub();
4242
}
4343

44+
/**
45+
* @expectedException InvalidArgumentException
46+
*/
47+
public function testInvalidCallbackFunctionLeadsToException()
48+
{
49+
new Server('invalid');
50+
}
51+
52+
public function testSimpleRequestCallsRequestHandlerOnce()
53+
{
54+
$called = null;
55+
$server = new Server(function (ServerRequestInterface $request) use (&$called) {
56+
++$called;
57+
});
58+
59+
$server->listen($this->socket);
60+
$this->socket->emit('connection', array($this->connection));
61+
$this->connection->emit('data', array("GET / HTTP/1.0\r\n\r\n"));
62+
63+
$this->assertSame(1, $called);
64+
}
65+
66+
/**
67+
* @requires PHP 5.4
68+
*/
69+
public function testSimpleRequestCallsArrayRequestHandlerOnce()
70+
{
71+
$this->called = null;
72+
$server = new Server(array($this, 'helperCallableOnce'));
73+
74+
$server->listen($this->socket);
75+
$this->socket->emit('connection', array($this->connection));
76+
$this->connection->emit('data', array("GET / HTTP/1.0\r\n\r\n"));
77+
78+
$this->assertSame(1, $this->called);
79+
}
80+
81+
public function helperCallableOnce()
82+
{
83+
++$this->called;
84+
}
85+
86+
public function testSimpleRequestWithMiddlewareArrayProcessesMiddlewareStack()
87+
{
88+
$called = null;
89+
$server = new Server(array(
90+
function (ServerRequestInterface $request, $next) use (&$called) {
91+
$called = 'before';
92+
$ret = $next($request->withHeader('Demo', 'ok'));
93+
$called .= 'after';
94+
95+
return $ret;
96+
},
97+
function (ServerRequestInterface $request) use (&$called) {
98+
$called .= $request->getHeaderLine('Demo');
99+
}
100+
));
101+
102+
$server->listen($this->socket);
103+
$this->socket->emit('connection', array($this->connection));
104+
$this->connection->emit('data', array("GET / HTTP/1.0\r\n\r\n"));
105+
106+
$this->assertSame('beforeokafter', $called);
107+
}
108+
44109
public function testPostFileUpload()
45110
{
46111
$loop = Factory::create();

tests/StreamingServerTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ public function testRequestEventIsEmitted()
6262
$this->connection->emit('data', array($data));
6363
}
6464

65+
/**
66+
* @requires PHP 5.4
67+
*/
68+
public function testRequestEventIsEmittedForArrayCallable()
69+
{
70+
$this->called = null;
71+
$server = new StreamingServer(array($this, 'helperCallableOnce'));
72+
73+
$server->listen($this->socket);
74+
$this->socket->emit('connection', array($this->connection));
75+
76+
$data = $this->createGetRequest();
77+
$this->connection->emit('data', array($data));
78+
79+
$this->assertEquals(1, $this->called);
80+
}
81+
82+
public function helperCallableOnce()
83+
{
84+
++$this->called;
85+
}
86+
6587
public function testRequestEvent()
6688
{
6789
$i = 0;

0 commit comments

Comments
 (0)