Last active
February 23, 2021 01:26
-
-
Save LucianoCharlesdeSouza/974abd05e423e025914cfc04cffa5a81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Router | |
{ | |
protected $middleware = []; | |
protected $groupStack = []; | |
protected $routes = []; | |
protected $namespaceDefault = 'App\\Controllers\\'; | |
public function get($uri, $callback) | |
{ | |
if ($this->hasGroupStack()) { | |
foreach ($this->getGroupStack() as $key => $value) { | |
$namespace = $this->namespaceDefault . $value['namespace'] . '\\'; | |
$middlewares = ($value['middleware']) ?? []; | |
} | |
} | |
$this->routes['GET'][] = [ | |
'uri' => $uri, | |
'middlewares' => $middlewares ?? [], | |
'callback' => (!is_callable($callback)) ? ($namespace ?? $this->namespaceDefault) . $callback : $callback, | |
]; | |
return $this; | |
} | |
public function getRoutes() | |
{ | |
return $this->routes; | |
} | |
public function group(array $attributes, Closure $callback) | |
{ | |
$this->updateGroupStack($attributes); | |
$this->loadRoutes($callback); | |
array_pop($this->groupStack); | |
} | |
public function getGroupStack() | |
{ | |
return $this->groupStack; | |
} | |
public function hasGroupStack() | |
{ | |
return !empty($this->groupStack); | |
} | |
protected function updateGroupStack(array $attributes) | |
{ | |
if ($this->hasGroupStack()) { | |
$attributes = $this->mergeWithLastGroup($attributes); | |
} | |
$this->groupStack[] = $attributes; | |
} | |
public function mergeWithLastGroup($new) | |
{ | |
return $this->merge($new, end($this->groupStack)); | |
} | |
protected function loadRoutes($callback) | |
{ | |
if ($callback instanceof Closure) { | |
$callback($this); | |
} | |
} | |
public function merge($new, $old) | |
{ | |
return $new = array_merge($new, [ | |
'namespace' => $this->formatNamespace($new, $old), | |
'middleware' => $this->formatMiddleware($new, $old), | |
]); | |
} | |
protected function formatNamespace($new, $old) | |
{ | |
if (isset($new['namespace'])) { | |
return isset($old['namespace']) && strpos($new['namespace'], '\\') !== 0 | |
? trim($old['namespace'], '\\') . '\\' . trim($new['namespace'], '\\') | |
: trim($new['namespace'], '\\'); | |
} | |
return $old['namespace'] ?? null; | |
} | |
protected function formatMiddleware($new, $old) | |
{ | |
if (isset($new['middleware'])) { | |
return isset($old['middleware']) | |
? array_merge($old['middleware'], $new['middleware']) | |
: $new['middleware']; | |
} | |
return $old['middleware'] ?? null; | |
} | |
} | |
class Route | |
{ | |
protected static $router; | |
private function __construct() | |
{ | |
/* prevents class instance */ | |
} | |
protected static function getRouter() | |
{ | |
if (empty(self::$router)) { | |
self::$router = new Router; | |
} | |
return self::$router; | |
} | |
public static function group(array $attributes, Closure $callback) | |
{ | |
return self::getRouter()->group($attributes, $callback); | |
} | |
public static function get($pattern, $callback) | |
{ | |
return self::getRouter()->get($pattern, $callback); | |
} | |
public static function getRoutes() | |
{ | |
return self::getRouter()->getRoutes(); | |
} | |
} | |
Route::group(['namespace' => 'Admin', 'middleware' => ['auth']], function () { | |
Route::get('/admin', 'PainelController@index'); | |
Route::group(['namespace' => 'Client', 'middleware' => ['role', 'csrf']], function () { | |
Route::get('/admin/client', 'ClientController@index'); | |
}); | |
}); | |
Route::get('/', 'HomeController@index'); | |
Route::get('/teste', function () { | |
//todo | |
}); | |
Route::group(['namespace' => 'Site', 'middleware' => ['csrf']], function () { | |
Route::get('/contato', 'ContatoController@index'); | |
}); | |
echo '<pre>'; | |
print_r(Route::getRoutes()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment