Skip to content

Completed middleware and API controller 📌 #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@

## Tue, July 02 2024

- Added `asset()` function in `Functions.php` file, and can distinguish to start path from relative path or root path
- `RouteController.php` in `slides_include()` function.
- Removed `::view` and `::root` string and it's functions.
- Added `__ROOT__` constant
- Updated slides_include file to auto clone a file to a generated file.
- Can now use normal relative path in `slides_include()` function.
- Improved in Route Map function, can now use `name()` method in any position.
- Updated Request URL to match both uppercase and lowercase
- Renamed `/routes/route.php` to `/routes/web.php`
- Added named route function to normal route method `POST`, `GET`, `PATCH`, `PUT`, `DELETE`, `VIEW`.
- Added `asset()` function in `Functions.php` file, and can distinguish to start path from relative path or root path
- `RouteController.php` in `slides_include()` function.
- Removed `::view` and `::root` string and it's functions.
- Added `__ROOT__` constant
- Updated slides_include file to auto clone a file to a generated file.
- Can now use normal relative path in `slides_include()` function.
- Improved in Route Map function, can now use `name()` method in any position.
- Updated Request URL to match both uppercase and lowercase
- Renamed `/routes/route.php` to `/routes/web.php`
- Added named route function to normal route method `POST`, `GET`, `PATCH`, `PUT`, `DELETE`, `VIEW`.

## Tue, July 09 2024

- Change all file names to CamelCase
- Added configuration for Console
- Added Console template for Controller, ApiController, Middleware.
- Change all file names to CamelCase
- Added configuration for Console
- Added Console template for Controller, ApiController, Middleware.

## Thursday, July 11 2024

- Added Authorization method for getting Basic and Bearer token.
- Added Authorization method for getting Basic and Bearer token.

## Saturday, July 13 2024

- Completed API Controller function.
- Completed middleware function.
- Completed API Controller function.
- Completed middleware function.

## Tuesday, July 16 2024

- Worked on controller and Middleware
- Added mapping method in Api controller
- Api controller now accepts naming route in mapping method.

## Friday, July 19 2024

- Make API `define()` method now working with `route()` method
- Work on `route()` so when using `define()` with `route()` they can pass second parameter in `route()` as controller method for the defined route
- Added more methods to `Request` class
- Added documentation to each methods in Request class and interface with Api class and interface
- Added more version methods API class manually.
- Added more version methods to API interface manually.
254 changes: 3 additions & 251 deletions app/PhpSlides.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
namespace PhpSlides;

use Exception;
use PhpSlides\Http\Request;
use PhpSlides\Route\MapRoute;
use PhpSlides\Resources\Resources;
use PhpSlides\Controller\Controller;
use PhpSlides\Interface\RouteInterface;
use PhpSlides\Interface\MiddlewareInterface;

/**
* -------------------------------------------------------------------------------
Expand All @@ -42,7 +41,7 @@
* -------------------------------------------------------------------------------
*/

