Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware Implementation #35

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Herbert/Framework/Middleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
namespace Herbert\Framework;

/**
* Class Middleware
* @package Plugin\library
*/
class Middleware extends Route
{
/**
* @var $request
*/
protected $request;

/**
* Middleware constructor.
*/
public function __construct()
{
//
}

/**
* Init Middleware Handler.
*
* @param $http
* @return bool
*/
public function init($http)
{
$this->request = $http;
return $this->handleRequest();
}

/**
* To Handling the Request with Specific Middlewares.
*/
protected function handleRequest()
{
$request = $this->request;
if ($this->hasMiddleware()) {
$middleware = $this->request->middleware;

if (class_exists($middleware)) {
$request = new $middleware;
$request = $request->handle($this->request);
}
}
return $request;
}


/**
* To Check Middleware Existence.
*
* @return bool
*/
public function hasMiddleware()
{
return (isset($this->request->middleware) && !empty($this->request->middleware));
}
}
6 changes: 6 additions & 0 deletions Herbert/Framework/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class Route {
* @var string
*/
protected $uses;

/**
* @var string
*/
protected $middleware;

/**
* @param \Herbert\Framework\Application $app
Expand All @@ -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'];
}

/**
Expand Down
4 changes: 4 additions & 0 deletions Herbert/Framework/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ protected function buildRoute($data, $params)
*/
protected function processRequest(Route $route)
{
// Middleware Implementations
$route = (new Middleware())->init($route);

$this->processResponse($route->handle());

}

/**
Expand Down