шаг 1 сделан
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
spl_autoload_register(static function (string $class): void {
|
||||
$prefix = 'App\\';
|
||||
|
||||
if (!str_starts_with($class, $prefix)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$relativeClass = substr($class, strlen($prefix));
|
||||
$path = __DIR__ . '/' . str_replace('\\', '/', $relativeClass) . '.php';
|
||||
|
||||
if (is_file($path)) {
|
||||
require_once $path;
|
||||
}
|
||||
});
|
||||
|
||||
require_once __DIR__ . '/helpers.php';
|
||||
|
||||
App\Core\Env::load(dirname(__DIR__) . '/.env');
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Core\View;
|
||||
|
||||
function app_env(string $key, ?string $default = null): ?string
|
||||
{
|
||||
$value = $_ENV[$key] ?? getenv($key);
|
||||
|
||||
if ($value === false || $value === null) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
function base_path(string $path = ''): string
|
||||
{
|
||||
$base = dirname(__DIR__);
|
||||
|
||||
return $path === '' ? $base : $base . '/' . ltrim($path, '/');
|
||||
}
|
||||
|
||||
function view(string $template, array $data = []): void
|
||||
{
|
||||
View::render($template, $data);
|
||||
}
|
||||
Reference in New Issue
Block a user