idk anymore! please put the dog down (me)!

This commit is contained in:
2026-04-19 19:22:38 +02:00
commit b6a258b3cf
16 changed files with 324 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace Controllers;
use PDO;
use src\View;
use src\ViewModels\HomeData;
class HomeController
{
public function __construct(
private PDO $db
) {}
public function index(): void
{
$data = new HomeData(
pageTitle: 'Welcome to Pawra',
posts: $this->db->query("SELECT * FROM posts LIMIT 10")->fetchAll()
);
View::render('home', $data);
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace src;
use PDO;
class Database
{
private static ?PDO $instance = null;
public static function getInstance(): PDO
{
if (self::$instance === null) {
$dataSourceName = sprintf(
'mysql:host=%s;dbname=%s',
getenv('DB_HOST'),
getenv('DB_NAME')
);
self::$instance = new PDO(
$dataSourceName,
getenv('DB_USER'),
getenv('DB_PASSWORD'),
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]
);
}
return self::$instance;
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace src;
use http\Exception\RuntimeException;
class View
{
public static function render(string $template, object $data): void
{
$path = __DIR__ . '/Views/' . $template . '.php';
if (!file_exists($path)) {
throw new \RuntimeException("View '$template' not found");
}
$templateFunc = require $path;
$templateFunc($data);
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace src\ViewModels;
class HomeData
{
public function __construct(
public string $pageTitle,
public array $posts // Or even better: public array|PostDTO[] $posts
) {}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
use src\ViewModels\HomeData;
return function(HomeData $data) { ?>
<!DOCTYPE html>
<html>
<head>
<title><?= htmlspecialchars($data->pageTitle) ?></title>
</head>
<body>
<h1><?= htmlspecialchars($data->pageTitle) ?></h1>
<?php foreach ($data->posts as $post): ?>
<h2><?= htmlspecialchars($post['title']) ?></h2>
<p><?= htmlspecialchars($post['body']) ?></p>
<?php endforeach; ?>
</body>
</html>
<?php }; ?>
+27
View File
@@ -0,0 +1,27 @@
<?php
use src\Database;
require_once __DIR__ . '/Database.php';
$db = Database::getInstance();
$db->exec("CREATE TABLE IF NOT EXISTS users (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)");
$db->exec("CREATE TABLE IF NOT EXISTS posts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id INT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
body TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)");
echo "Migrations ran successfully.\n";
+45
View File
@@ -0,0 +1,45 @@
<?php
use src\Database;
require_once __DIR__ . '/Database.php';
$db = Database::getInstance();
$db->exec("SET FOREIGN_KEY_CHECKS = 0");
$db->exec("TRUNCATE TABLE posts");
$db->exec("TRUNCATE TABLE users");
$db->exec("SET FOREIGN_KEY_CHECKS = 1");
$users = [
['luna', 'luna@pawra.dev', 'password123'],
['marco', 'marco@pawra.dev', 'password456'],
];
$insertUser = $db->prepare(
"INSERT INTO users (username, email, password) VALUES (?, ?, ?)"
);
foreach ($users as [$username, $email, $password]) {
$insertUser->execute([
$username,
$email,
password_hash($password, PASSWORD_DEFAULT),
]);
}
$posts = [
[1, 'First post', 'Hello from Luna — Pawra is live!'],
[1, 'Getting started', 'Here is how I set up my profile...'],
[2, 'Hello Pawra', 'Marco here, excited to join!'],
];
$insertPost = $db->prepare(
"INSERT INTO posts (user_id, title, body) VALUES (?, ?, ?)"
);
foreach ($posts as $post) {
$insertPost->execute($post);
}
echo "Seeded successfully.\n";