🚀 مُصادَق: الإطلاق الأولي للنظام المتكامل
This commit is contained in:
58
app/Core/Application.php
Normal file
58
app/Core/Application.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
use App\Core\{Request, Response, Router, Container};
|
||||
|
||||
final class Application
|
||||
{
|
||||
private Container $container;
|
||||
private Router $router;
|
||||
|
||||
public function __construct(string $basePath)
|
||||
{
|
||||
// 1. Load Environment Variables
|
||||
$dotenv = Dotenv::createImmutable($basePath);
|
||||
$dotenv->load();
|
||||
|
||||
// 2. Set Timezone
|
||||
date_default_timezone_set($_ENV['APP_TIMEZONE'] ?? 'Asia/Amman');
|
||||
|
||||
// 3. Initialize Core Components
|
||||
$this->container = new Container();
|
||||
$this->router = new Router($this->container);
|
||||
|
||||
// Register core services in container
|
||||
$this->container->set(Container::class, $this->container);
|
||||
$this->container->set(Router::class, $this->router);
|
||||
}
|
||||
|
||||
public function getRouter(): Router
|
||||
{
|
||||
return $this->router;
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
try {
|
||||
$request = new Request();
|
||||
$this->router->dispatch($request, $this->container);
|
||||
} catch (\Throwable $e) {
|
||||
// Global Exception Handler
|
||||
Response::error(
|
||||
'حدث خطأ غير متوقع في النظام',
|
||||
'INTERNAL_SERVER_ERROR',
|
||||
500,
|
||||
$_ENV['APP_ENV'] === 'development' ? [
|
||||
'message' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
] : null
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
72
app/Core/Container.php
Normal file
72
app/Core/Container.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use ReflectionNamedType;
|
||||
|
||||
final class Container
|
||||
{
|
||||
private array $instances = [];
|
||||
|
||||
public function set(string $id, mixed $concrete): void
|
||||
{
|
||||
$this->instances[$id] = $concrete;
|
||||
}
|
||||
|
||||
public function get(string $id): mixed
|
||||
{
|
||||
if (isset($this->instances[$id])) {
|
||||
if ($this->instances[$id] instanceof \Closure) {
|
||||
$this->instances[$id] = ($this->instances[$id])($this);
|
||||
}
|
||||
return $this->instances[$id];
|
||||
}
|
||||
|
||||
return $this->resolve($id);
|
||||
}
|
||||
|
||||
public function resolve(string $id): mixed
|
||||
{
|
||||
if (!class_exists($id)) {
|
||||
throw new Exception("Class {$id} cannot be resolved.");
|
||||
}
|
||||
|
||||
$reflection = new ReflectionClass($id);
|
||||
|
||||
if (!$reflection->isInstantiable()) {
|
||||
throw new Exception("Class {$id} is not instantiable.");
|
||||
}
|
||||
|
||||
$constructor = $reflection->getConstructor();
|
||||
|
||||
if (is_null($constructor)) {
|
||||
return new $id();
|
||||
}
|
||||
|
||||
$parameters = $constructor->getParameters();
|
||||
$dependencies = [];
|
||||
|
||||
foreach ($parameters as $parameter) {
|
||||
$type = $parameter->getType();
|
||||
|
||||
if (!$type instanceof ReflectionNamedType || $type->isBuiltin()) {
|
||||
if ($parameter->isDefaultValueAvailable()) {
|
||||
$dependencies[] = $parameter->getDefaultValue();
|
||||
continue;
|
||||
}
|
||||
throw new Exception("Unable to resolve parameter '{$parameter->getName()}' in class {$id}");
|
||||
}
|
||||
|
||||
$dependencies[] = $this->get($type->getName());
|
||||
}
|
||||
|
||||
$instance = $reflection->newInstanceArgs($dependencies);
|
||||
$this->instances[$id] = $instance;
|
||||
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
41
app/Core/Database.php
Normal file
41
app/Core/Database.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Exception;
|
||||
|
||||
final class Database
|
||||
{
|
||||
private static ?PDO $instance = null;
|
||||
|
||||
public static function getInstance(): PDO
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
$host = $_ENV['DB_HOST'];
|
||||
$db = $_ENV['DB_DATABASE'];
|
||||
$user = $_ENV['DB_USERNAME'];
|
||||
$pass = $_ENV['DB_PASSWORD'];
|
||||
$port = $_ENV['DB_PORT'];
|
||||
$charset = $_ENV['DB_CHARSET'] ?? 'utf8mb4';
|
||||
|
||||
$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset";
|
||||
$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, $user, $pass, $options);
|
||||
} catch (PDOException $e) {
|
||||
throw new Exception("Database Connection Error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
||||
33
app/Core/Redis.php
Normal file
33
app/Core/Redis.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
use Predis\Client;
|
||||
use Exception;
|
||||
|
||||
final class Redis
|
||||
{
|
||||
private static ?Client $instance = null;
|
||||
|
||||
public static function getInstance(): Client
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
try {
|
||||
self::$instance = new Client([
|
||||
'scheme' => 'tcp',
|
||||
'host' => $_ENV['REDIS_HOST'] ?? '127.0.0.1',
|
||||
'port' => $_ENV['REDIS_PORT'] ?? 6379,
|
||||
'password' => $_ENV['REDIS_PASSWORD'] ?: null,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
// If Redis fails, we might want to log it or handle gracefully
|
||||
// depending on how critical it is.
|
||||
throw new Exception("Redis Connection Error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
||||
56
app/Core/Request.php
Normal file
56
app/Core/Request.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
final class Request
|
||||
{
|
||||
private string $method;
|
||||
private string $path;
|
||||
private array $headers;
|
||||
private array $queryParams;
|
||||
private array $body;
|
||||
private array $files;
|
||||
public ?object $user = null; // Populated by AuthMiddleware
|
||||
public ?string $tenantId = null; // Populated by TenantMiddleware
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->method = $_SERVER['REQUEST_METHOD'];
|
||||
$this->path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||
$this->headers = getallheaders();
|
||||
$this->queryParams = $_GET;
|
||||
$this->files = $_FILES;
|
||||
|
||||
$contentType = $this->getHeader('Content-Type');
|
||||
if ($contentType && str_contains($contentType, 'application/json')) {
|
||||
$this->body = json_decode(file_get_contents('php://input'), true) ?? [];
|
||||
} else {
|
||||
$this->body = $_POST;
|
||||
}
|
||||
}
|
||||
|
||||
public function getMethod(): string { return $this->method; }
|
||||
public function getPath(): string { return $this->path; }
|
||||
public function getHeaders(): array { return $this->headers; }
|
||||
public function getQueryParams(): array { return $this->queryParams; }
|
||||
public function getBody(): array { return $this->body; }
|
||||
public function getFiles(): array { return $this->files; }
|
||||
|
||||
public function getHeader(string $name): ?string
|
||||
{
|
||||
$name = strtolower($name);
|
||||
foreach ($this->headers as $key => $value) {
|
||||
if (strtolower($key) === $name) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function input(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return $this->body[$key] ?? $this->queryParams[$key] ?? $default;
|
||||
}
|
||||
}
|
||||
45
app/Core/Response.php
Normal file
45
app/Core/Response.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
final class Response
|
||||
{
|
||||
public static function json(array $data, int $status = 200, array $headers = []): void
|
||||
{
|
||||
self::send($data, $status, array_merge(['Content-Type' => 'application/json; charset=utf-8'], $headers));
|
||||
}
|
||||
|
||||
public static function error(string $messageAr, string $code, int $status = 400, ?array $details = null): void
|
||||
{
|
||||
$data = [
|
||||
'success' => false,
|
||||
'error' => [
|
||||
'message_ar' => $messageAr,
|
||||
'code' => $code,
|
||||
'details' => $details
|
||||
]
|
||||
];
|
||||
self::json($data, $status);
|
||||
}
|
||||
|
||||
private static function send(mixed $data, int $status, array $headers): void
|
||||
{
|
||||
http_response_code($status);
|
||||
|
||||
foreach ($headers as $name => $value) {
|
||||
header("$name: $value");
|
||||
}
|
||||
|
||||
// Apply Security Headers
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
header('X-Frame-Options: DENY');
|
||||
header('X-XSS-Protection: 1; mode=block');
|
||||
header('Referrer-Policy: strict-origin-when-cross-origin');
|
||||
header_remove('X-Powered-By');
|
||||
|
||||
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
90
app/Core/Router.php
Normal file
90
app/Core/Router.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
use FastRoute\RouteCollector;
|
||||
use function FastRoute\simpleDispatcher;
|
||||
|
||||
final class Router
|
||||
{
|
||||
private array $routes = [];
|
||||
public Container $container;
|
||||
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
public function addRoute(string $method, string $path, array|callable $handler): void
|
||||
{
|
||||
$this->routes[] = [$method, $path, $handler];
|
||||
}
|
||||
|
||||
public function dispatch(Request $request): void
|
||||
{
|
||||
$dispatcher = simpleDispatcher(function (RouteCollector $r) {
|
||||
foreach ($this->routes as $route) {
|
||||
$r->addRoute($route[0], $route[1], $route[2]);
|
||||
}
|
||||
});
|
||||
|
||||
$routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getPath());
|
||||
|
||||
switch ($routeInfo[0]) {
|
||||
case \FastRoute\Dispatcher::NOT_FOUND:
|
||||
Response::error('المسار غير موجود', 'NOT_FOUND', 404);
|
||||
break;
|
||||
case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
|
||||
Response::error('الطريقة غير مسموح بها', 'METHOD_NOT_ALLOWED', 405);
|
||||
break;
|
||||
case \FastRoute\Dispatcher::FOUND:
|
||||
$handler = $routeInfo[1];
|
||||
$vars = $routeInfo[2];
|
||||
|
||||
$this->executeHandler($handler, $request, $container, $vars);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private function executeHandler(mixed $handler, Request $request, Container $container, array $vars): void
|
||||
{
|
||||
if (is_array($handler) && isset($handler['middleware'])) {
|
||||
$middlewares = (array) $handler['middleware'];
|
||||
$finalHandler = $handler['handler'];
|
||||
|
||||
$pipeline = $this->createPipeline($middlewares, $finalHandler, $container, $vars);
|
||||
$pipeline($request);
|
||||
} else {
|
||||
$this->callHandler($handler, $request, $container, $vars);
|
||||
}
|
||||
}
|
||||
|
||||
private function createPipeline(array $middlewares, mixed $handler, Container $container, array $vars): callable
|
||||
{
|
||||
return array_reduce(
|
||||
array_reverse($middlewares),
|
||||
function ($next, $middleware) use ($container) {
|
||||
return function ($request) use ($next, $middleware, $container) {
|
||||
$instance = $container->get($middleware);
|
||||
return $instance->handle($request, $next);
|
||||
};
|
||||
},
|
||||
function ($request) use ($handler, $container, $vars) {
|
||||
$this->callHandler($handler, $request, $container, $vars);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private function callHandler(mixed $handler, Request $request, Container $container, array $vars): void
|
||||
{
|
||||
if (is_array($handler)) {
|
||||
[$controllerClass, $method] = $handler;
|
||||
$controller = $container->get($controllerClass);
|
||||
$controller->$method($request, ...array_values($vars));
|
||||
} else {
|
||||
$handler($request, ...array_values($vars));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user