diff --git a/composer.json b/composer.json index 713f0a1..790e6e8 100644 --- a/composer.json +++ b/composer.json @@ -21,8 +21,8 @@ ], "require" : { "php" : ">=7.0", - "mouf/mvc.splash-common": "^8.2", - "zendframework/zend-stratigility": "^2.0", + "mouf/mvc.splash-common": "^9.0", + "zendframework/zend-stratigility": "^3.0", "mouf/mouf": "~2.0.0" }, "autoload" : { diff --git a/src/Mouf/Mvc/Splash/ConditionMiddleware.php b/src/Mouf/Mvc/Splash/ConditionMiddleware.php new file mode 100644 index 0000000..012ad79 --- /dev/null +++ b/src/Mouf/Mvc/Splash/ConditionMiddleware.php @@ -0,0 +1,53 @@ +condition = $condition; + $this->ifMiddleware = $ifMiddleware; + $this->elseMiddleware = $elseMiddleware; + } + + /** + * Process an incoming server request and return a response, optionally delegating + * response creation to a handler. + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + if($this->condition->isOk()) { + $this->ifMiddleware->process($request, $handler); + } else if($this->elseMiddleware) { + $this->elseMiddleware->process($request, $handler); + } else { + $handler->handle($request); + } + } +} \ No newline at end of file diff --git a/src/Mouf/Mvc/Splash/MiddlewarePipe.php b/src/Mouf/Mvc/Splash/MiddlewarePipe.php new file mode 100644 index 0000000..10b306a --- /dev/null +++ b/src/Mouf/Mvc/Splash/MiddlewarePipe.php @@ -0,0 +1,51 @@ + + * It acts as a wrapper on Zend's MiddleWarePipe
+ * It is in charge of binding an Url to a Controller.
+ * There is one and only one instance of Splash per web application.
+ * The name of the instance MUST be "splashMiddleware".
+ *
+ * The SplashMiddleware component has several ways to bind an URL to a Controller.
+ * It can do so based on the @URL annotation, or based on the @Action annotation.
+ * Check out the Splash documentation here: + * https://github.com/thecodingmachine/mvc.splash/. + */ +class MiddlewarePipe implements MiddlewareInterface +{ + private $zendPipe; + + /** + * MiddlewarePipe constructor. + * @param MiddlewareInterface[] $middlewares + */ + public function __construct(array $middlewares) + { + $this->zendPipe = new ZendMiddleWarePipe(); + foreach ($middlewares as $middleware) { + /** @var Router $router */ + $this->zendPipe->pipe($middleware); + } + } + + /** + * Process an incoming server request and return a response, optionally delegating + * response creation to a handler. + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + return $this->zendPipe->process($request, $handler); + } + + +} diff --git a/src/Mouf/Mvc/Splash/Routers/Router.php b/src/Mouf/Mvc/Splash/Routers/Router.php deleted file mode 100644 index 3f8f30c..0000000 --- a/src/Mouf/Mvc/Splash/Routers/Router.php +++ /dev/null @@ -1,77 +0,0 @@ -path = $path; - $this->middleware = $middleware; - $this->enableCondition = $enableCondition; - } - - /** - * The path to that middleware (defaults to /). - * - * @return string - */ - public function getPath() - { - return $this->path; - } - - /** - * The PSR-7 middleware to call. - * - * @return MiddlewareInterface - */ - public function getMiddleware() - { - return $this->middleware; - } - - /** - * If this returns false, the router is skipped. - * - * @return bool - */ - public function isActive() - { - if ($this->enableCondition !== null && $this->enableCondition->isOk() === false) { - return false; - } else { - return true; - } - } -} diff --git a/src/Mouf/Mvc/Splash/Routers/RouterInterface.php b/src/Mouf/Mvc/Splash/Routers/RouterInterface.php deleted file mode 100644 index c388260..0000000 --- a/src/Mouf/Mvc/Splash/Routers/RouterInterface.php +++ /dev/null @@ -1,39 +0,0 @@ - - * It is in charge of binding an Url to a Controller.
- * There is one and only one instance of Splash per web application.
- * The name of the instance MUST be "splashMiddleware".
- *
- * The SplashMiddleware component has several ways to bind an URL to a Controller.
- * It can do so based on the @URL annotation, or based on the @Action annotation.
- * Check out the Splash documentation here: - * https://github.com/thecodingmachine/mvc.splash/. - */ -class SplashMiddleware extends MiddlewarePipe -{ - /** - * @param RouterInterface[] $routers - */ - public function __construct(array $routers) - { - parent::__construct(); - $this->setResponsePrototype(new Response()); - foreach ($routers as $router) { - if ($router->isActive()) { - $this->pipe($router->getPath(), $router->getMiddleware()); - } - } - } -}