Skip to content

Commit

Permalink
Added a first middleware support
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Oct 1, 2015
1 parent 6775182 commit fe65cba
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"zendframework/zend-servicemanager": "~2.5",
"zendframework/zend-hydrator": "~1.0",
"zendframework/zend-form": "~2.6",
"zendframework/zend-stdlib": "~2.7"
"zendframework/zend-stdlib": "~2.7",
"zendframework/zend-psr7bridge": "^0.2"
},
"require-dev": {
"zendframework/zend-authentication": "~2.5",
Expand Down
3 changes: 2 additions & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class Application implements
const ERROR_CONTROLLER_INVALID = 'error-controller-invalid';
const ERROR_EXCEPTION = 'error-exception';
const ERROR_ROUTER_NO_MATCH = 'error-router-no-match';

const ERROR_MIDDLEWARE_CANNOT_DISPATCH = 'error-middleware-cannot-dispatch';

/**
* @var array
*/
Expand Down
30 changes: 24 additions & 6 deletions src/DispatchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Exception\InvalidControllerException;
use Zend\Stdlib\ArrayUtils;
use Zend\Psr7Bridge\Psr7ServerRequest as Psr7Request;
use Zend\Psr7Bridge\Psr7Response;

/**
* Default dispatch listener
Expand Down Expand Up @@ -61,11 +63,30 @@ public function attach(EventManagerInterface $events)
*/
public function onDispatch(MvcEvent $e)
{
$routeMatch = $e->getRouteMatch();
$routeMatch = $e->getRouteMatch();
$request = $e->getRequest();
$application = $e->getApplication();
$response = $application->getResponse();
$serviceManager = $application->getServiceManager();

// middleware?
$middleware = $routeMatch->getParam('middleware', false);
if (false !== $middleware) {
if (is_string($middleware) && $serviceManager->has($middleware)) {
$middleware = $serviceManager->get($middleware);
}
if (!is_callable($middleware)) {
$middleware = is_string($middleware) ? $middleware : get_class($middleware);
$return = $this->marshalControllerNotFoundEvent($application::ERROR_MIDDLEWARE_CANNOT_DISPATCH, $middleware, $e, $application);
return $this->complete($return, $e);
}
$return = $middleware(Psr7Request::fromZend($request), Psr7Response::fromZend($response));
return $this->complete(Psr7Response::toZend($return), $e);
}

$controllerName = $routeMatch->getParam('controller', 'not-found');
$application = $e->getApplication();
$events = $application->getEventManager();
$controllerLoader = $application->getServiceManager()->get('ControllerManager');
$controllerLoader = $serviceManager->get('ControllerManager');

if (!$controllerLoader->has($controllerName)) {
$return = $this->marshalControllerNotFoundEvent($application::ERROR_CONTROLLER_NOT_FOUND, $controllerName, $e, $application);
Expand All @@ -82,9 +103,6 @@ public function onDispatch(MvcEvent $e)
return $this->complete($return, $e);
}

$request = $e->getRequest();
$response = $application->getResponse();

if ($controller instanceof InjectApplicationEventInterface) {
$controller->setEvent($e);
}
Expand Down
40 changes: 40 additions & 0 deletions test/DispatchListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,44 @@ public function testUnlocatableControllerLoaderComposedOfAbstractFactory()
$this->assertArrayHasKey('error', $log);
$this->assertSame('error-controller-not-found', $log['error']);
}

public function testMiddlewareDispatch()
{
$request = $this->serviceManager->get('Request');
$request->setUri('http://example.local/path');

$router = $this->serviceManager->get('HttpRouter');
$route = Router\Http\Literal::factory([
'route' => '/path',
'defaults' => [
'middleware' => function($request, $response) {
$this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $request);
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
$response->getBody()->write('Test!');
return $response;
}
],
]);
$router->addRoute('path', $route);
$this->application->bootstrap();

$controllerLoader = $this->serviceManager->get('ControllerLoader');
$controllerLoader->addAbstractFactory('ZendTest\Mvc\Controller\TestAsset\ControllerLoaderAbstractFactory');

$log = [];
$this->application->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) use (&$log) {
$log['error'] = $e->getError();
});

$this->application->run();

$event = $this->application->getMvcEvent();
$dispatchListener = $this->serviceManager->get('DispatchListener');
$return = $dispatchListener->onDispatch($event);

$this->assertEmpty($log);
$this->assertInstanceOf('Zend\Http\Response', $return);
$this->assertSame(200, $return->getStatusCode());
$this->assertEquals('Test!', $return->getBody());
}
}

0 comments on commit fe65cba

Please sign in to comment.