From bcd35728906bf164a1055b30274dc54ba5b3d893 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Sun, 23 Aug 2026 12:19:16 +0200 Subject: [PATCH] feat(core): add config loader and request context --- src/Core/Config.php | 72 ++++++++++++++++++++++++++++++++++++++++++++ src/Core/Context.php | 49 ++++++++++++++++++++++++++++++ src/Core/Csrf.php | 36 ++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 src/Core/Config.php create mode 100644 src/Core/Context.php create mode 100644 src/Core/Csrf.php diff --git a/src/Core/Config.php b/src/Core/Config.php new file mode 100644 index 0000000..5231150 --- /dev/null +++ b/src/Core/Config.php @@ -0,0 +1,72 @@ += 2 && ($value[0] === '"' || $value[0] === "'")) { + $value = substr($value, 1, -1); + } + self::$values[$name] = $value; + } + self::hydrateDefaults(); + } + + private static function hydrateDefaults(): void + { + self::$values += [ + 'APP_ENV' => 'production', + 'APP_DEBUG' => 'false', + 'APP_URL' => 'http://127.0.0.1:8000', + 'DB_DRIVER' => 'sqlite', + 'DB_DATABASE' => basePath('database/bazaar.sqlite'), + 'DB_HOST' => '127.0.0.1', + 'DB_PORT' => '3306', + 'DB_USERNAME' => 'root', + 'DB_PASSWORD' => '', + 'STRIPE_SECRET_KEY' => 'sk_test_placeholder', + 'STRIPE_PUBLISHABLE_KEY' => 'pk_test_placeholder', + 'STRIPE_WEBHOOK_SECRET' => 'whsec_placeholder', + 'CURRENCY' => 'usd', + ]; + } + + public static function get(string $key, mixed $default = null): mixed + { + if (!self::$loaded) { + self::hydrateDefaults(); + self::$loaded = true; + } + return self::$values[$key] ?? $default; + } + + public static function set(string $key, mixed $value): void + { + self::$values[$key] = $value; + } + + public static function stripeConfigured(): bool + { + $key = (string) self::get('STRIPE_SECRET_KEY'); + return str_starts_with($key, 'sk_') && !str_contains($key, 'placeholder'); + } +} diff --git a/src/Core/Context.php b/src/Core/Context.php new file mode 100644 index 0000000..b28ae6a --- /dev/null +++ b/src/Core/Context.php @@ -0,0 +1,49 @@ +session->get(self::KEY); + if (!is_string($token) || $token === '') { + $token = bin2hex(random_bytes(32)); + $this->session->put(self::KEY, $token); + } + return $token; + } + + public function verify(?string $candidate): bool + { + $token = $this->session->get(self::KEY); + if (!is_string($token) || !is_string($candidate) || $candidate === '') { + return false; + } + return hash_equals($token, $candidate); + } + + public function field(): string + { + return ''; + } +}