final class Route extends Controller implements RouteInterface
final class Route extends Resources implements RouteInterface
{
/**
* `$log` method prints logs in `.log` file in the root of the project each time any request has been received, when setted to true.
Expand All @@ -63,33 +62,10 @@ final class Route extends Controller implements RouteInterface
*/
public static string $root_dir;

/**
* Get's all full request URL
*
* @static $root_dir
* @var string $root_dir
* @return string
*/
public static string $request_uri;

private static array|bool $map_info = false;

private static array $routes;

private static array $route;

private static mixed $action = null;

private static ?array $middleware = null;

private static ?array $method = null;

private static ?array $view = null;

private static ?string $use = null;

private static ?string $file = null;

/**
* Call all static methods
* and initialize them
Expand Down Expand Up @@ -656,51 +632,6 @@ public function action (mixed $callback): self
return $this;
}

private function __action (): void
{
$action = self::$action;

if (array_key_exists('params', self::$map_info))
{
$GLOBALS['params'] = self::$map_info['params'];
$params_value = self::$map_info['params_value'];

if (is_callable($action))
{
$a = $action(...$params_value);
print_r($a);
}
elseif (preg_match('/(?=.*Controller)(?=.*::)/', $action))
{
self::$use = $action;
$this->__use();
}
else
{
print_r($action);
}
}
else
{
if (is_callable($action))
{
print_r($action());
}
elseif (preg_match('/(?=.*Controller)(?=.*::)/', $action))
{
self::$use = $action;
$this->__use();
}
else
{
print_r($action);
}
}

self::log();
exit();
}

/**
* Controller method
* Work with map controller route
Expand All @@ -717,43 +648,6 @@ public function use(string $controller): self
return $this;
}

private function __use (): void
{
$controller = self::$use;

if (!preg_match('/(?=.*Controller)(?=.*::)/', $controller))
{
exit('invalid parameter $controller Controller type');
}

[ $c_name, $c_method ] = explode('::', $controller);

$cc = 'PhpSlides\\Controller\\' . $c_name;

if (class_exists($cc))
{
if (array_key_exists('params', self::$map_info))
{
$GLOBALS['params'] = self::$map_info['params'];
$params_value = self::$map_info['params_value'];

$cc = new $cc();
print_r($cc->$c_method(...$params_value));
}
else
{
$cc = new $cc();
print_r($cc->$c_method());
}
}
else
{
echo "No class controller found as: `$cc`";
}

self::log();
exit();
}

/**
* file method
Expand All @@ -770,19 +664,6 @@ public function file (string $file): self
return $this;
}

private function __file (): void
{
$file = self::$file;

if (array_key_exists('params', self::$map_info))
{
$GLOBALS['params'] = self::$map_info['params'];

print_r(view::render($file));
self::log();
exit();
}
}

/**
* Middleware
Expand All @@ -796,81 +677,6 @@ public function middleware (array $middleware): self
return $this;
}

private function __middleware (): void
{
$use = self::$use;
$file = self::$use;
$action = self::$action;

$view = self::$view;
$method = self::$method;
$middleware = self::$middleware ?? [];

$params = self::$map_info['params'] ?? null;
$request = new Request($params);

for ($i = 0; $i < count((array) $middleware); $i++)
{
include_once dirname(__DIR__) . '/configs/middlewares.php';

if (array_key_exists($middleware[$i], $middlewares))
{
$middleware = $middlewares[$middleware[$i]];
}
else
{
throw new Exception('No Registered Middleware as `' . $middleware[$i] . '`');
}

if (!class_exists($middleware))
{
throw new Exception(
"Middleware class does not exist: `{$middleware}`"
);
}
$mw = new $middleware();

if ($mw instanceof MiddlewareInterface)
{
$next = function () use ($use, $file, $action, $view, $method)
{
if ($use !== null)
{
self::__use();
}
elseif ($file !== null)
{
self::__file();
}
elseif ($action !== null)
{
self::__action();
}
elseif ($view !== null)
{
self::__view();
}
elseif ($method !== null)
{
self::__method();
}
else
{
throw new Exception('Cannot use middleware with this method');
}
};

$mw->handle($request, $next);
}
else
{
throw new Exception(
'Middleware class must implements `MiddlewareInterface`'
);
}
}
}

/**
* ---------------------------------------------------------------------------
*
Expand All @@ -897,51 +703,6 @@ public static function view (array|string $route, string $view): self
return new self();
}

private static function __view (): void
{
$route = self::$view['route'];
$view = self::$view['view'];

/**
* ----------------------------------------
* | Replacing first and last forward slashes
* | $_REQUEST['uri'] will be empty if req uri is /
* ----------------------------------------
*/
$uri = [];
$str_route = '';
$reqUri = strtolower(
preg_replace("/(^\/)|(\/$)/", '', self::$request_uri)
);

if (is_array($route))
{
for ($i = 0; $i < count($route); $i++)
{
$each_route = preg_replace("/(^\/)|(\/$)/", '', $route[$i]);
array_push($uri, strtolower($each_route));
}
}
else
{
$str_route = strtolower(preg_replace("/(^\/)|(\/$)/", '', $route));
}

if (in_array($reqUri, $uri) || $reqUri === $str_route)
{
if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'GET')
{
http_response_code(405);
self::log();
exit('Method Not Allowed');
}

// render view page to browser
print_r(view::render($view));
self::log();
exit();
}
}

/**
* --------------------------------------------------------------
Expand Down Expand Up @@ -1086,15 +847,6 @@ public static function delete (array|string $route, $callback): self
return new self();
}

private static function __method (): void
{
$route = self::$method['route'];
$method = self::$method['method'];
$callback = self::$method['callback'];

self::any($route, $callback, $method);
}

public function __destruct ()
{
if (self::$middleware !== null)
Expand Down Expand Up @@ -1189,4 +941,4 @@ final public static function render (string $view): mixed
exit($e->getMessage());
}
}
}
}
Loading