diff --git a/composer.json b/composer.json index 2e9deb1..a0906c4 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 <2.2", + "mouf/mvc.splash-common": "^10.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..77f833f --- /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()) { + return $this->ifMiddleware->process($request, $handler); + } else if($this->elseMiddleware) { + return $this->elseMiddleware->process($request, $handler); + } else { + return $handler->handle($request); + } + } +} \ No newline at end of file diff --git a/src/Mouf/Mvc/Splash/Controllers/Admin/SplashCreateControllerController.php b/src/Mouf/Mvc/Splash/Controllers/Admin/SplashCreateControllerController.php index 96af82b..33ad43e 100644 --- a/src/Mouf/Mvc/Splash/Controllers/Admin/SplashCreateControllerController.php +++ b/src/Mouf/Mvc/Splash/Controllers/Admin/SplashCreateControllerController.php @@ -3,13 +3,13 @@ namespace Mouf\Mvc\Splash\Controllers\Admin; use Mouf\Composer\ClassNameMapper; -use Mouf\Mvc\Splash\Controllers\Controller; use Mouf\Html\Template\TemplateInterface; use Mouf\Html\HtmlElement\HtmlBlock; use Mouf\Html\Utils\WebLibraryManager\WebLibrary; -use Mouf\Mvc\Splash\Services\SplashCreateControllerService; -use Mouf\Mvc\Splash\Services\SplashCreateControllerServiceException; use Mouf\MoufManager; +use Mouf\Mvc\Splash\Controllers\Controller; +use TheCodingMachine\Splash\Services\SplashCreateControllerService; +use TheCodingMachine\Splash\Services\SplashCreateControllerServiceException; /** * A controller used to create controllers in Splash. diff --git a/src/Mouf/Mvc/Splash/MiddlewarePipe.php b/src/Mouf/Mvc/Splash/MiddlewarePipe.php new file mode 100644 index 0000000..5018eae --- /dev/null +++ b/src/Mouf/Mvc/Splash/MiddlewarePipe.php @@ -0,0 +1,70 @@ + + * 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, RequestHandlerInterface +{ + 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); + } + + /** + * Handle an incoming request. + * + * Attempts to handle an incoming request by doing the following: + * + * - Cloning itself, to produce a request handler. + * - Dequeuing the first middleware in the cloned handler. + * - Processing the first middleware using the request and the cloned handler. + * + * If the pipeline is empty at the time this method is invoked, it will + * raise an exception. + * + * @throws Exception\EmptyPipelineException if no middleware is present in + * the instance in order to process the request. + */ + public function handle(ServerRequestInterface $request) : ResponseInterface + { + return $this->zendPipe->handle($request); + } + +} 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()); - } - } - } -}