first commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
class Auth
|
||||
{
|
||||
public static function check(): bool
|
||||
{
|
||||
return !empty($_SESSION['user']);
|
||||
}
|
||||
|
||||
public static function user(): ?array
|
||||
{
|
||||
return $_SESSION['user'] ?? null;
|
||||
}
|
||||
|
||||
public static function login(array $user): void
|
||||
{
|
||||
$_SESSION['user'] = $user;
|
||||
}
|
||||
|
||||
public static function logout(): void
|
||||
{
|
||||
unset($_SESSION['user']);
|
||||
session_destroy();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class DB
|
||||
{
|
||||
private static ?PDO $pdo = null;
|
||||
|
||||
public static function connection(): PDO
|
||||
{
|
||||
if (self::$pdo !== null) {
|
||||
return self::$pdo;
|
||||
}
|
||||
|
||||
$config = require __DIR__ . '/../../config/db.php';
|
||||
|
||||
$dsn = sprintf(
|
||||
'mysql:host=%s;port=%d;dbname=%s;charset=%s',
|
||||
$config['host'],
|
||||
$config['port'],
|
||||
$config['database'],
|
||||
$config['charset']
|
||||
);
|
||||
|
||||
self::$pdo = new PDO($dsn, $config['username'], $config['password'], [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
]);
|
||||
|
||||
return self::$pdo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
class Router
|
||||
{
|
||||
private array $routes = [
|
||||
'GET' => [],
|
||||
'POST' => [],
|
||||
];
|
||||
|
||||
public function get(string $path, array $handler): void
|
||||
{
|
||||
$this->routes['GET'][] = [$path, $handler];
|
||||
}
|
||||
|
||||
public function post(string $path, array $handler): void
|
||||
{
|
||||
$this->routes['POST'][] = [$path, $handler];
|
||||
}
|
||||
|
||||
public function dispatch(string $method, string $uri): void
|
||||
{
|
||||
$path = parse_url($uri, PHP_URL_PATH);
|
||||
|
||||
foreach ($this->routes[$method] ?? [] as [$route, $handler]) {
|
||||
$pattern = preg_replace('/\{[a-zA-Z_][a-zA-Z0-9_]*\}/', '([^/]+)', $route);
|
||||
$pattern = '#^' . $pattern . '$#';
|
||||
|
||||
if (preg_match($pattern, $path, $matches)) {
|
||||
array_shift($matches);
|
||||
|
||||
[$class, $action] = $handler;
|
||||
$controller = new $class();
|
||||
$controller->$action(...$matches);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
http_response_code(404);
|
||||
echo '404 Not Found';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
class View
|
||||
{
|
||||
public static function render(string $viewName, array $data = [], ?string $layout = 'app'): void
|
||||
{
|
||||
extract($data, EXTR_SKIP);
|
||||
|
||||
$viewFile = __DIR__ . '/../Views/' . $viewName . '.php';
|
||||
|
||||
ob_start();
|
||||
require $viewFile;
|
||||
$content = ob_get_clean();
|
||||
|
||||
if ($layout === null) {
|
||||
echo $content;
|
||||
return;
|
||||
}
|
||||
|
||||
$layoutFile = __DIR__ . '/../Views/layouts/' . $layout . '.php';
|
||||
require $layoutFile;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user