Skip to content

Dev #64

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

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Dev #64

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3201a9c
Refactor namespaces and update workflow configurations
dconco Dec 25, 2024
01f59c9
Refactor namespaces from PhpSlides\Src to PhpSlides\Core across multi…
dconco Dec 25, 2024
4fcf412
Update composer.json to refactor namespace from PhpSlides\Src to PhpS…
dconco Dec 25, 2024
1ce2f12
Update GitHub Actions workflow to merge 'dev' into 'main' instead of …
dconco Dec 25, 2024
da776ab
Update GitHub Actions workflow to use 'git branch -M' for renaming ma…
dconco Dec 25, 2024
3e81ec9
Update GitHub Actions workflow to merge from origin/dev instead of dev
dconco Dec 25, 2024
28bfe12
Refactor GitHub Actions workflow for main branch handling and enhance…
dconco Dec 25, 2024
06cdf4c
ss
dconco Dec 25, 2024
8f3750f
Refactor GitHub Actions workflow to streamline main branch updates fr…
dconco Dec 25, 2024
05c6794
Enhance type validation in StrictTypes and InvalidTypesException; imp…
dconco Dec 25, 2024
5f6e7f3
Add INT range validation in StrictTypes; update route parameter mappi…
dconco Dec 25, 2024
8191cc6
updated and fixes error on how URL parameter type works
dconco Dec 25, 2024
807596c
implemented case insensitive function for route
dconco Dec 26, 2024
1e278dc
added STRING<> strict type for declaring specific length
dconco Dec 26, 2024
9da29f0
added handleInvalidParameterType() and caseSensitive() function to ap…
dconco Dec 27, 2024
b303513
updated
dconco Jan 1, 2025
d3267c3
updated files & folders structure
dconco Jan 6, 2025
fa88dbb
updated namespace
dconco Jan 6, 2025
6723504
updated use database
dconco Jan 17, 2025
c1ffd72
completed the route map implementation
dconco Jan 17, 2025
fb92ce2
worked onon csrf typo
dconco Jan 20, 2025
f23aa01
Update ViewLoader.php
dconco Feb 21, 2025
fd7c26f
Update Functions.php
dconco Feb 21, 2025
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
Empty file removed .env
Empty file.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Configure and Run Tests Before Release
name: Release

on:
push:
Expand Down
11 changes: 4 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Execute Tests
name: Tests

on:
push:
Expand Down Expand Up @@ -63,11 +63,8 @@ jobs:

- name: Push changes to main
run: |
git fetch origin main dev
git checkout main
git config pull.rebase false
git pull origin main
git merge origin/dev --allow-unrelated-histories --no-ff
git push origin main
git checkout -b main
git reset --hard dev
git push -u -f origin main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44 changes: 31 additions & 13 deletions Router/MapRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace PhpSlides\Router;

use PhpSlides\Src\Controller\Controller;
use PhpSlides\Src\Foundation\Application;
use PhpSlides\Core\Controller\Controller;
use PhpSlides\Core\Foundation\Application;
use PhpSlides\Router\Interface\MapInterface;

