instances added god may help

This commit is contained in:
2026-02-23 12:58:19 +01:00
parent 25370a1a55
commit 99db252f60
290 changed files with 9858 additions and 4914 deletions

View File

@@ -2,9 +2,10 @@
namespace MintyPHP\Http;
use MintyPHP\Service\Audit\ApiAuditService;
use MintyPHP\Service\Security\RateLimiterService;
use MintyPHP\Service\Settings\SettingService;
use MintyPHP\Service\Security\SecurityServicesFactory;
use MintyPHP\Service\Settings\SettingGateway;
use MintyPHP\Service\Settings\SettingServicesFactory;
class ApiBootstrap
{
@@ -17,6 +18,10 @@ class ApiBootstrap
private static bool $initialized = false;
private static bool $shutdownRegistered = false;
private static ?SecurityServicesFactory $securityServicesFactory = null;
private static ?RateLimiterService $rateLimiterService = null;
private static ?SettingServicesFactory $settingServicesFactory = null;
private static ?SettingGateway $settingGateway = null;
/**
* Initialize the API request context.
@@ -36,7 +41,7 @@ class ApiBootstrap
}
self::registerShutdownHandler();
ApiAuditService::startRequestContext();
\auditServicesFactory()->createApiAuditService()->startRequestContext();
self::setCorsHeaders();
if (strtoupper($_SERVER['REQUEST_METHOD'] ?? '') === 'OPTIONS') {
@@ -63,7 +68,7 @@ class ApiBootstrap
return;
}
$allowedOrigins = array_fill_keys(SettingService::getApiCorsAllowedOrigins(), true);
$allowedOrigins = array_fill_keys(self::settings()->getApiCorsAllowedOrigins(), true);
if (!isset($allowedOrigins[$origin])) {
return;
}
@@ -89,7 +94,7 @@ class ApiBootstrap
if ($statusCode <= 0) {
$statusCode = 200;
}
ApiAuditService::finish($statusCode);
\auditServicesFactory()->createApiAuditService()->finish($statusCode);
return;
}
@@ -100,7 +105,7 @@ class ApiBootstrap
if ($statusCode <= 0) {
$statusCode = 200;
}
ApiAuditService::finish($statusCode);
\auditServicesFactory()->createApiAuditService()->finish($statusCode);
return;
}
@@ -108,7 +113,7 @@ class ApiBootstrap
if ($statusCode < 400) {
$statusCode = 500;
}
ApiAuditService::finish($statusCode, 'fatal_error');
\auditServicesFactory()->createApiAuditService()->finish($statusCode, 'fatal_error');
});
}
@@ -154,7 +159,7 @@ class ApiBootstrap
$ip = 'unknown';
}
$ipResult = RateLimiterService::hit(
$ipResult = self::rateLimiter()->hit(
self::API_RATE_SCOPE_IP,
$ip,
self::API_RATE_LIMIT_IP,
@@ -170,7 +175,7 @@ class ApiBootstrap
return;
}
$tokenResult = RateLimiterService::hit(
$tokenResult = self::rateLimiter()->hit(
self::API_RATE_SCOPE_TOKEN,
strtolower($selector) . '|' . $ip,
self::API_RATE_LIMIT_TOKEN,
@@ -198,4 +203,32 @@ class ApiBootstrap
return $selector;
}
private static function settings(): SettingGateway
{
if (self::$settingGateway instanceof SettingGateway) {
return self::$settingGateway;
}
if (!(self::$settingServicesFactory instanceof SettingServicesFactory)) {
self::$settingServicesFactory = new SettingServicesFactory();
}
self::$settingGateway = self::$settingServicesFactory->createSettingGateway();
return self::$settingGateway;
}
private static function rateLimiter(): RateLimiterService
{
if (self::$rateLimiterService instanceof RateLimiterService) {
return self::$rateLimiterService;
}
if (!(self::$securityServicesFactory instanceof SecurityServicesFactory)) {
self::$securityServicesFactory = new SecurityServicesFactory();
}
self::$rateLimiterService = self::$securityServicesFactory->createRateLimiterService();
return self::$rateLimiterService;
}
}