- Notifications
You must be signed in to change notification settings - Fork90
Modify middleware listener to use stratigility pipe#217
Uh oh!
There was an error while loading.Please reload this page.
Changes from15 commits
2c64d2e2712a99cab17a224201cbab6363981c5f2e6f9c2717e89d6acebe041d0debca6073ca2b05cd8b7d899004c393f103ffda041168034dbdb4b2a4a6447e5c56000bdd8cbf741df6d090196a4bf9fFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
| /** | ||
| * Zend Framework (http://framework.zend.com/) | ||
| * | ||
| * @link http://github.com/zendframework/zf2 for the canonical source repository | ||
| * @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com) | ||
| * @license http://framework.zend.com/license/new-bsd New BSD License | ||
| */ | ||
| namespaceZend\Mvc\Exception; | ||
| finalclass MiddlewareNotCallableExceptionextends RuntimeException | ||
| ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| private$middlewareName; | ||
| /** | ||
| * @param string $middlewareName | ||
| * @return self | ||
| */ | ||
| publicstaticfunctionfromMiddlewareName($middlewareName) | ||
| { | ||
| $middlewareName = (string)$middlewareName; | ||
| $instance =newself(sprintf('Cannot dispatch middleware %s',$middlewareName)); | ||
| $instance->middlewareName =$middlewareName; | ||
| return$instance; | ||
| } | ||
| /** | ||
| * @return string | ||
| */ | ||
| publicfunctiontoMiddlewareName() | ||
| { | ||
| returnnull !==$this->middlewareName ?$this->middlewareName :''; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
| /** | ||
| * Zend Framework (http://framework.zend.com/) | ||
| * | ||
| * @link http://github.com/zendframework/zf2 for the canonical source repository | ||
| * @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com) | ||
| * @license http://framework.zend.com/license/new-bsd New BSD License | ||
| */ | ||
| namespace Zend\Mvc\Exception; | ||
| class ReachedFinalHandlerException extends RuntimeException | ||
| { | ||
| /** | ||
| * @return self | ||
| */ | ||
| public static function create() | ||
| { | ||
| return new self('Reached the final handler for middleware pipe - check the pipe configuration'); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -9,12 +9,19 @@ | ||
| namespace Zend\Mvc; | ||
| use Interop\Container\ContainerInterface; | ||
| use Interop\Http\ServerMiddleware\MiddlewareInterface; | ||
| use Psr\Http\Message\ResponseInterface as PsrResponseInterface; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\ServerRequestInterface as PsrServerRequestInterface; | ||
| use Zend\EventManager\AbstractListenerAggregate; | ||
| use Zend\EventManager\EventManagerInterface; | ||
| use Zend\Mvc\Exception\MiddlewareNotCallableException; | ||
| use Zend\Mvc\Exception\ReachedFinalHandlerException; | ||
| use Zend\Psr7Bridge\Psr7ServerRequest as Psr7Request; | ||
| use Zend\Psr7Bridge\Psr7Response; | ||
| use Zend\Router\RouteMatch; | ||
| use Zend\Stratigility\MiddlewarePipe; | ||
| class MiddlewareListener extends AbstractListenerAggregate | ||
| { | ||
| @@ -47,15 +54,19 @@ public function onDispatch(MvcEvent $event) | ||
| $application = $event->getApplication(); | ||
| $response = $application->getResponse(); | ||
| $serviceManager = $application->getServiceManager(); | ||
| $psr7ResponsePrototype = Psr7Response::fromZend($response); | ||
| try { | ||
| $pipe = $this->createPipeFromSpec( | ||
| $serviceManager, | ||
| $psr7ResponsePrototype, | ||
| is_array($middleware) ? $middleware : [$middleware] | ||
| ); | ||
| } catch (MiddlewareNotCallableException $middlewareNotCallableException) { | ||
| $return = $this->marshalMiddlewareNotCallable( | ||
| $application::ERROR_MIDDLEWARE_CANNOT_DISPATCH, | ||
| $middlewareNotCallableException->toMiddlewareName(), | ||
| $event, | ||
| $application | ||
| ); | ||
| @@ -69,7 +80,13 @@ public function onDispatch(MvcEvent $event) | ||
| foreach ($routeMatch->getParams() as $key => $value) { | ||
| $psr7Request = $psr7Request->withAttribute($key, $value); | ||
| } | ||
| $return = $pipe( | ||
| ||
| $psr7Request, | ||
| $psr7ResponsePrototype, | ||
| function (PsrServerRequestInterface $request, PsrResponseInterface $response) { | ||
| throw ReachedFinalHandlerException::create(); | ||
| } | ||
| ); | ||
| } catch (\Throwable $ex) { | ||
| $caughtException = $ex; | ||
| } catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced | ||
| @@ -79,8 +96,6 @@ public function onDispatch(MvcEvent $event) | ||
| if ($caughtException !== null) { | ||
| $event->setName(MvcEvent::EVENT_DISPATCH_ERROR); | ||
| $event->setError($application::ERROR_EXCEPTION); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Why are these lines removed? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. There is potentially no longer just "one" middleware name... how do we decide which is the failed one? First? Last? IIRC I don't think there's any way (outside of the exception bubble here) Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Ah, right - that makes sense, then. | ||
| $event->setParam('exception', $caughtException); | ||
| $events = $application->getEventManager(); | ||
| @@ -100,6 +115,42 @@ public function onDispatch(MvcEvent $event) | ||
| return $response; | ||
| } | ||
| /** | ||
| * Create a middleware pipe from the array spec given. | ||
| * | ||
| * @param ContainerInterface $serviceLocator | ||
| * @param ResponseInterface $responsePrototype | ||
| * @param array $middlewaresToBePiped | ||
| * @return MiddlewarePipe | ||
| * @throws \InvalidArgumentException | ||
| * @throws \Zend\Mvc\Exception\MiddlewareNotCallableException | ||
| */ | ||
| private function createPipeFromSpec( | ||
| ContainerInterface $serviceLocator, | ||
| ResponseInterface $responsePrototype, | ||
| array $middlewaresToBePiped | ||
| ) { | ||
| $pipe = new MiddlewarePipe(); | ||
| $pipe->setResponsePrototype($responsePrototype); | ||
| foreach ($middlewaresToBePiped as $middlewareToBePiped) { | ||
| if (null === $middlewareToBePiped) { | ||
| throw new \InvalidArgumentException('Middleware name cannot be null'); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. If we rename the exception to | ||
| } | ||
| $middlewareName = is_string($middlewareToBePiped) ? $middlewareToBePiped : get_class($middlewareToBePiped); | ||
| if (is_string($middlewareToBePiped) && $serviceLocator->has($middlewareToBePiped)) { | ||
| $middlewareToBePiped = $serviceLocator->get($middlewareToBePiped); | ||
| } | ||
| if (! $middlewareToBePiped instanceof MiddlewareInterface && ! is_callable($middlewareToBePiped)) { | ||
| throw MiddlewareNotCallableException::fromMiddlewareName($middlewareName); | ||
| } | ||
| $pipe->pipe($middlewareToBePiped); | ||
| } | ||
| return $pipe; | ||
| } | ||
| /** | ||
| * Marshal a middleware not callable exception event | ||
| * | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
| /** | ||
| * Zend Framework (http://framework.zend.com/) | ||
| * | ||
| * @link http://github.com/zendframework/zf2 for the canonical source repository | ||
| * @copyright Copyright (c) 2005-2017 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; | ||
| use Zend\Mvc\Exception\MiddlewareNotCallableException; | ||
| final class MiddlewareNotCallableExceptionTest extends TestCase | ||
| { | ||
| public function testFromMiddlewareName() | ||
| { | ||
| $middlewareName = uniqid('middlewareName', true); | ||
| $exception = MiddlewareNotCallableException::fromMiddlewareName($middlewareName); | ||
| $this->assertInstanceOf(MiddlewareNotCallableException::class, $exception); | ||
| $this->assertSame('Cannot dispatch middleware ' . $middlewareName, $exception->getMessage()); | ||
| $this->assertSame($middlewareName, $exception->toMiddlewareName()); | ||
| } | ||
| public function testToMiddlewareNameWhenNotSet() | ||
| { | ||
| $exception = new MiddlewareNotCallableException(); | ||
| $this->assertSame('', $exception->toMiddlewareName()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
| /** | ||
| * Zend Framework (http://framework.zend.com/) | ||
| * | ||
| * @link http://github.com/zendframework/zf2 for the canonical source repository | ||
| * @copyright Copyright (c) 2005-2017 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; | ||
| use Zend\Mvc\Exception\ReachedFinalHandlerException; | ||
| final class ReachedFinalHandlerExceptionTest extends TestCase | ||
| { | ||
| public function testFromNothing() | ||
| { | ||
| $exception = ReachedFinalHandlerException::create(); | ||
| $this->assertInstanceOf(ReachedFinalHandlerException::class, $exception); | ||
| $this->assertSame( | ||
| 'Reached the final handler for middleware pipe - check the pipe configuration', | ||
| $exception->getMessage() | ||
| ); | ||
| } | ||
| } |
Uh oh!
There was an error while loading.Please reload this page.