From 5c82b9ec200be963ff91981dd74539e989859b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=27Guessan=20Kouadio=20Elis=C3=A9e?= Date: Thu, 24 Mar 2022 10:33:19 +0000 Subject: [PATCH] feat: added custom routes paths configuration fix routes grouping improve storage support update views ui --- config/routes.php | 15 +++++ core/Console/Cache.php | 2 +- core/Console/Logs.php | 2 +- core/Console/Routes.php | 5 +- core/Console/Server.php | 2 +- core/Console/Tests.php | 2 +- core/Routing/Route.php | 105 +++++++++++++++++++++++----------- core/Support/Storage.php | 33 ++++++++--- routes/api.php | 4 +- routes/auth.php | 54 +++++++++-------- routes/web.php | 2 - views/index.html.twig | 6 +- views/layouts/email.html.twig | 6 +- 13 files changed, 156 insertions(+), 82 deletions(-) create mode 100644 config/routes.php diff --git a/config/routes.php b/config/routes.php new file mode 100644 index 00000000..8b3427a6 --- /dev/null +++ b/config/routes.php @@ -0,0 +1,15 @@ + [], +]; diff --git a/core/Console/Cache.php b/core/Console/Cache.php index ad234695..61cf9d98 100644 --- a/core/Console/Cache.php +++ b/core/Console/Cache.php @@ -18,7 +18,7 @@ */ class Cache extends Command { - protected static $defaultName = 'twig:clear'; + protected static $defaultName = 'clear:twig'; protected function configure() { diff --git a/core/Console/Logs.php b/core/Console/Logs.php index 9311a042..a20fef40 100644 --- a/core/Console/Logs.php +++ b/core/Console/Logs.php @@ -18,7 +18,7 @@ */ class Logs extends Command { - protected static $defaultName = 'logs:clear'; + protected static $defaultName = 'clear:logs'; protected function configure() { diff --git a/core/Console/Routes.php b/core/Console/Routes.php index 63d7e606..aa10f632 100644 --- a/core/Console/Routes.php +++ b/core/Console/Routes.php @@ -14,7 +14,7 @@ */ class Routes extends Command { - protected static $defaultName = 'routes:list'; + protected static $defaultName = 'routes'; protected function configure() { @@ -24,7 +24,6 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { $rows = []; - $routes = Route::$routes; foreach ($routes as $route => $options) { @@ -43,7 +42,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } if (!empty($middlewares)) { - $middlewares = json_encode($middlewares); + $middlewares = implode(', ', $middlewares); } $rows[] = [$method, $uri, $handler, $middlewares, $name]; diff --git a/core/Console/Server.php b/core/Console/Server.php index 49c32c2d..8637a098 100644 --- a/core/Console/Server.php +++ b/core/Console/Server.php @@ -13,7 +13,7 @@ */ class Server extends Command { - protected static $defaultName = 'server:start'; + protected static $defaultName = 'serve'; protected function configure() { diff --git a/core/Console/Tests.php b/core/Console/Tests.php index 5504b7a6..23cf1b1b 100644 --- a/core/Console/Tests.php +++ b/core/Console/Tests.php @@ -13,7 +13,7 @@ */ class Tests extends Command { - protected static $defaultName = 'tests:run'; + protected static $defaultName = 'test'; protected function configure() { diff --git a/core/Routing/Route.php b/core/Routing/Route.php index 1102da46..f4b4650b 100644 --- a/core/Routing/Route.php +++ b/core/Routing/Route.php @@ -22,91 +22,128 @@ class Route private static function add(string $route, $handler): self { - static::$route = self::format($route); + static::$route = static::format($route); static::$tmp_routes[static::$route] = ['handler' => $handler]; - return new self(); + return new static(); } public static function get(string $uri, $handler): self { - return self::add('GET ' . $uri, $handler); + return static::add('GET ' . $uri, $handler); } public static function post(string $uri, $handler): self { - return self::add('POST ' . $uri, $handler); + return static::add('POST ' . $uri, $handler); } public static function delete(string $uri, $handler): self { - return self::add('DELETE ' . $uri, $handler); + return static::add('DELETE ' . $uri, $handler); } public static function options(string $uri, $handler): self { - return self::add('OPTIONS ' . $uri, $handler); + return static::add('OPTIONS ' . $uri, $handler); } public static function patch(string $uri, $handler): self { - return self::add('PATCH ' . $uri, $handler); + return static::add('PATCH ' . $uri, $handler); } public static function put(string $uri, $handler): self { - return self::add('PUT ' . $uri, $handler); + return static::add('PUT ' . $uri, $handler); } public static function any(string $uri, $handler): self { - return self::add('GET|POST|DELETE|PUT|OPTIONS|PATCH ' . $uri, $handler); + return static::add('GET|POST|DELETE|PUT|OPTIONS|PATCH ' . $uri, $handler); } public static function match(string $methods, string $uri, $handler): self { - return self::add($methods . ' ' . $uri, $handler); + return static::add($methods . ' ' . $uri, $handler); } public static function view(string $uri, string $view, array $params = []): self { - return self::get($uri, function (Response $response) use ($view, $params) { + return static::get($uri, function (Response $response) use ($view, $params) { $response->view($view, $params); }); } public function name(string $name): self { - static::$tmp_routes[static::$route] += ['name' => $name]; + static::$tmp_routes[static::$route]['name'] = $name; return $this; } + + public static function group($callback): self + { + call_user_func($callback); + return new static(); + } - public function middlewares(string ...$middlewares): self + public function middleware(string ...$middlewares): self { - static::$tmp_routes[static::$route] += ['middlewares' => $middlewares]; + static::$tmp_routes[static::$route]['middlewares'] = $middlewares; return $this; } - public static function groupMiddlewares(array $middlewares, $callback): self + public function byMiddleware(string ...$middlewares): self { - call_user_func($callback); + foreach (static::$tmp_routes as $route => $options) { + if (isset($options['middlewares'])) { + static::$tmp_routes[$route]['middlewares'] = array_merge($middlewares, $options['middlewares']); + } else { + static::$tmp_routes[$route]['middlewares'] = $middlewares; + } + } + + return $this; + } + + public function byPrefix(string $prefix): self + { + if ($prefix[-1] === '/') $prefix = rtrim($prefix, '/'); foreach (static::$tmp_routes as $route => $options) { - static::$tmp_routes[$route] += ['middlewares' => $middlewares]; + list($method, $uri) = explode(' ', $route, 2); + $_route = implode(' ', [$method, $prefix . $uri]); + + $_route = static::format($_route); + static::$tmp_routes = static::update($route, $_route); } - return new self(); + return $this; } - public static function groupPrefix(string $prefix, $callback): self + public function byName(string $name): self { - call_user_func($callback); - foreach (static::$tmp_routes as $route => $options) { - $_route = self::format(self::addPrefix($prefix, $route)); - static::$tmp_routes = self::update($route, $_route); + if (isset($options['name'])) { + static::$tmp_routes[$route]['name'] = $name . '.' . $options['name']; + } else { + static::$tmp_routes[$route]['name'] = $name; + } } - return new self(); + return $this; + } + + public function byController(string $controller): self + { + foreach (static::$tmp_routes as $route => $options) { + if (isset($options['handler'])) { + static::$tmp_routes[$route]['handler'] = [$controller, $options['handler']]; + } else { + static::$tmp_routes[$route]['handler'] = $controller; + } + } + + return $this; } public function register() @@ -117,15 +154,6 @@ public function register() static::$tmp_routes = []; } - private static function addPrefix(string $prefix, string $route) - { - if ($prefix[-1] === '/') $prefix = rtrim($prefix, '/'); - - list($method, $uri) = explode(' ', $route, 2); - - return implode(' ', [$method, $prefix . $uri]); - } - private static function format(string $route) { list($method, $uri) = explode(' ', $route, 2); @@ -169,5 +197,16 @@ public static function load() foreach ($routes as $route) { require_once config('storage.routes') . $route; } + + $paths = config('routes.paths'); + + foreach ($paths as $path) { + $storage = Storage::path(config('storage.routes'))->addPath($path); + $custom_routes = $storage->getFiles(); + + foreach ($custom_routes as $custom_route) { + require_once $storage->file($custom_route); + } + } } } diff --git a/core/Support/Storage.php b/core/Support/Storage.php index 77a8c8a8..c2b5c668 100644 --- a/core/Support/Storage.php +++ b/core/Support/Storage.php @@ -8,6 +8,8 @@ namespace Core\Support; +use ErrorException; + /** * Manage files and folders */ @@ -119,16 +121,14 @@ public function deleteDir(string $pathname = '') return false; } - + public function getFiles() { $results = []; - $objects = scandir(self::$path); + $objects = $this->getFilesAndFolders(); foreach ($objects as $object) { - if ($object != '.' && $object != '..' && $this->isFile($object)) { - $results[] = basename($object); - } + if ($this->isFile($object)) $results[] = basename($object); } return $results; @@ -137,11 +137,28 @@ public function getFiles() public function getFolders() { $results = []; - $objects = scandir(self::$path); + $objects = $this->getFilesAndFolders(); + + foreach ($objects as $object) { + if ($this->isDir($object)) $results[] = basename($object); + } + + return $results; + } + + public function getFilesAndFolders() + { + $results = []; + + try { + $objects = scandir(self::$path); + } catch(ErrorException $e) { + return $results; + } foreach ($objects as $object) { - if ($object != '.' && $object != '..' && $this->isDir($object)) { - $results[] = basename($object); + if ($object != '.' && $object != '..') { + $results[] = $object; } } diff --git a/routes/api.php b/routes/api.php index 0ac3f34c..940ec0d4 100644 --- a/routes/api.php +++ b/routes/api.php @@ -12,6 +12,6 @@ * API routes */ -Route::groupPrefix('api', function () { +Route::group(function () { // -})->register(); +})->byPrefix('api')->register(); diff --git a/routes/auth.php b/routes/auth.php index 9ab20e14..bbb81e93 100644 --- a/routes/auth.php +++ b/routes/auth.php @@ -19,27 +19,33 @@ * Authentication routes */ -Route::groupMiddlewares(['remember'], function () { - Route::get('login', [LoginController::class, 'index']); - Route::get('signup', [RegisterController::class, 'index']); -})->register(); - -Route::groupMiddlewares(['csrf'], function () { - Route::post('authenticate', [LoginController::class, 'authenticate']); - Route::post('register', [RegisterController::class, 'register']); -})->register(); - -Route::post('logout', LogoutController::class)->middlewares('auth')->register(); - -Route::view('password/forgot', 'auth.password.forgot')->register(); - -Route::get('password/new', function (Request $request, Response $response) { - $response->view('auth.password.new', ['email' => $request->queries('email')]); -})->register(); - -Route::get('password/reset', [ForgotPasswordController::class, 'reset'])->register(); -Route::post('password/notify', [ForgotPasswordController::class, 'notify'])->register(); -Route::post('password/update', [ForgotPasswordController::class, 'update'])->register(); - -Route::get('email/verify', [EmailVerificationController::class, 'verify'])->register(); -Route::get('email/notify', [EmailVerificationController::class, 'notify'])->register(); \ No newline at end of file +Route::group(function () { + Route::get('/login', [LoginController::class, 'index']); + Route::get('/signup', [RegisterController::class, 'index']); +})->byMiddleware('remember')->register(); + +Route::group(function () { + Route::post('/authenticate', [LoginController::class, 'authenticate']); + Route::post('/register', [RegisterController::class, 'register']); +})->byMiddleware('csrf')->register(); + +Route::group(function () { + Route::view('/forgot', 'auth.password.forgot'); + + Route::get('/new', function (Request $request, Response $response) { + $response->view('auth.password.new', ['email' => $request->queries('email')]); + }); +})->byPrefix('password')->register(); + +Route::group(function () { + Route::get('/reset', 'reset'); + Route::post('/notify', 'notify'); + Route::post('/update', 'update'); +})->byPrefix('password')->byController(ForgotPasswordController::class)->register(); + +Route::group(function () { + Route::get('/verify', 'verify'); + Route::get('/notify', 'notify'); +})->byPrefix('email')->byController(EmailVerificationController::class)->register(); + +Route::post('/logout', LogoutController::class)->middleware('auth')->register(); diff --git a/routes/web.php b/routes/web.php index e5d01c7a..6dd1fe6d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -6,8 +6,6 @@ * @link https://github.com/eliseekn/tinymvc */ -use Core\Http\Response\JsonResponse; -use Core\Http\Response\Response; use Core\Routing\Route; /** diff --git a/views/index.html.twig b/views/index.html.twig index 2f2efb9d..1a87ce8a 100644 --- a/views/index.html.twig +++ b/views/index.html.twig @@ -32,9 +32,13 @@ {% if auth('id') is empty %} - + Log in + + + Sign up + {% else %}