Skip to content

Commit

Permalink
The request dispatcher now broadcasts the request to a collection of …
Browse files Browse the repository at this point in the history
…applications.
  • Loading branch information
phuedx committed Nov 27, 2011
1 parent 60d3a0e commit 4da9f80
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
28 changes: 21 additions & 7 deletions src/Seraph/Request/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class Seraph_Request_Dispatcher
{
const SERVER_HEADER_NAME = 'X-M2-Server';

protected $application;
protected $applications;
protected $request;
protected $response;

public function __construct(Seraph_Application_Interface $application, Seraph_Request $request = null, Seraph_Response $response = null) {
public function __construct(Seraph_Request $request = null, Seraph_Response $response = null) {
if ( ! $request) {
$request = new Seraph_Request();
}
Expand All @@ -26,11 +26,21 @@ public function __construct(Seraph_Application_Interface $application, Seraph_Re
$response = new Seraph_Response();
}

$this->application = $application;
$this->request = $request;
$this->response = $response;
$this->applications = array();
$this->request = $request;
$this->response = $response;
}

public function registerApplication(Seraph_Application_Interface $application) {
if ( ! in_array($application, $this->applications)) {
$this->applications[] = $application;
}

return $this;
}

// public function deregisterApplication(Seraph_Application_Interface $application) { }

public function onRawRequest(ZMQSocket $inboundSocket, ZMQSocket $outboundSocket, $server) {
$request = $this->request;
$rawRequest = $inboundSocket->recv();
Expand All @@ -39,10 +49,14 @@ public function onRawRequest(ZMQSocket $inboundSocket, ZMQSocket $outboundSocket
$request->fromRawRequest($rawRequest)
->setHeader(self::SERVER_HEADER_NAME, $server);

$this->response->fromRequest($request);
$response->fromRequest($request);

$this->application->onRequest($request, $response); // It's dispatchin' time!
foreach ($this->applications as $application) {
$application->onRequest($request, $response); // It's dispatchin' time!
}

$outboundSocket->send($response);

return $this;
}
}
7 changes: 4 additions & 3 deletions tests/Seraph/Request/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public function setUp() {
$this->mockRequest = $this->getMock('Seraph_Request');
$this->mockResponse = $this->getMock('Seraph_Response');

$this->dispatcher = new Seraph_Request_Dispatcher($this->mockApplication, $this->mockRequest, $this->mockResponse);
$this->dispatcher = new Seraph_Request_Dispatcher($this->mockRequest, $this->mockResponse);
}

public function testOnRawRequest() {
$context = new ZMQContext();
$rawRequest = 'RAW REQUEST';
$server = 'SERVER';

$mockInboundSocket = $this->getMockBuilder('ZMQSocket')
->setConstructorArgs(array($context, ZMQ::SOCKET_PULL))
->getMock();
Expand Down Expand Up @@ -63,6 +63,7 @@ public function testOnRawRequest() {
->method('send')
->with($this->mockResponse);

$this->dispatcher->onRawRequest($mockInboundSocket, $mockOutboundSocket, $server);
$this->dispatcher->registerApplication($this->mockApplication)
->onRawRequest($mockInboundSocket, $mockOutboundSocket, $server);
}
}
11 changes: 5 additions & 6 deletions tests/handler_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@

$signals['seraph.handler.raw_request'] = $signal;

// Dispatcher
class HandlerTest implements Seraph_Application_Interface
// App
class HelloWorldApp implements Seraph_Application_Interface
{
public function onRequest(Seraph_Request $request, Seraph_Response $response) {
var_dump($request);

$response->setBody('Hello, World!');
}
}

$app = new HandlerTest();
$dispatcher = new Seraph_Request_Dispatcher($app);
$app = new HelloWorldApp();
$dispatcher = new Seraph_Request_Dispatcher();
$dispatcher->registerApplication($app);

$signal->connect(array($dispatcher, 'onRawRequest'));

Expand Down

0 comments on commit 4da9f80

Please sign in to comment.