This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(makefile): add makefile * fix(readme): config, tests * feat(routing): update route system * feat(with): implement with method like laravel * fix(controller): send data from controller to view * feat(errors): abort pages
- Loading branch information
1 parent
023b63a
commit 9ef1911
Showing
18 changed files
with
367 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
class RouteManager { | ||
|
||
/** | ||
* @var array<string> METHODS | ||
*/ | ||
protected const METHODS = ["GET", "POST"]; | ||
|
||
public function __construct() {} | ||
|
||
/** | ||
* @param string $method | ||
* @return bool | ||
*/ | ||
protected function availableMethod(string $method): bool | ||
{ | ||
return in_array($method, $this::METHODS); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
class Router { | ||
|
||
/** | ||
* @var array $routes<int,Route> | ||
*/ | ||
public array $routes; /** @phpstan-ignore-line */ | ||
|
||
/** | ||
* @params array<Route> $routes | ||
* @return Router | ||
*/ | ||
public static function load(array $routes): Router /** @phpstan-ignore-line */ | ||
{ | ||
foreach ($routes as $route) { | ||
if (!is_object($route) && !($route instanceof Route)) { /** @phpstan-ignore-line */ | ||
throw new Exception("Route given to Router isn't instance of Route class."); | ||
} | ||
} | ||
return new Router($routes); | ||
} | ||
|
||
/** | ||
* @param array<int,Route> $routes | ||
*/ | ||
public function __construct(array $routes) | ||
{ | ||
$this->routes = $routes; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function run(): void | ||
{ | ||
foreach ($this->routes as $route) { | ||
if (explode('?', $_SERVER["REQUEST_URI"])[0] === $route->getUri()) { | ||
$route->call(); | ||
return; | ||
} | ||
} | ||
View::abort("404", header: true); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
class View { | ||
|
||
/** | ||
* @var string ROOT | ||
*/ | ||
public const ROOT = __DIR__ . "/../../resources/views"; | ||
|
||
/** | ||
* @param string $view | ||
* @return string | ||
*/ | ||
public static function find(string $view): string | ||
{ | ||
$el = explode(".", $view); | ||
if (count($el) === 1) return $el[0]; | ||
$view = self::ROOT; | ||
foreach ($el as $l) { | ||
$view .= "/$l"; | ||
} | ||
$view .= ".php"; | ||
return $view; | ||
} | ||
|
||
/** | ||
* @param string $view | ||
* @param array $data | ||
* @throws Exception | ||
* @return void | ||
*/ | ||
public static function show(string $view, array $data = []): void | ||
{ | ||
try { | ||
foreach ($data as $k => $v) { $$k = $v; } | ||
include self::ROOT . "/layouts/head.php"; | ||
include self::find($view); | ||
include self::ROOT . "/layouts/foot.php"; | ||
} catch (\Throwable $th) { | ||
throw new Exception("Invalid path to view given to View::show() static function $th"); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $view | ||
* @param bool $header | ||
* @param array $data | ||
* @return void | ||
*/ | ||
public static function abort(string $view, bool $header = false, array $data = []): void | ||
{ | ||
try { | ||
if ($header) include self::ROOT . "/layouts/head.php"; | ||
include self::find("errors.$view"); | ||
if ($header) include self::ROOT . "/layouts/foot.php"; | ||
} catch (\Throwable $th) { | ||
throw new Exception("Invalid path to view given to View::show() static function $th"); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.