Initial commit
This commit is contained in:
99
backend/app/Core/Database.php
Normal file
99
backend/app/Core/Database.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
/**
|
||||
* PDO Database wrapper using Singleton pattern.
|
||||
*/
|
||||
class Database
|
||||
{
|
||||
private static ?PDO $instance = null;
|
||||
|
||||
/**
|
||||
* Get active PDO database instance
|
||||
*
|
||||
* @return PDO
|
||||
* @throws PDOException
|
||||
*/
|
||||
public static function getConnection(): PDO
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
$host = getenv('DB_HOST') ?: '127.0.0.1';
|
||||
$port = getenv('DB_PORT') ?: '3306';
|
||||
$dbName = getenv('DB_DATABASE') ?: 'nabeh_master';
|
||||
$username = getenv('DB_USERNAME') ?: 'root';
|
||||
$password = getenv('DB_PASSWORD') ?: '';
|
||||
|
||||
$dsn = "mysql:host={$host};port={$port};dbname={$dbName};charset=utf8mb4";
|
||||
|
||||
$options = [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
];
|
||||
|
||||
try {
|
||||
self::$instance = new PDO($dsn, $username, $password, $options);
|
||||
} catch (PDOException $e) {
|
||||
// Log the exact error internally but hide sensitive DSN on production
|
||||
error_log("Database Connection Error: " . $e->getMessage());
|
||||
throw new PDOException("Could not connect to the database. Check database settings.");
|
||||
}
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand execute statement with parameters
|
||||
*
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @return \PDOStatement
|
||||
*/
|
||||
public static function query(string $sql, array $params = []): \PDOStatement
|
||||
{
|
||||
$pdo = self::getConnection();
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all matching records
|
||||
*/
|
||||
public static function select(string $sql, array $params = []): array
|
||||
{
|
||||
return self::query($sql, $params)->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve single matching record
|
||||
*/
|
||||
public static function selectOne(string $sql, array $params = [])
|
||||
{
|
||||
return self::query($sql, $params)->fetch() ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert record and return last inserted ID
|
||||
*/
|
||||
public static function insert(string $sql, array $params = []): string
|
||||
{
|
||||
$pdo = self::getConnection();
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
return $pdo->lastInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute generic non-query SQL (Update/Delete) and return affected rows
|
||||
*/
|
||||
public static function execute(string $sql, array $params = []): int
|
||||
{
|
||||
return self::query($sql, $params)->rowCount();
|
||||
}
|
||||
}
|
||||
86
backend/app/Core/Env.php
Normal file
86
backend/app/Core/Env.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
/**
|
||||
* Lightweight secure environment variable loader
|
||||
*/
|
||||
class Env
|
||||
{
|
||||
/**
|
||||
* Load environment variables from a file path
|
||||
*
|
||||
* @param string $path
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function load(string $path): void
|
||||
{
|
||||
if (!file_exists($path)) {
|
||||
// Create a default if it doesn't exist to prevent crash, or throw
|
||||
if (file_exists($path . '.example')) {
|
||||
copy($path . '.example', $path);
|
||||
} else {
|
||||
throw new \Exception("Environment file not found at: {$path}");
|
||||
}
|
||||
}
|
||||
|
||||
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
foreach ($lines as $line) {
|
||||
$line = trim($line);
|
||||
|
||||
// Skip comments and empty lines
|
||||
if (empty($line) || strpos($line, '#') === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Split by the first equals sign
|
||||
if (strpos($line, '=') !== false) {
|
||||
list($key, $value) = explode('=', $line, 2);
|
||||
$key = trim($key);
|
||||
$value = trim($value);
|
||||
|
||||
// Strip surrounding quotes
|
||||
if (preg_match('/^"(.+)"$/', $value, $matches) || preg_match("/^'(.+)'$/", $value, $matches)) {
|
||||
$value = $matches[1];
|
||||
}
|
||||
|
||||
// Inject into PHP superglobals and env
|
||||
putenv("{$key}={$value}");
|
||||
$_ENV[$key] = $value;
|
||||
$_SERVER[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve environment variable with optional default value
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get(string $key, $default = null)
|
||||
{
|
||||
$val = getenv($key);
|
||||
if ($val === false) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
switch (strtolower($val)) {
|
||||
case 'true':
|
||||
case '(true)':
|
||||
return true;
|
||||
case 'false':
|
||||
case '(false)':
|
||||
return false;
|
||||
case 'null':
|
||||
case '(null)':
|
||||
return null;
|
||||
case 'empty':
|
||||
case '(empty)':
|
||||
return '';
|
||||
}
|
||||
|
||||
return $val;
|
||||
}
|
||||
}
|
||||
102
backend/app/Core/Request.php
Normal file
102
backend/app/Core/Request.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
/**
|
||||
* Handles HTTP requests, extracting query params, body data, and headers.
|
||||
*/
|
||||
class Request
|
||||
{
|
||||
private string $method;
|
||||
private string $path;
|
||||
private array $queryParams;
|
||||
private array $bodyParams;
|
||||
private array $headers;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->method = strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
|
||||
|
||||
// Extract clean path without query parameters
|
||||
$uri = $_SERVER['REQUEST_URI'] ?? '/';
|
||||
$path = explode('?', $uri)[0];
|
||||
$this->path = '/' . trim($path, '/');
|
||||
|
||||
$this->queryParams = $_GET;
|
||||
$this->headers = $this->extractHeaders();
|
||||
$this->bodyParams = $this->parseBody();
|
||||
}
|
||||
|
||||
public function getMethod(): string
|
||||
{
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
public function getPath(): string
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
public function getQueryParams(): array
|
||||
{
|
||||
return $this->queryParams;
|
||||
}
|
||||
|
||||
public function getQuery(string $key, $default = null)
|
||||
{
|
||||
return $this->queryParams[$key] ?? $default;
|
||||
}
|
||||
|
||||
public function getBody(): array
|
||||
{
|
||||
return $this->bodyParams;
|
||||
}
|
||||
|
||||
public function get(string $key, $default = null)
|
||||
{
|
||||
return $this->bodyParams[$key] ?? ($this->queryParams[$key] ?? $default);
|
||||
}
|
||||
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
public function getHeader(string $key, $default = null): ?string
|
||||
{
|
||||
$keyLower = strtolower($key);
|
||||
return $this->headers[$keyLower] ?? $default;
|
||||
}
|
||||
|
||||
private function extractHeaders(): array
|
||||
{
|
||||
$headers = [];
|
||||
foreach ($_SERVER as $name => $value) {
|
||||
if (substr($name, 0, 5) == 'HTTP_') {
|
||||
$headers[strtolower(str_replace('_', '-', substr($name, 5)))] = $value;
|
||||
} elseif ($name == 'CONTENT_TYPE') {
|
||||
$headers['content-type'] = $value;
|
||||
} elseif ($name == 'CONTENT_LENGTH') {
|
||||
$headers['content-length'] = $value;
|
||||
}
|
||||
}
|
||||
return $headers;
|
||||
}
|
||||
|
||||
private function parseBody(): array
|
||||
{
|
||||
if ($this->method === 'GET') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$contentType = $this->getHeader('content-type', '');
|
||||
|
||||
if (strpos($contentType, 'application/json') !== false) {
|
||||
$input = file_get_contents('php://input');
|
||||
$data = json_decode($input, true);
|
||||
return is_array($data) ? $data : [];
|
||||
}
|
||||
|
||||
return $_POST;
|
||||
}
|
||||
}
|
||||
93
backend/app/Core/Response.php
Normal file
93
backend/app/Core/Response.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
/**
|
||||
* Handles generating and sending consistent API and HTML responses.
|
||||
*/
|
||||
class Response
|
||||
{
|
||||
private int $statusCode = 200;
|
||||
private array $headers = [];
|
||||
|
||||
public function setStatusCode(int $code): self
|
||||
{
|
||||
$this->statusCode = $code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
|
||||
public function setHeader(string $name, string $value): self
|
||||
{
|
||||
$this->headers[$name] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send JSON response and terminate execution
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param int $code
|
||||
* @return void
|
||||
*/
|
||||
public function json($data, int $code = 200): void
|
||||
{
|
||||
$this->setStatusCode($code);
|
||||
$this->setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||
|
||||
// Setup base CORS headers for our API
|
||||
$this->setHeader('Access-Control-Allow-Origin', '*');
|
||||
$this->setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
||||
$this->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
|
||||
|
||||
$this->sendHeaders();
|
||||
http_response_code($this->statusCode);
|
||||
|
||||
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send success JSON response
|
||||
*/
|
||||
public function success(string $message, array $data = [], int $code = 200): void
|
||||
{
|
||||
$this->json([
|
||||
'status' => 'success',
|
||||
'message' => $message,
|
||||
'data' => $data
|
||||
], $code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send error JSON response
|
||||
*/
|
||||
public function error(string $message, int $code = 400, array $errors = []): void
|
||||
{
|
||||
$response = [
|
||||
'status' => 'error',
|
||||
'message' => $message
|
||||
];
|
||||
|
||||
if (!empty($errors)) {
|
||||
$response['errors'] = $errors;
|
||||
}
|
||||
|
||||
$this->json($response, $code);
|
||||
}
|
||||
|
||||
private function sendHeaders(): void
|
||||
{
|
||||
if (headers_sent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->headers as $name => $value) {
|
||||
header("{$name}: {$value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
127
backend/app/Core/Router.php
Normal file
127
backend/app/Core/Router.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
/**
|
||||
* Basic regex-based router supporting dynamic parameters, middlewares, and CORS OPTIONS.
|
||||
*/
|
||||
class Router
|
||||
{
|
||||
private array $routes = [];
|
||||
private array $globalMiddleware = [];
|
||||
|
||||
/**
|
||||
* Define a GET route
|
||||
*/
|
||||
public function get(string $path, $handler, array $middleware = []): void
|
||||
{
|
||||
$this->addRoute('GET', $path, $handler, $middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define a POST route
|
||||
*/
|
||||
public function post(string $path, $handler, array $middleware = []): void
|
||||
{
|
||||
$this->addRoute('POST', $path, $handler, $middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define a PUT route
|
||||
*/
|
||||
public function put(string $path, $handler, array $middleware = []): void
|
||||
{
|
||||
$this->addRoute('PUT', $path, $handler, $middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define a DELETE route
|
||||
*/
|
||||
public function delete(string $path, $handler, array $middleware = []): void
|
||||
{
|
||||
$this->addRoute('DELETE', $path, $handler, $middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add global middleware applied to all routes
|
||||
*/
|
||||
public function use($middleware): void
|
||||
{
|
||||
$this->globalMiddleware[] = $middleware;
|
||||
}
|
||||
|
||||
private function addRoute(string $method, string $path, $handler, array $middleware): void
|
||||
{
|
||||
// Convert path matching expressions: /api/tickets/{id} -> regex
|
||||
$pattern = preg_replace('/\{([a-zA-Z0-9_]+)\}/', '(?P<$1>[^/]+)', $path);
|
||||
$pattern = '#^' . $pattern . '$#';
|
||||
|
||||
$this->routes[] = [
|
||||
'method' => $method,
|
||||
'path' => $path,
|
||||
'pattern' => $pattern,
|
||||
'handler' => $handler,
|
||||
'middleware' => $middleware
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Match current request and execute middleware and controller action
|
||||
*/
|
||||
public function dispatch(Request $request, Response $response): void
|
||||
{
|
||||
$method = $request->getMethod();
|
||||
$path = $request->getPath();
|
||||
|
||||
// Handle CORS Preflight Preemptively
|
||||
if ($method === 'OPTIONS') {
|
||||
$response->setHeader('Access-Control-Allow-Origin', '*');
|
||||
$response->setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
||||
$response->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
|
||||
$response->setStatusCode(200);
|
||||
exit;
|
||||
}
|
||||
|
||||
foreach ($this->routes as $route) {
|
||||
if ($route['method'] === $method && preg_match($route['pattern'], $path, $matches)) {
|
||||
// Filter named captures from regex match
|
||||
$params = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
|
||||
|
||||
// Run global middleware first
|
||||
foreach ($this->globalMiddleware as $mw) {
|
||||
$mwInstance = new $mw();
|
||||
$mwInstance->handle($request, $response);
|
||||
}
|
||||
|
||||
// Run route specific middleware
|
||||
foreach ($route['middleware'] as $mw) {
|
||||
$mwInstance = new $mw();
|
||||
$mwInstance->handle($request, $response);
|
||||
}
|
||||
|
||||
// Execute Controller
|
||||
$handler = $route['handler'];
|
||||
if (is_array($handler) && count($handler) === 2) {
|
||||
list($controllerClass, $action) = $handler;
|
||||
if (class_exists($controllerClass)) {
|
||||
$controller = new $controllerClass();
|
||||
if (method_exists($controller, $action)) {
|
||||
// Call action with Request, Response and URI dynamic parameters
|
||||
call_user_func_array([$controller, $action], array_merge([$request, $response], $params));
|
||||
return;
|
||||
}
|
||||
}
|
||||
} elseif (is_callable($handler)) {
|
||||
call_user_func_array($handler, array_merge([$request, $response], $params));
|
||||
return;
|
||||
}
|
||||
|
||||
$response->error("Handler error for route: {$path}", 500);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Route not found
|
||||
$response->error("Route not found: [{$method}] {$path}", 404);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user