шаг 1 сделан

This commit is contained in:
Alisa
2026-06-03 23:12:10 +03:00
parent 891315780f
commit 6d7ef09c5b
21 changed files with 658 additions and 34 deletions
+36
View File
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Core;
use PDO;
final class Database
{
private static ?PDO $pdo = null;
public static function pdo(): PDO
{
if (self::$pdo !== null) {
return self::$pdo;
}
$host = app_env('DB_HOST', 'localhost');
$port = app_env('DB_PORT', '3306');
$database = app_env('DB_DATABASE', '');
$charset = app_env('DB_CHARSET', 'utf8mb4');
$username = app_env('DB_USERNAME', '');
$password = app_env('DB_PASSWORD', '');
$dsn = "mysql:host={$host};port={$port};dbname={$database};charset={$charset}";
self::$pdo = new PDO($dsn, $username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
return self::$pdo;
}
}
+52
View File
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace App\Core;
final class Env
{
public static function load(string $path): void
{
if (!is_file($path)) {
return;
}
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === false) {
return;
}
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || str_starts_with($line, '#')) {
continue;
}
[$key, $value] = array_pad(explode('=', $line, 2), 2, '');
$key = trim($key);
$value = self::normalizeValue(trim($value));
if ($key === '') {
continue;
}
$_ENV[$key] = $value;
putenv($key . '=' . $value);
}
}
private static function normalizeValue(string $value): string
{
if (
strlen($value) >= 2
&& (($value[0] === '"' && $value[-1] === '"') || ($value[0] === "'" && $value[-1] === "'"))
) {
return substr($value, 1, -1);
}
return $value;
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Core;
final class View
{
public static function render(string $template, array $data = []): void
{
$templatePath = base_path('views/' . trim($template, '/') . '.php');
if (!is_file($templatePath)) {
http_response_code(500);
echo 'Template not found';
return;
}
extract($data, EXTR_SKIP);
require base_path('views/layouts/main.php');
}
}