Skip to content

Commit 7823791

Browse files
authored
Merge pull request #285 from clue-labs/middleware-server
Both Server and StreamingServer accept middleware arrays
2 parents 0f0d293 + 7d2317f commit 7823791

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
@@ -787,7 +787,8 @@ passed explicitly.
787787

788788
### Middleware
789789

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

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

818819
In order to use middleware request handlers, simply pass an array with all
819-
callables as defined above to the [`StreamingServer`](#streamingserver).
820+
callables as defined above to the [`Server`](#server) or
821+
[`StreamingServer`](#streamingserver) respectively.
820822
The following example adds a middleware request handler that adds the current time to the request as a
821823
header (`Request-Time`) and a final request handler that always returns a 200 code without a body:
822824

823825
```php
824-
$server = new StreamingServer(array(
826+
$server = new Server(array(
825827
function (ServerRequestInterface $request, callable $next) {
826828
$request = $request->withHeader('Request-Time', time());
827829
return $next($request);
@@ -852,7 +854,7 @@ The following example shows how this middleware can be used to ensure no more
852854
than 10 handlers will be invoked at once:
853855

854856
```php
855-
$server = new StreamingServer(array(
857+
$server = new Server(array(
856858
new LimitConcurrentRequestsMiddleware(10),
857859
$handler
858860
));

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)