forked from zendframework/zend-mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
239 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Mvc; | ||
|
||
use ArrayObject; | ||
use Zend\EventManager\AbstractListenerAggregate; | ||
use Zend\EventManager\EventManagerInterface; | ||
use Zend\Mvc\Exception\InvalidControllerException; | ||
use Zend\Stdlib\ArrayUtils; | ||
use Zend\Psr7Bridge\Psr7ServerRequest as Psr7Request; | ||
use Zend\Psr7Bridge\Psr7Response; | ||
|
||
class MiddlewareListener extends AbstractListenerAggregate | ||
{ | ||
/** | ||
* Attach listeners to an event manager | ||
* | ||
* @param EventManagerInterface $events | ||
* @return void | ||
*/ | ||
public function attach(EventManagerInterface $events) | ||
{ | ||
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch'], 1000); | ||
} | ||
|
||
/** | ||
* Listen to the "dispatch" event | ||
* | ||
* @param MvcEvent $e | ||
* @return mixed | ||
*/ | ||
public function onDispatch(MvcEvent $e) | ||
{ | ||
$routeMatch = $e->getRouteMatch(); | ||
$middleware = $routeMatch->getParam('middleware', false); | ||
if (false === $middleware) { | ||
return; | ||
} | ||
|
||
$request = $e->getRequest(); | ||
$application = $e->getApplication(); | ||
$response = $application->getResponse(); | ||
$serviceManager = $application->getServiceManager(); | ||
$middlewareName = is_string($middleware) ? $middleware : get_class($middleware); | ||
|
||
if (is_string($middleware) && $serviceManager->has($middleware)) { | ||
$middleware = $serviceManager->get($middleware); | ||
} | ||
if (!is_callable($middleware)) { | ||
$return = $this->marshalMiddlewareNotCallable($application::ERROR_MIDDLEWARE_CANNOT_DISPATCH, $middlewareName, $e, $application); | ||
$e->setResult($return); | ||
return $return; | ||
} | ||
try { | ||
$return = $middleware(Psr7Request::fromZend($request), Psr7Response::fromZend($response)); | ||
} catch (\Exception $ex) { | ||
$e->setError($application::ERROR_EXCEPTION) | ||
->setController($middlewareName) | ||
->setControllerClass(get_class($middleware)) | ||
->setParam('exception', $ex); | ||
$results = $events->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $e); | ||
$return = $results->last(); | ||
if (! $return) { | ||
$return = $e->getResult(); | ||
} | ||
} | ||
|
||
if (! $return instanceof \Psr\Http\Message\ResponseInterface) { | ||
$e->setResult($return); | ||
return $return; | ||
} | ||
$response = Psr7Response::toZend($return); | ||
$e->setResult($response); | ||
return $response; | ||
} | ||
|
||
/** | ||
* Marshal a middleware not callable exception event | ||
* | ||
* @param string $type | ||
* @param string $middlewareName | ||
* @param MvcEvent $event | ||
* @param Application $application | ||
* @param \Exception $exception | ||
* @return mixed | ||
*/ | ||
protected function marshalMiddlewareNotCallable( | ||
$type, | ||
$middlewareName, | ||
MvcEvent $event, | ||
Application $application, | ||
\Exception $exception = null | ||
) { | ||
$event->setError($type) | ||
->setController($middlewareName) | ||
->setControllerClass('Middleware not callable: ' . $middlewareName); | ||
if ($exception !== null) { | ||
$event->setParam('exception', $exception); | ||
} | ||
|
||
$events = $application->getEventManager(); | ||
$results = $events->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event); | ||
$return = $results->last(); | ||
if (! $return) { | ||
$return = $event->getResult(); | ||
} | ||
return $return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Mvc; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Zend\Mvc\Application; | ||
use Zend\Mvc\MvcEvent; | ||
use Zend\Mvc\Router; | ||
use Zend\Mvc\Service\ServiceManagerConfig; | ||
use Zend\Mvc\Service\ServiceListenerFactory; | ||
use Zend\ServiceManager\ServiceManager; | ||
use Zend\Stdlib\ArrayUtils; | ||
|
||
class MiddlewareListenerTest extends TestCase | ||
{ | ||
/** | ||
* @var ServiceManager | ||
*/ | ||
protected $serviceManager; | ||
|
||
/** | ||
* @var Application | ||
*/ | ||
protected $application; | ||
|
||
public function setUp() | ||
{ | ||
$serviceConfig = ArrayUtils::merge( | ||
$this->readAttribute(new ServiceListenerFactory, 'defaultServiceConfig'), | ||
[ | ||
'allow_override' => true, | ||
'invokables' => [ | ||
'Request' => 'Zend\Http\PhpEnvironment\Request', | ||
'Response' => 'Zend\Http\PhpEnvironment\Response', | ||
'ViewManager' => 'ZendTest\Mvc\TestAsset\MockViewManager', | ||
'SendResponseListener' => 'ZendTest\Mvc\TestAsset\MockSendResponseListener', | ||
'BootstrapListener' => 'ZendTest\Mvc\TestAsset\StubBootstrapListener', | ||
], | ||
'aliases' => [ | ||
'Router' => 'HttpRouter', | ||
], | ||
'services' => [ | ||
'Config' => [], | ||
'ApplicationConfig' => [ | ||
'modules' => [], | ||
'module_listener_options' => [ | ||
'config_cache_enabled' => false, | ||
'cache_dir' => 'data/cache', | ||
'module_paths' => [], | ||
], | ||
], | ||
], | ||
] | ||
); | ||
$this->serviceManager = new ServiceManager(new ServiceManagerConfig($serviceConfig)); | ||
$this->application = $this->serviceManager->get('Application'); | ||
} | ||
|
||
public function setupPathMiddleware() | ||
{ | ||
$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(); | ||
} | ||
|
||
|
||
public function testMiddlewareDispatch() | ||
{ | ||
$this->setupPathMiddleware(); | ||
|
||
$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()); | ||
} | ||
} |