Skip to content

Commit

Permalink
Add routes attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseekn committed Feb 27, 2024
1 parent 0c2e040 commit 62d902b
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 63 deletions.
5 changes: 4 additions & 1 deletion app/Http/Controllers/Auth/EmailVerificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Http\UseCases\User\UpdateUseCase;
use App\Mails\VerificationMail;
use App\Mails\WelcomeMail;
use Core\Routing\Attributes\Route;
use Core\Routing\Controller;
use Core\Support\Alert;

Expand All @@ -21,7 +22,8 @@
*/
class EmailVerificationController extends Controller
{
public function notify(): void
#[Route('GET', '/email/notify')]
public function notify(): void
{
$tokenValue = generate_token(15);
$token = Token::findByDescription($this->request->queries('email'), TokenDescription::EMAIL_VERIFICATION_TOKEN->value);
Expand All @@ -46,6 +48,7 @@ public function notify(): void
$this->render('auth.login');
}

#[Route('GET', '/email/verify')]
public function verify(UpdateUseCase $useCase): void
{
if (!$this->request->hasQuery(['email', 'token'])) {
Expand Down
8 changes: 6 additions & 2 deletions app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Http\UseCases\User\UpdateUseCase;
use App\Http\Validators\Auth\LoginValidator;
use App\Mails\TokenMail;
use Core\Routing\Attributes\Route;
use Core\Routing\Controller;
use Core\Support\Alert;

Expand All @@ -21,6 +22,7 @@
*/
class ForgotPasswordController extends Controller
{
#[Route('POST', '/password/notify', ['csrf'])]
public function notify(): void
{
$tokenValue = generate_token(15);
Expand All @@ -45,7 +47,8 @@ public function notify(): void

$this->redirectBack();
}


#[Route('GET', '/password/reset')]
public function reset(): void
{
if (!$this->request->hasQuery(['email', 'token'])) {
Expand All @@ -65,7 +68,8 @@ public function reset(): void
$token->delete();
$this->render('auth.password.new', ['email' => $this->request->queries('email')]);
}


#[Route('POST', '/password/update', ['csrf'])]
public function update(UpdateUseCase $useCase): void
{
$validated = $this->validate(new LoginValidator());
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
namespace App\Http\Controllers\Auth;

use App\Http\Validators\Auth\LoginValidator;
use Core\Routing\Attributes\Route;
use Core\Routing\Controller;
use Core\Support\Alert;
use Core\Support\Auth;

class LoginController extends Controller
{
{
#[Route('GET', '/login', ['remember'])]
public function index(): void
{
if (!Auth::check($this->request)) {
Expand All @@ -24,6 +26,7 @@ public function index(): void
$this->redirectUrl(config('app.home'));
}

#[Route(methods: 'POST', middlewares: ['csrf'])]
public function authenticate(): void
{
$this->validate(new LoginValidator());
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Auth/LogoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

namespace App\Http\Controllers\Auth;

use Core\Routing\Attributes\Route;
use Core\Routing\Controller;
use Core\Support\Alert;
use Core\Support\Auth;

class LogoutController extends Controller
{
#[Route('POST', '/logout', ['auth'])]
public function __invoke(): void
{
Auth::forget();
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
use App\Events\UserRegistered\UserRegisteredEvent;
use App\Http\UseCases\User\StoreUseCase;
use App\Http\Validators\Auth\RegisterValidator;
use Core\Routing\Attributes\Route;
use Core\Routing\Controller;
use Core\Support\Alert;
use Core\Support\Auth;

class RegisterController extends Controller
{
#[Route('GET', '/signup', ['remember'])]
public function index(): void
{
if (!Auth::check($this->request)) {
Expand All @@ -26,6 +28,7 @@ public function index(): void
$this->redirectUrl(config('app.home'));
}

#[Route(methods: 'POST', middlewares: ['csrf'])]
public function register(StoreUseCase $useCase): void
{
$validated = $this->validate(new RegisterValidator());
Expand Down
16 changes: 16 additions & 0 deletions core/Routing/Attributes/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Core\Routing\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_METHOD)]
class Route
{
public function __construct(
public string $methods,
public ?string $uri = null,
public ?array $middlewares = null,
public ?string $name = null
) {}
}
58 changes: 47 additions & 11 deletions core/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Core\Exceptions\RoutesPathsNotDefinedException;
use Core\Support\Storage;
use Core\Http\Response;
use ReflectionClass;
use ReflectionMethod;
use Spatie\StructureDiscoverer\Discover;

/**
* Manage routes
Expand Down Expand Up @@ -214,25 +217,58 @@ public static function getRoutes(): array
return self::$routes;
}

protected static function loadFromAttributes(): void
{
$controllers = Discover::in(config('storage.controllers'))->classes()->get();

foreach ($controllers as $controller) {
$reflectionClass = new ReflectionClass($controller);
$methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);

foreach ($methods as $method) {
$attributes = $method->getAttributes(Attributes\Route::class);

foreach ($attributes as $attribute) {
$attribute = $attribute->newInstance();
$route = self::match($attribute->methods, $attribute->uri ?? $method->getName(), [$controller, $method->getName()]);

if ($attribute->middlewares) {
$route->middleware($attribute->middlewares);
}

if ($attribute->name) {
$route->name($attribute->name);
}

$route->register();
}
}
}
}

public static function load(): void
{
if (empty(config('routes.paths'))) {
self::loadFromAttributes();

if (empty(config('routes.paths')) && empty(self::$routes)) {
throw new RoutesPathsNotDefinedException();
}

$paths = array_map(function ($path) {
$path = $path === '/'
? config('storage.routes')
: Storage::path(config('storage.routes'))->addPath($path)->getPath();
if (!empty(config('routes.paths'))) {
$paths = array_map(function ($path) {
$path = $path === '/'
? config('storage.routes')
: Storage::path(config('storage.routes'))->addPath($path)->getPath();

return str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $path);
}, config('routes.paths'));
return str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $path);
}, config('routes.paths'));

foreach ($paths as $path) {
$routes = Storage::path($path)->addPath('')->getFiles();
foreach ($paths as $path) {
$routes = Storage::path($path)->addPath('')->getFiles();

foreach ($routes as $route) {
require_once $path . $route;
foreach ($routes as $route) {
require_once $path . $route;
}
}
}
}
Expand Down
49 changes: 1 addition & 48 deletions routes/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,10 @@
* @link https://github.com/eliseekn/tinymvc
*/

use Core\Http\Request;
use Core\Routing\Route;
use Core\Http\Response;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\LogoutController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\EmailVerificationController;

/**
* Authentication routes
*/

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::get('/reset', [ForgotPasswordController::class, 'reset']);
Route::view('/forgot', 'auth.password.forgot');
})
->byPrefix('password')
->register();

Route::group(function () {
Route::post('/notify', 'notify');
Route::post('/update', 'update');
})
->byPrefix('password')
->byController(ForgotPasswordController::class)
->byMiddleware('csrf')
->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();
Route::view('/password/forgot', 'auth.password.forgot')->register();

0 comments on commit 62d902b

Please sign in to comment.