Skip to content

Commit

Permalink
Middlewares in route and group routes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicollassilva committed Jun 9, 2021
1 parent d483878 commit c9c473e
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 29 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"autoload": {
"psr-4": {
"MinasRouter\\": "src/"
"MinasRouter\\": "src/",
"App\\": "app/"
}
},
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions src/Exceptions/BadMiddlewareExecuteException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MinasRouter\Exceptions;

class BadMiddlewareExecuteException extends \Exception
{

}
190 changes: 190 additions & 0 deletions src/Router/Middlewares/MiddlewareCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

namespace MinasRouter\Router\Middlewares;

use MinasRouter\Http\Request;
use MinasRouter\Router\Middlewares\MiddlewareRoute;

class MiddlewareCollection
{
/** @var array */
protected $middlewares = [];

/** @var array */
protected $currentRequest;

/** @var array */
protected $queue;

/** @var int = 0 */
protected $currentQueueNumber = 0;

public function __construct($middlewares)
{
$this->middlewares = $middlewares;
}

/**
* Method responsible for setting the Route's Request
*
* @param MinasRouter\Http\Request $request
*
* @return void
*/
public function setRequest(Request $request)
{
$this->currentRequest = $request;
}

/**
* Method responsible for setting the
* middlewares of the route.
*
* @var array $middlewares
*
* @return void
*/
public function setMiddlewares(Array $middlewares)
{
$this->middlewares = $middlewares;
}

/**
* Alias for execute the middlewares.
*
* @return mixed|bool
*/
public function execute()
{
return $this->executeMiddleware();
}

/**
* Execute the middlewares.
*
* @return mixed|bool
*/
protected function executeMiddleware()
{
$middlewares = $this->middlewares;

if (is_string($middlewares)) {
$middlewares = explode(',', $middlewares);
}

$this->resolveNestedMiddleware($middlewares);

return $this->callMiddlewares();
}

/**
* Method responsible for identifying
* middlewares and instantiating them.
*
* @param array $middlewares
*
* @return void
*/
protected function resolveNestedMiddleware(Array $middlewares): void
{
$this->queue = array_map(function($middleware) {
$middleware = trim(rtrim($middleware));

return $this->instanceMiddleware($middleware);
}, $middlewares);
}

/**
* Method responsible for instantiating middlewares.
*
* @param string
*
* @return null|object
*/
protected function instanceMiddleware($middleware)
{
if (!preg_match("/\\\/", $middleware)) {
if(!$middlewareClass = $this->getByAlias($middleware)) return;

return new $middlewareClass();
}

if (class_exists($middleware)) {
return new $middleware();
}

return;
}

/**
* Method responsible for identifying
* a middleware by alias.
*
* @param string $alias
*
* @return null|string
*/
protected function getByAlias(String $alias)
{
$middlewares = MiddlewareRoute::getSetMiddlewares();

if (!array_key_exists($alias, $middlewares)) {
return;
}

if (class_exists($middlewares[$alias])) {
return $middlewares[$alias];
}

return;
}

/**
* Method responsible for
* calling the next middleware.
*
* @return mixed|bool
*/
protected function next()
{
$this->currentQueueNumber++;

return $this->callMiddlewares();
}

/**
* Method responsible for resetting
* the middleware queue.
*
* @return void
*/
protected function reset()
{
$this->currentQueueNumber = 0;
}

/**
* Method responsible for calling
* middlewares and class handle.
*
* @return mixed|bool
*/
protected function callMiddlewares()
{
if (!isset($this->queue[$this->currentQueueNumber])) {
$this->reset();
return true;
}

$currentMiddleware = $this->queue[$this->currentQueueNumber];

if (is_null($currentMiddleware) || empty($currentMiddleware)) {
return $this->next();
}

return $currentMiddleware->handle(
$this->currentRequest,
fn() => $this->next()
);
}
}
31 changes: 31 additions & 0 deletions src/Router/Middlewares/MiddlewareRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace MinasRouter\Router\Middlewares;

abstract class MiddlewareRoute
{
/** @var array */
protected static $setMiddlewares = [];

/**
* Method responsible for setting middlewares and their alias.
*
* @param array|string|null $middlewares
*
* @return void
*/
public static function setMiddlewares($middlewares)
{
self::$setMiddlewares = $middlewares;
}

/**
* Return the set Middlewares.
*
* @return array
*/
public static function getSetMiddlewares()
{
return self::$setMiddlewares;
}
}
17 changes: 0 additions & 17 deletions src/Router/Middlewares/Middlewares.php

This file was deleted.

60 changes: 59 additions & 1 deletion src/Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MinasRouter\Router\RouteGroups;
use MinasRouter\Router\RouteCollection;
use MinasRouter\Router\Middlewares\MiddlewareRoute;

abstract class Route extends RouteCollection
{
Expand Down Expand Up @@ -138,6 +139,12 @@ public static function match(array $methods, String $uri, $callback)
return self::$collection->addMultipleHttpRoutes($uri, $callback, $methods);
}

/**
* @param string $method
* @param array $arguments
*
* @return void
*/
public static function __callStatic($method, $arguments)
{
return self::$collection->{$method}(...$arguments);
Expand All @@ -153,6 +160,12 @@ public static function execute()
self::$collection->run();
}

/**
* Method responsible for creating a
* new instance of RouteGroups.
*
* @return \MinasRouter\Router\RouteGroups
*/
private static function newRouteGroup()
{
$group = (new RouteGroups(self::$collection));
Expand All @@ -162,21 +175,66 @@ private static function newRouteGroup()
return $group;
}

/**
* Method responsible for setting all global
* middlewares and identifying them with alias.
*
* @param Array $middlewares
*
* @return void
*/
public static function globalMiddlewares(Array $middlewares)
{
MiddlewareRoute::setMiddlewares($middlewares);
}

/**
* Method responsible for setting the default
* namespace in a route group.
*
* @param string $namespace
*
* @return \MinasRouter\Router\RouteGroups
*/
public static function namespace(String $namespace)
{
return self::newRouteGroup()->namespace($namespace);
}

/**
* Method responsible for setting the default
* prefix in a route group.
*
* @param string $prefix
*
* @return \MinasRouter\Router\RouteGroups
*/
public static function prefix(String $prefix)
{
return self::newRouteGroup()->prefix($prefix);
}

public static function middleware(String $middleware)
/**
* Method responsible for setting the default
* middleware in a route group.
*
* @param string $middleware
*
* @return \MinasRouter\Router\RouteGroups
*/
public static function middleware($middleware)
{
return self::newRouteGroup()->middlewares($middleware);
}

/**
* Method responsible for setting the default
* name in a route group.
*
* @param string $name
*
* @return \MinasRouter\Router\RouteGroups
*/
public static function name(String $name)
{
return self::newRouteGroup()->name($name);
Expand Down
Loading

0 comments on commit c9c473e

Please sign in to comment.