From 862edf6752aba9a583e493ee7dbbcaaa8500553f Mon Sep 17 00:00:00 2001 From: Shankar Thiyagaraajan Date: Wed, 5 Oct 2016 12:55:00 +0530 Subject: [PATCH 1/5] - Implementing Middleware Route Define ex. $router->get([ 'as' => 'URI', 'uri' => '/name', 'middleware' => '[your_middlewares]', 'uses' => __NAMESPACE__ . '\Controllers\Controller_name@function ]); --- Herbert/Framework/Route.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Herbert/Framework/Route.php b/Herbert/Framework/Route.php index 457fd73..5b530b9 100644 --- a/Herbert/Framework/Route.php +++ b/Herbert/Framework/Route.php @@ -33,6 +33,11 @@ class Route { * @var string */ protected $uses; + + /** + * @var string + */ + protected $middleware; /** * @param \Herbert\Framework\Application $app @@ -46,6 +51,7 @@ public function __construct(Application $app, $data, $parameters = []) $this->uri = $data['uri']; $this->name = array_get($data, 'as', $this->uri); $this->uses = $data['uses']; + $this->middleware = $data['middleware']; } /** From db8436742b51a85d4ac2b9f610a7fb6969883cf9 Mon Sep 17 00:00:00 2001 From: Shankar Thiyagaraajan Date: Thu, 6 Oct 2016 11:08:50 +0530 Subject: [PATCH 2/5] - Middleware Implementation --- Herbert/Framework/Router.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Herbert/Framework/Router.php b/Herbert/Framework/Router.php index 5b41a07..8d27bc4 100644 --- a/Herbert/Framework/Router.php +++ b/Herbert/Framework/Router.php @@ -292,6 +292,8 @@ protected function buildRoute($data, $params) */ protected function processRequest(Route $route) { + // Middleware Implementations + (new Middleware())->init($route); $this->processResponse($route->handle()); } From 2d8ae712ac5a36d6f20144557f6f8969fff06746 Mon Sep 17 00:00:00 2001 From: Shankar Thiyagaraajan Date: Thu, 6 Oct 2016 11:09:40 +0530 Subject: [PATCH 3/5] - Implementing Middleware --- Herbert/Framework/Middleware.php | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Herbert/Framework/Middleware.php diff --git a/Herbert/Framework/Middleware.php b/Herbert/Framework/Middleware.php new file mode 100644 index 0000000..571d140 --- /dev/null +++ b/Herbert/Framework/Middleware.php @@ -0,0 +1,58 @@ +request = $http; + $this->handleRequest(); + } + + /** + * To Handling the Request with Specific Middlewares. + */ + protected function handleRequest() + { + if ($this->hasMiddleware()) { + $middleware = $this->request->middleware; + + if (class_exists($middleware)) { + $request = new $middleware; + $request->handle($this->request); + } + } + } + + + /** + * To Check Middleware Existence. + * + * @return bool + */ + public function hasMiddleware() + { + return (isset($this->request->middleware) && !empty($this->request->middleware)); + } +} From c7b66d0489b951aa3036ce9a5a11874c13b360bc Mon Sep 17 00:00:00 2001 From: Shankar Thiyagaraajan Date: Thu, 6 Oct 2016 16:23:17 +0530 Subject: [PATCH 4/5] - Request Chain Re-Build --- Herbert/Framework/Router.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Herbert/Framework/Router.php b/Herbert/Framework/Router.php index 8d27bc4..beff62b 100644 --- a/Herbert/Framework/Router.php +++ b/Herbert/Framework/Router.php @@ -293,8 +293,10 @@ protected function buildRoute($data, $params) protected function processRequest(Route $route) { // Middleware Implementations - (new Middleware())->init($route); + $route = (new Middleware())->init($route); + $this->processResponse($route->handle()); + } /** From ad5c16d0c9613cd91e6e46b485b2d345a7d116d9 Mon Sep 17 00:00:00 2001 From: Shankar Thiyagaraajan Date: Thu, 6 Oct 2016 16:24:00 +0530 Subject: [PATCH 5/5] - Request Chain Re-Build --- Herbert/Framework/Middleware.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Herbert/Framework/Middleware.php b/Herbert/Framework/Middleware.php index 571d140..3baa0b0 100644 --- a/Herbert/Framework/Middleware.php +++ b/Herbert/Framework/Middleware.php @@ -3,6 +3,7 @@ /** * Class Middleware + * @package Plugin\library */ class Middleware extends Route { @@ -23,11 +24,12 @@ public function __construct() * Init Middleware Handler. * * @param $http + * @return bool */ public function init($http) { $this->request = $http; - $this->handleRequest(); + return $this->handleRequest(); } /** @@ -35,14 +37,16 @@ public function init($http) */ protected function handleRequest() { + $request = $this->request; if ($this->hasMiddleware()) { $middleware = $this->request->middleware; if (class_exists($middleware)) { $request = new $middleware; - $request->handle($this->request); + $request = $request->handle($this->request); } } + return $request; }