Итерация 1: каркас Slim 4 приложения
- Slim 4 + php-di контейнер с маршрутизацией - Docker Compose: app (PHP 8.3-FPM) + web (nginx) + db (MariaDB) - Phinx миграция: таблица users (uuid, username, password_hash) - Auth: login/logout с сессиями - Setup: создание первого пользователя - Dashboard с layout (Bootstrap 5.3) - CLI: bin/console, bin/run-scan-worker.php - Smoke test: scripts/check.sh - README.md, PLAN.md
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domovoy\Repositories;
|
||||
|
||||
use PDO;
|
||||
use Domovoy\Models\User;
|
||||
|
||||
class UserRepository
|
||||
{
|
||||
private PDO $pdo;
|
||||
|
||||
public function __construct(PDO $pdo)
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
public function findById(string $id): ?User
|
||||
{
|
||||
$stmt = $this->pdo->prepare('SELECT * FROM users WHERE id = :id');
|
||||
$stmt->execute(['id' => $id]);
|
||||
$row = $stmt->fetch();
|
||||
return $row ? User::fromArray($row) : null;
|
||||
}
|
||||
|
||||
public function findByUsername(string $username): ?User
|
||||
{
|
||||
$stmt = $this->pdo->prepare('SELECT * FROM users WHERE username = :username');
|
||||
$stmt->execute(['username' => $username]);
|
||||
$row = $stmt->fetch();
|
||||
return $row ? User::fromArray($row) : null;
|
||||
}
|
||||
|
||||
public function findAll(): array
|
||||
{
|
||||
$stmt = $this->pdo->query('SELECT * FROM users ORDER BY created_at DESC');
|
||||
$users = [];
|
||||
while ($row = $stmt->fetch()) {
|
||||
$users[] = User::fromArray($row);
|
||||
}
|
||||
return $users;
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
$stmt = $this->pdo->query('SELECT COUNT(*) FROM users');
|
||||
return (int) $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
public function save(User $user): void
|
||||
{
|
||||
if ($user->id === null) {
|
||||
$user->id = \Ramsey\Uuid\Uuid::uuid4()->toString();
|
||||
}
|
||||
|
||||
$now = (new \DateTimeImmutable())->format('Y-m-d H:i:s');
|
||||
$user->updatedAt = new \DateTimeImmutable($now);
|
||||
if ($user->createdAt === null) {
|
||||
$user->createdAt = new \DateTimeImmutable($now);
|
||||
}
|
||||
|
||||
$stmt = $this->pdo->prepare(
|
||||
'INSERT INTO users (id, username, password_hash, created_at, updated_at)
|
||||
VALUES (:id, :username, :password_hash, :created_at, :updated_at)'
|
||||
);
|
||||
$stmt->execute([
|
||||
'id' => $user->id,
|
||||
'username' => $user->username,
|
||||
'password_hash' => $user->passwordHash,
|
||||
'created_at' => $user->createdAt->format('Y-m-d H:i:s'),
|
||||
'updated_at' => $user->updatedAt->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user