- add documentation for EF API calling implementation - greater adherence to standards - better DI logic
65 lines
2.2 KiB
PHP
65 lines
2.2 KiB
PHP
<?php
|
|
// bootstrap.php
|
|
// Posizione consigliata: project_root/bootstrap.php
|
|
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
|
|
use Core\Container;
|
|
use Core\Config;
|
|
use Core\RateLimiter\RateLimiterInterface;
|
|
use Core\RateLimiter\FileRateLimiter;
|
|
use Core\RateLimiter\InMemoryRateLimiter;
|
|
use Psr\Log\LoggerInterface;
|
|
use Core\LoggerFactory;
|
|
use Services\ExternalApiService;
|
|
|
|
$container = new Container();
|
|
|
|
$config = new Config();
|
|
$container->singleton(Config::class, function() use ($config) {
|
|
return $config;
|
|
});
|
|
|
|
$container->singleton(\Core\HttpClient::class, function() {
|
|
return new \Core\HttpClient();
|
|
});
|
|
|
|
// Rate limiter driver configurabile via config or env: 'file' or 'memory'
|
|
$rlDriver = $config->get('rate_limiter_driver', 'file');
|
|
|
|
if ($rlDriver === 'memory') {
|
|
$container->singleton(RateLimiterInterface::class, function($c) {
|
|
$config = $c->make(Config::class);
|
|
$requests = (int) $config->get('rate_limit_requests', 100);
|
|
$window = (int) $config->get('rate_limit_window_seconds', 60);
|
|
return new InMemoryRateLimiter($requests, $window);
|
|
});
|
|
} else {
|
|
$container->singleton(RateLimiterInterface::class, function($c) {
|
|
$config = $c->make(Config::class);
|
|
$dir = $config->get('rate_limit_storage_dir', sys_get_temp_dir() . '/api_rate_limit');
|
|
$requests = (int) $config->get('rate_limit_requests', 100);
|
|
$window = (int) $config->get('rate_limit_window_seconds', 60);
|
|
return new FileRateLimiter($dir, $requests, $window);
|
|
});
|
|
}
|
|
|
|
// Logger binding as PSR LoggerInterface
|
|
$container->singleton(LoggerInterface::class, function($c) {
|
|
$config = $c->make(Config::class);
|
|
// LoggerFactory::create returns a Monolog\Logger instance
|
|
return LoggerFactory::create($config->get('log_ident', 'api'));
|
|
});
|
|
|
|
// External API service binding
|
|
$container->singleton(ExternalApiService::class, function($c) {
|
|
return new ExternalApiService();
|
|
});
|
|
|
|
// If you have other services, bind them here, for example:
|
|
// $container->singleton(SomeService::class, function($c) {
|
|
// return new SomeService($c->make(LoggerInterface::class), ...);
|
|
// });
|
|
|
|
return $container;
|