Skip to content

Commit

Permalink
Add named routes
Browse files Browse the repository at this point in the history
  • Loading branch information
marriosdev committed Apr 25, 2024
1 parent 4e0a5fa commit 9110361
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 66 deletions.
31 changes: 24 additions & 7 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,65 @@ class Controller
private String $classController;
private Mixed $controller;

/**
*
*/
public function __construct(String $classController)
{
$this->classController = $classController;
}

/**
*
*/
private function build() : void
{
$reflectionControllerInstance = new ReflectionClass($this->classController);
$this->controller = $this->recursiveDependenciesBuild($reflectionControllerInstance);
}

public function runMethod(String $method, RouteParameters $routeParams) : mixed
/**
*
*/
public function runMethod(String $method, Request $request) : mixed
{
if($this->isValidMethod($method) == false) {
throw new MareaTurboException("Method not found");
}
$this->build();
return $this->controller->$method($routeParams);
return $this->controller->$method($request);
}

/**
*
*/
private function isValidMethod(String $method, ) : bool
{
return method_exists($this->classController, $method);
}

/**
*
*/
private function recursiveDependenciesBuild(ReflectionClass $reflectionControllerInstance) : mixed
{
if($reflectionControllerInstance->getConstructor() == null) {
return $reflectionControllerInstance->newInstance();
}

$dependencies = $reflectionControllerInstance->getConstructor()->getParameters();
if($dependencies == null) {
return $reflectionControllerInstance->newInstance();
}

$arguments = array();
foreach($dependencies as $dependency){
$dependencyClassName = $dependency->getType()->getName();
$dependencyReflectionClass = new ReflectionClass($dependencyClassName);
$arguments[] = $this->recursiveDependenciesBuild($dependencyReflectionClass);
if(class_exists($dependency->getType()->getName())) {
$dependencyClassName = $dependency->getType()->getName();
$dependencyReflectionClass = new ReflectionClass($dependencyClassName);
$arguments[] = $this->recursiveDependenciesBuild($dependencyReflectionClass);
}
}
return $reflectionControllerInstance->newInstanceArgs($arguments);
}
}
}
37 changes: 37 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace MareaTurbo;

use AllowDynamicProperties;

#[AllowDynamicProperties]
class Request
{
/**
*
*/
public function __construct(\MareaTurbo\Route $route)
{
$this->route = $route;
}

/**
*
*/
public function all()
{
return get_object_vars($this);
}

/**
*
*/
public function only(array $keys = [])
{
return array_filter($keys, function ($key) {
return $this->$key;
});
}
}
55 changes: 25 additions & 30 deletions src/Route.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace MareaTurbo;
Expand All @@ -11,56 +12,50 @@ class Route
{
public String $path;
public HttpMethod $method;
public string $name;

public function __construct(String $path, String $method) {
/**
*
*/
public function __construct(String $path, String $method, string $name = '')
{
$this->path = $path;
$this->method = new HttpMethod($method);
$this->name = $name;
}

public function isMatch(Route $route) : bool
/**
*
*/
public function isMatch(Route $route): bool
{
$currentRoute = $this->routeStringToArray($this->path);
$accessedRoute = $this->routeStringToArray($route->path);
if($route->method->name != $this->method->name) {

if ($route->method->name != $this->method->name) {
return false;
}

if(count($accessedRoute) != count($currentRoute)) {
if (count($accessedRoute) != count($currentRoute)) {
return false;
}

for($i = 0; $i < count($accessedRoute); $i++) {
if($accessedRoute[$i] != $currentRoute[$i]) {
if(strpos($currentRoute[$i], '{') !== false) {
continue;
foreach ($currentRoute as $key => $value) {
if ($value != $accessedRoute[$key]) {
if (substr($value, 0, 1) != "{" || substr($value, -1) != "}") {
return false;
}
return false;
}
}
return true;
}

public function getDynamicParameters(Route $acessedRoute) : RouteParameters
{
$currentRoute = $this->routeStringToArray($this->path);
$accessedRoute = $this->routeStringToArray($acessedRoute->path);

$parameters = new RouteParameters();

for($i=0; $i < count($accessedRoute); $i++) {
if($accessedRoute[$i] != $currentRoute[$i]) {
if(strpos($currentRoute[$i], '{') !== false) {
$parameters->{str_replace(['{', '}'], '', $currentRoute[$i])} = $accessedRoute[$i];
}
}
}
return $parameters;
}

public function routeStringToArray(string $string) : array

/**
*
*/
public function routeStringToArray(string $string): array
{
if(substr($string, -1) == "/") {
if (substr($string, -1) == "/") {
$string = substr($string, 0, -1);
}
return explode('/', $string);
Expand Down
10 changes: 0 additions & 10 deletions src/RouteParameters.php

This file was deleted.

45 changes: 26 additions & 19 deletions src/Router.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace MareaTurbo;

use MareaTurbo\Controller;
use ReflectionClass;
use ReflectionMethod;

Expand All @@ -23,43 +24,43 @@ public function middleware(String $middleware, $httpStatusCode)
$middlewareInstance = $reflectionClass->newInstance();
if (!$middlewareInstance->handle()) {
http_response_code($httpStatusCode);
exit;
exit;
}
return $this;
}

private function run()

/**
*
*/
private function run(): void
{
foreach($this->controllers as $controller) {
foreach ($this->controllers as $controller) {
$reflectionController = new ReflectionClass($controller);
foreach($reflectionController->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
foreach ($reflectionController->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
$this->getRouteAttribute($method, $controller);
}
}

http_response_code(404);
echo "Route not found";
}

private function getRouteAttribute($method, $controller)
{
if($method->getAttributes()) {
foreach($method->getAttributes() as $route) {
if ($method->getAttributes()) {
foreach ($method->getAttributes() as $route) {
$routeInstance = $route->newInstance();
if($routeInstance->isMatch($this->getAccessedRoute())) {
if ($routeInstance->isMatch($this->getAccessedRoute())) {
return $this->invokeController($method, $controller, $routeInstance);
}
}
}
}

private function invokeController($method, $controller, $routeInstance)
private function invokeController($method, $controller, $route)
{
(new Controller($controller))->runMethod(
$method->getName(),
$routeInstance->getDynamicParameters($this->getAccessedRoute())
(new \MareaTurbo\Controller($controller))->runMethod(
$method->getName(),
(new \MareaTurbo\Request($route))
);

$this->endLifeCycle();
}

Expand All @@ -73,7 +74,13 @@ private function getAccessedRoute()
global $argv;
$route = $this->isCli() ? $argv[2] : explode("?", $_SERVER['REQUEST_URI'])[0];
$method = $this->isCli() ? $argv[1] : explode("?", $_SERVER['REQUEST_METHOD'])[0];
return new Route($route, $method);
return new Route($route, $method);
}

private function getUri(): string
{
global $argv;
return $this->isCli() ? $argv[2] : explode("?", $_SERVER['REQUEST_URI'])[0];
}

private function endLifeCycle()
Expand Down

0 comments on commit 9110361

Please sign in to comment.