This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
Add a middleware dispatch #32
Merged
weierophinney
merged 3 commits into
zendframework:develop
from
ezimuel:feature/middleware
Oct 12, 2015
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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, $priority = 1) | ||
{ | ||
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch'], 1); | ||
} | ||
|
||
/** | ||
* 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?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(); | ||
}); | ||
|
||
$return = $this->application->run(); | ||
$response = $return->getResponse(); | ||
|
||
$this->assertEmpty($log); | ||
$this->assertInstanceOf('Zend\Http\Response', $response); | ||
$this->assertSame(200, $response->getStatusCode()); | ||
$this->assertEquals('Test!', $response->getBody()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to use statement, I created PR to your branch for it ezimuel#1