Skip to content

Commit

Permalink
Merge pull request #22 from PhpSlides/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dconco authored Aug 25, 2024
2 parents ca8ad8c + 342ee2b commit e35b1dc
Show file tree
Hide file tree
Showing 28 changed files with 1,026 additions and 1,123 deletions.
28 changes: 9 additions & 19 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
# Change Logs

## [v1.2.6]

### [Tue, 20-08-2024]

- Fixes bugs on `Request`
- Fixes bugs on `JWT` and `payload()` function.

## [v1.2.7]

### [Wed, 21-08-2024]

- Update API `route` method to allow a particular request method and a particular controller method

## [v1.2.8]

### [Thu, 22-08-2024]

- Updated `Render` getting value from array according to strict types
- Changed API controller namespace to `App\Controller\Api`
## [v1.2.9]

- Added Authorization Guard.
- Implemented AuthGuard to Routes
- Implemented AuthGuard to APIs
- Middleware is now deprecated and removed
- Added AuthGuard to Console
- Updated ApiController and no more interface, now an abstract class.
-
8 changes: 4 additions & 4 deletions Console/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public function __construct(array $argv)
self::$commands = ['api-controller', $arguments];
break;

case 'make:middleware':
case 'make:auth-guard':
if (count($arguments) < 1) {
exit(
"<name> argument is required! Type --help for list of commands\n"
);
}
self::$commands = ['middleware', $arguments];
self::$commands = ['auth-guard', $arguments];
break;

case 'generate:secret-key':
Expand Down Expand Up @@ -111,8 +111,8 @@ public function __destruct()
case 'api-controller':
self::makeApiController(self::$commands[1], self::$resolve);
break;
case 'middleware':
self::makeMiddleware(self::$commands[1], self::$resolve);
case 'auth-guard':
self::makeAuthGuard(self::$commands[1], self::$resolve);
break;
case 'secret-key':
self::generateSecretKey(self::$commands[1]);
Expand Down
8 changes: 8 additions & 0 deletions Router/Interface/RouteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public function use(string $controller): self;
*/
public function file(string $file): self;

/**
* Applies Authentication Guard to the current route.
*
* @param string ...$guards String parameters of registered guards.
* @return self
*/
public function withGuard(string ...$guards): self;

/**
* ---------------------------------------------------------------------------
*
Expand Down
50 changes: 26 additions & 24 deletions Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ class Route extends Controller implements RouteInterface
*/
public static string $root_dir;

private static array $routes;
private ?array $guards = null;

private static array $route;
private mixed $action = null;

private static mixed $action = null;
private ?string $use = null;

private static ?array $middleware = null;
private ?string $file = null;

private static array $routes;

private static array $route;

private static ?array $redirect = null;

Expand All @@ -78,10 +82,6 @@ class Route extends Controller implements RouteInterface

private static ?array $view = null;

private static ?string $use = null;

private static ?string $file = null;

private static ?array $map = null;

/**
Expand Down Expand Up @@ -343,7 +343,7 @@ public function name(string $name): self
public function action(mixed $callback): self
{
if (self::$map) {
self::$action = $callback;
$this->action = $callback;
}
return $this;
}
Expand All @@ -358,7 +358,7 @@ public function action(mixed $callback): self
public function use(string $controller): self
{
if (self::$map) {
self::$use = $controller;
$this->use = $controller;
}
return $this;
}
Expand All @@ -372,19 +372,21 @@ public function use(string $controller): self
public function file(string $file): self
{
if (self::$map) {
self::$file = $file;
$this->file = $file;
}
return $this;
}

/**
* Add Middlewares.
* Middleware must be verified before redirecting to the specific Controller.
* Applies Authentication Guard to the current route.
*
* @param string ...$guards String parameters of registered guards.
* @return self
*/
public function middleware(array $middleware): self
public function withGuard(string ...$guards): self
{
if (self::$map || self::$method || self::$view) {
self::$middleware = $middleware;
$this->guards = $guards;
}
return $this;
}
Expand Down Expand Up @@ -557,31 +559,31 @@ public function __destruct()
$GLOBALS['__registered_routes'][$route_index]['map'] = self::$map;
}

if (self::$middleware !== null) {
$GLOBALS['__registered_routes'][$route_index]['middleware'] =
self::$middleware;
if ($this->guards !== null) {
$GLOBALS['__registered_routes'][$route_index]['guards'] =
$this->guards;
}

if (self::$redirect !== null) {
$GLOBALS['__registered_routes'][$route_index]['redirect'] =
self::$redirect;
}

if (self::$action !== null) {
if ($this->action !== null) {
$GLOBALS['__registered_routes'][$route_index]['action'] =
self::$action;
$this->action;
}

if (self::$any !== null) {
$GLOBALS['__registered_routes'][$route_index]['any'] = self::$any;
}

if (self::$use !== null) {
$GLOBALS['__registered_routes'][$route_index]['use'] = self::$use;
if ($this->use !== null) {
$GLOBALS['__registered_routes'][$route_index]['use'] = $this->use;
}

if (self::$file !== null) {
$GLOBALS['__registered_routes'][$route_index]['file'] = self::$file;
if ($this->file !== null) {
$GLOBALS['__registered_routes'][$route_index]['file'] = $this->file;
}

if (self::$method !== null) {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"firebase/php-jwt": "^6.10"
},
"suggest": {
"phpslides/status": "^0.0.1"
"phpslides/status": "^0.0.3"
},
"require-dev": {
"phpunit/phpunit": "^11.2"
Expand Down
36 changes: 18 additions & 18 deletions src/Config/cors.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@
use PhpSlides\Loader\FileLoader;
use PhpSlides\Foundation\Application;

$cors = (new FileLoader())
->safeLoad(Application::$configsDir . 'cors.php')
->getLoad() ?: [];
$cors =
(new FileLoader())
->safeLoad(Application::$configsDir . 'cors.php')
->getLoad() ?:
[];

foreach ($cors as $key => $value)
{
foreach ($cors as $key => $value) {
$key = 'Access-Control-' . str_replace('_', '-', ucwords($key, '_'));
$value = is_array($value) ? implode(', ', $value) : $value;

$header_value =
$key . ': ' . (is_bool($value) ? var_export($value, true) : $value);
$key . ': ' . (is_bool($value) ? var_export($value, true) : $value);
header($header_value);
}

/**
* Handle preflight requests (OPTIONS method)
*/
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS')
{
new Log();
http_response_code(200);
exit;
}

class Log
{
use Logger;
public function __construct ()
public function __construct()
{
self::log();
}
}
}

/**
* Handle preflight requests (OPTIONS method)
*/
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
new Log();
http_response_code(200);
exit();
}
2 changes: 1 addition & 1 deletion src/Config/middleware.php → src/Config/guards.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
use PhpSlides\Foundation\Application;

return (new FileLoader())
->safeLoad(Application::$configsDir . 'middleware.php')
->safeLoad(Application::$configsDir . 'guards.php')
->getLoad() ?: [];
Loading

0 comments on commit e35b1dc

Please sign in to comment.