/**
Expand All @@ -18,8 +18,8 @@
*/
class MapRoute extends Controller implements MapInterface
{
use \PhpSlides\Src\Utils\Validate;
use \PhpSlides\Src\Utils\Routes\StrictTypes;
use \PhpSlides\Core\Utils\Validate;
use \PhpSlides\Core\Utils\Routes\StrictTypes;

/**
* @var string|array $route The route(s) to be mapped.
Expand Down Expand Up @@ -64,14 +64,13 @@ public function match(string $method, string|array $route): bool|array
* | $_REQUEST['uri'] will be empty if req uri is /
* ----------------------------------------------
*/
self::$request_uri = strtolower(
preg_replace("/(^\/)|(\/$)/", '', Application::$request_uri),
);
self::$request_uri =
preg_replace("/(^\/)|(\/$)/", '', Application::$request_uri);
self::$request_uri = empty(self::$request_uri) ? '/' : self::$request_uri;

self::$route = is_array($route)
? $route
: strtolower(preg_replace("/(^\/)|(\/$)/", '', $route));
: preg_replace("/(^\/)|(\/$)/", '', $route);

// Firstly, resolve route with pattern
if (is_array(self::$route))
Expand Down Expand Up @@ -191,9 +190,15 @@ public function match(string $method, string|array $route): bool|array
* -----------------------------------
*/
$reqUri = str_replace('/', '\\/', $reqUri);
$route = self::$route;

if (Application::$caseInSensitive === true) {
$reqUri = strtolower($reqUri);
$route = strtolower($route);
}

// now matching route with regex
if (preg_match("/$reqUri/", self::$route . '$'))
if (preg_match("/$reqUri/", $route . '$'))
{
// checks if the requested method is of the given route
if (
Expand Down Expand Up @@ -247,6 +252,7 @@ private function match_routing (): bool|array
{
$uri = [];
$str_route = '';
$request_uri = self::$request_uri;

if (is_array(self::$route))
{
Expand All @@ -255,14 +261,20 @@ private function match_routing (): bool|array
$each_route = preg_replace("/(^\/)|(\/$)/", '', self::$route[$i]);

empty($each_route)
? array_push($uri, strtolower('/'))
: array_push($uri, strtolower($each_route));
? array_push($uri, '/')
: array_push($uri, Application::$caseInSensitive === true ? strtolower($each_route) : $each_route);
}
}
else
{
$str_route = empty(self::$route) ? '/' : self::$route;
}


if (Application::$caseInSensitive === true) {
$request_uri = strtolower($request_uri);
$str_route = strtolower($str_route);
}

if (
in_array(self::$request_uri, $uri) ||
Expand Down Expand Up @@ -329,9 +341,15 @@ private function pattern (): array|bool
*/
private function validatePattern (string $pattern): array|bool
{
$request_uri = self::$request_uri;
$pattern = preg_replace("/(^\/)|(\/$)/", '', trim(substr($pattern, 8)));

if (fnmatch($pattern, self::$request_uri))
if (Application::$caseInSensitive === true) {
$request_uri = strtolower($request_uri);
$pattern = strtolower($pattern);
}

if (fnmatch($pattern, $request_uri))
{
if (
!in_array($_SERVER['REQUEST_METHOD'], self::$method) &&
Expand All @@ -349,4 +367,4 @@ private function validatePattern (string $pattern): array|bool
}
return false;
}
}
}
21 changes: 16 additions & 5 deletions Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

use Closure;
use PhpSlides\Exception;
use PhpSlides\Src\Traits\FileHandler;
use PhpSlides\Src\Controller\Controller;
use PhpSlides\Core\Traits\FileHandler;
use PhpSlides\Core\Controller\Controller;
use PhpSlides\Router\Interface\RouteInterface;

/**
Expand Down Expand Up @@ -51,6 +51,8 @@ class Route extends Controller implements RouteInterface

private ?array $mapRoute = null;

private bool $caseSensitive = false;

private ?Closure $handleInvalidParameterType = null;

private static array $routes;
Expand Down Expand Up @@ -140,14 +142,14 @@ public function name(string $name): self
* @param string $route
* @param Closure $callback
*/
public function route(string $route, Closure $callback): self
public function route(string $route, callable $callback): self
{
$route = rtrim('/', self::$map['route']) . '/' . ltrim('/', $route);
$route = rtrim(self::$map['route'], '/') . '/' . ltrim($route, '/');

if (self::$map) {
$this->mapRoute = [
'route' => $route,
'method' => self::$map['method'],
'method' => '*',
'callback' => $callback,
];
} else {
Expand Down Expand Up @@ -215,6 +217,12 @@ public function handleInvalidParameterType(Closure $closure): self
return $this;
}

public function caseSensitive(): self
{
$this->caseSensitive = true;
return $this;
}

/**
* Applies Authentication Guard to the current route.
*
Expand Down Expand Up @@ -393,6 +401,9 @@ public function __destruct()
$route_index = end(self::$route);
$route_index = is_array($route_index) ? $route_index[0] : $route_index;

$GLOBALS['__registered_routes'][$route_index]['caseSensitive'] =
$this->caseSensitive;

if (self::$map !== null) {
$GLOBALS['__registered_routes'][$route_index]['map'] = self::$map;
}
Expand Down
8 changes: 4 additions & 4 deletions Router/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace PhpSlides\Router;

use component;
use PhpSlides\Src\Foundation\Application;
use function component;
use PhpSlides\Core\Foundation\Application;

/**
* --------------------------------------------------------------
Expand Down Expand Up @@ -31,11 +31,11 @@ final class view
*
* --------------------------------------------------------------
*/
final public static function render(string $view, mixed ...$props): mixed
final public static function render (string $view, mixed ...$props): mixed
{
// split :: into array and extract the folder and files
$file = preg_replace('/(::)|::/', '/', $view);
$file = strtolower(trim($file, '\/\/'));
$file = trim($file, '\/\/');
$file_uri = Application::$viewsDir . $file;
header('Content-Type: text/html');

Expand Down
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"homepage": "https://github.com/PhpSlides",
"type": "library",
"license": "MIT",
"keywords": [ "framework", "phpslides" ],
"keywords": ["framework", "phpslides"],
"support": {
"issues": "https://github.com/PhpSlides/framework/issues",
"source": "https://github.com/PhpSlides/framework"
Expand All @@ -31,22 +31,23 @@
},
"autoload": {
"psr-4": {
"PhpSlides\\": [ "src/Exception/" ],
"PhpSlides\\Src\\": [ "src/" ],
"PhpSlides\\Router\\": [ "Router/" ]
"PhpSlides\\": "src/Exception/",
"PhpSlides\\Core\\": "src/",
"PhpSlides\\Router\\": "Router/"
},
"files": [ "src/Bootstrap/App.php" ]
"files": ["src/Bootstrap/App.php"]
},
"autoload-dev": {
"psr-4": {
"PhpSlides\\Tests\\": "tests/"
"PhpSlides\\Tests\\": "tests/__tests__/"
}
},
"config": {
"preferred-install": "dist"
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "phpunit || vendor/bin/phpunit || php vendor/bin/phpunit",
"post-install-cmd": ["PhpSlides\\Core\\Cache\\Cache::clear()"]
},
"minimum-stability": "stable",
"prefer-stable": true
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
backupStaticProperties="false">
<testsuites>
<testsuite name="PhpSlides Test Suite">
<directory>tests/tests</directory>
<directory>tests/__tests__</directory>
</testsuite>
</testsuites>

Expand Down
2 changes: 1 addition & 1 deletion src/Bootstrap/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/**
* Start the PhpSlides Application
*/
(new \PhpSlides\Src\Foundation\Application())->create();
(new \PhpSlides\Core\Foundation\Application())->create();
16 changes: 9 additions & 7 deletions src/Cache/Cache.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace PhpSlides\Src\Cache;
namespace PhpSlides\Core\Cache;

use PhpSlides\Src\Foundation\Application;
use PhpSlides\Core\Foundation\Application;

/**
* Class Cache
Expand All @@ -20,10 +20,11 @@ class Cache
* effectively clearing all cached files. It is commonly used to
* reset the cache during development or when cache corruption occurs.
*/
public function clear()
public static function clear ()
{
// Check if the cache directory exists
if (is_dir(Application::$basePath . 'app/cache')) {
if (is_dir(Application::$basePath . 'app/cache'))
{
// Remove the cache directory
rmdir(Application::$basePath . 'app/cache');
}
Expand All @@ -36,12 +37,13 @@ public function clear()
* It is typically used during development when hot reload functionality
* needs to be reset or reloaded.
*/
public function clearHotReload()
public static function clearHotReload ()
{
// Check if the hot-reload cache file exists
if (file_exists(Application::$basePath . 'app/cache/hot-reload.json')) {
if (file_exists(Application::$basePath . 'app/cache/hot-reload.json'))
{
// Delete the hot-reload cache file
unlink(Application::$basePath . 'app/cache/hot-reload.json');
}
}
}
}
4 changes: 2 additions & 2 deletions src/Cli/Configure.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php declare(strict_types=1);


namespace PhpSlides\Src\Cli;
namespace PhpSlides\Core\Cli;

trait Configure
{
public static function bootstrap ()
protected static function bootstrap ()
{
if (php_sapi_name() == 'cli')
{
Expand Down
2 changes: 1 addition & 1 deletion src/Config/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

use PhpSlides\Src\Loader\FileLoader;
use PhpSlides\Core\Loader\FileLoader;

(new FileLoader())->load(__DIR__ . '/cors.php');
24 changes: 13 additions & 11 deletions src/Config/cors.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
<?php

use PhpSlides\Src\Logger\Logger;
use PhpSlides\Src\Loader\FileLoader;
use PhpSlides\Src\Foundation\Application;
use PhpSlides\Core\Logger\Logger;
use PhpSlides\Core\Loader\FileLoader;
use PhpSlides\Core\Foundation\Application;

$cors =
(new FileLoader())
->safeLoad(Application::$configsDir . 'cors.php')
->getLoad() ?:
[];
(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') {
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS')
{
class Log
{
use Logger;
public function __construct()
public function __construct ()
{
self::log();
}
Expand Down
Loading
Loading