Skip to content

Commit

Permalink
feat: added custom routes paths configuration
Browse files Browse the repository at this point in the history
fix routes grouping
improve storage support
update views ui
  • Loading branch information
eliseekn committed Mar 24, 2022
1 parent 2104448 commit 5c82b9e
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 82 deletions.
15 changes: 15 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* @copyright (2019 - 2022) - N'Guessan Kouadio Elisée ([email protected])
* @license MIT (https://opensource.org/licenses/MIT)
* @link https://github.com/eliseekn/tinymvc
*/

/**
* Custom routes paths configuration
*/

return [
'paths' => [],
];
2 changes: 1 addition & 1 deletion core/Console/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class Cache extends Command
{
protected static $defaultName = 'twig:clear';
protected static $defaultName = 'clear:twig';

protected function configure()
{
Expand Down
2 changes: 1 addition & 1 deletion core/Console/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class Logs extends Command
{
protected static $defaultName = 'logs:clear';
protected static $defaultName = 'clear:logs';

protected function configure()
{
Expand Down
5 changes: 2 additions & 3 deletions core/Console/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class Routes extends Command
{
protected static $defaultName = 'routes:list';
protected static $defaultName = 'routes';

protected function configure()
{
Expand All @@ -24,7 +24,6 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$rows = [];

$routes = Route::$routes;

foreach ($routes as $route => $options) {
Expand All @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion core/Console/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class Server extends Command
{
protected static $defaultName = 'server:start';
protected static $defaultName = 'serve';

protected function configure()
{
Expand Down
2 changes: 1 addition & 1 deletion core/Console/Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class Tests extends Command
{
protected static $defaultName = 'tests:run';
protected static $defaultName = 'test';

protected function configure()
{
Expand Down
105 changes: 72 additions & 33 deletions core/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
}
33 changes: 25 additions & 8 deletions core/Support/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Core\Support;

use ErrorException;

/**
* Manage files and folders
*/
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand Down
4 changes: 2 additions & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
* API routes
*/

Route::groupPrefix('api', function () {
Route::group(function () {
//
})->register();
})->byPrefix('api')->register();
54 changes: 30 additions & 24 deletions routes/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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();
Loading

0 comments on commit 5c82b9e

Please sign in to comment.