fix namespaces and references
This commit is contained in:
+10
-9
@@ -4,14 +4,15 @@
|
||||
|
||||
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 Api\Core\Container;
|
||||
use Api\Core\Config;
|
||||
use Api\Core\HttpClient;
|
||||
use Api\Core\RateLimiter\RateLimiterInterface;
|
||||
use Api\Core\RateLimiter\FileRateLimiter;
|
||||
use Api\Core\RateLimiter\InMemoryRateLimiter;
|
||||
use Api\Core\Log\LoggerFactory;
|
||||
use ElixForms\ElixFormsClient;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
$container = new Container();
|
||||
|
||||
@@ -20,8 +21,8 @@ $container->singleton(Config::class, function() use ($config) {
|
||||
return $config;
|
||||
});
|
||||
|
||||
$container->singleton(\Core\HttpClient::class, function() {
|
||||
return new \Core\HttpClient();
|
||||
$container->singleton(HttpClient::class, function() {
|
||||
return new HttpClient();
|
||||
});
|
||||
|
||||
// Rate limiter driver configurabile via config or env: 'file' or 'memory'
|
||||
|
||||
Generated
+2643
File diff suppressed because it is too large
Load Diff
+4
-8
@@ -2,10 +2,10 @@
|
||||
$container = require __DIR__ . '/../bootstrap.php';
|
||||
|
||||
use Api\Auth\ApiTokenAuthenticator;
|
||||
use Core\Router;
|
||||
use Core\Request;
|
||||
use Core\Response;
|
||||
use Core\RateLimiter\RateLimiterInterface;
|
||||
use Api\Core\Router;
|
||||
use Api\Core\Request;
|
||||
use Api\Core\Response;
|
||||
use Api\Core\RateLimiter\RateLimiterInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
$logger = $container->make(LoggerInterface::class);
|
||||
@@ -48,10 +48,6 @@ $router->get('/users', 'UsersController@index');
|
||||
$router->post('/users', 'UsersController@create');
|
||||
$router->get('/example', 'ExampleController@test');
|
||||
|
||||
// external API routes
|
||||
$router->get('/api/elixforms/status', 'Api\\Controllers\\ExternalElixFormsController@status');
|
||||
$router->post('/api/elixforms/login', 'Api\\Controllers\\ExternalElixFormsController@login');
|
||||
|
||||
// Rate limiting by IP address
|
||||
$key = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
|
||||
if (!$limiter->allow($key)) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
namespace Api\Auth;
|
||||
|
||||
use Core\Config;
|
||||
use Core\Request;
|
||||
use Api\Core\Config;
|
||||
use Api\Core\Request;
|
||||
|
||||
class ApiTokenAuthenticator
|
||||
{
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
namespace Controllers;
|
||||
namespace Api\Controllers;
|
||||
|
||||
use Core\Request;
|
||||
use Core\Response;
|
||||
use Api\Core\Request;
|
||||
use Api\Core\Response;
|
||||
use ElixForms\ElixFormsClient;
|
||||
|
||||
class ExampleController
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
namespace Api\Controllers;
|
||||
|
||||
use Core\Request;
|
||||
use Core\Response;
|
||||
use ElixForms\ElixFormsClient;
|
||||
use ElixForms\Exceptions\ElixFormsException;
|
||||
|
||||
class ExternalElixFormsController
|
||||
{
|
||||
private ElixFormsClient $elixFormsClient;
|
||||
|
||||
public function __construct(ElixFormsClient $elixFormsClient)
|
||||
{
|
||||
$this->elixFormsClient = $elixFormsClient;
|
||||
}
|
||||
|
||||
public function status(Request $req, Response $res)
|
||||
{
|
||||
try {
|
||||
$response = $this->elixFormsClient->request('GET', '/status');
|
||||
return $res->json([
|
||||
'status' => $response['status'],
|
||||
'body' => $response['json'] ?? $response['body']
|
||||
]);
|
||||
} catch (ElixFormsException $e) {
|
||||
return $res->json(['error' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function login(Request $req, Response $res)
|
||||
{
|
||||
$data = $req->body();
|
||||
$username = $data['username'] ?? '';
|
||||
$password = $data['password'] ?? '';
|
||||
|
||||
try {
|
||||
$token = $this->elixFormsClient->auth()->login($username, $password);
|
||||
return $res->json(['authToken' => $token]);
|
||||
} catch (ElixFormsException $e) {
|
||||
return $res->json(['error' => $e->getMessage()], 401);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,10 @@
|
||||
<?php
|
||||
namespace Controllers;
|
||||
namespace Api\Controllers\v1;
|
||||
|
||||
use Core\Request;
|
||||
use Core\Response;
|
||||
use Api\Core\Request;
|
||||
use Api\Core\Response;
|
||||
|
||||
class UsersController {
|
||||
private $externalService;
|
||||
|
||||
public function __construct(\Services\ExternalApiService $externalService) {
|
||||
$this->externalService = $externalService;
|
||||
}
|
||||
|
||||
public function index(Request $req, Response $res) {
|
||||
return $res->json([
|
||||
'users' => ['Mario', 'Luigi']
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace Core;
|
||||
namespace Api\Core;
|
||||
|
||||
class Config {
|
||||
private $config;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace Core;
|
||||
namespace Api\Core;
|
||||
|
||||
/**
|
||||
* Simple service container / dependency injector.
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
namespace Core;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ElixFormsException extends Exception {
|
||||
// Custom exception per errori specifici di elixForms (es. login, lookup)
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace Core;
|
||||
namespace Api\Core;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace Core;
|
||||
namespace Api\Core\Log;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\SyslogHandler;
|
||||
@@ -7,7 +7,7 @@ use Monolog\Processor\UidProcessor;
|
||||
use Monolog\Formatter\JsonFormatter;
|
||||
|
||||
class LoggerFactory {
|
||||
public static function create(string $name = null): Logger {
|
||||
public static function create(?string $name): Logger {
|
||||
$ident = $name ?? 'api';
|
||||
$logger = new Logger($ident);
|
||||
$handler = new SyslogHandler($ident, LOG_USER);
|
||||
@@ -1,14 +1,12 @@
|
||||
<?php
|
||||
namespace Core\RateLimiter;
|
||||
|
||||
use Core\Config;
|
||||
namespace Api\Core\RateLimiter;
|
||||
|
||||
class FileRateLimiter implements RateLimiterInterface {
|
||||
private $dir;
|
||||
private $requests;
|
||||
private $window;
|
||||
|
||||
public function __construct(string $storageDir = null, int $requests = 100, int $window = 60) {
|
||||
public function __construct(?string $storageDir, int $requests = 100, int $window = 60) {
|
||||
$this->dir = $storageDir ?? sys_get_temp_dir() . '/api_rate_limit';
|
||||
if (!is_dir($this->dir)) {
|
||||
mkdir($this->dir, 0700, true);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace Core\RateLimiter;
|
||||
namespace Api\Core\RateLimiter;
|
||||
|
||||
class InMemoryRateLimiter implements RateLimiterInterface {
|
||||
private $requests;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace Core\RateLimiter;
|
||||
namespace Api\Core\RateLimiter;
|
||||
|
||||
interface RateLimiterInterface {
|
||||
public function allow(string $key): bool;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace Core;
|
||||
namespace Api\Core;
|
||||
|
||||
class Request {
|
||||
public function method() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace Core;
|
||||
namespace Api\Core;
|
||||
|
||||
class Response {
|
||||
public function json($data, $status = 200) {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
<?php
|
||||
namespace Core;
|
||||
namespace Api\Core;
|
||||
|
||||
class Router {
|
||||
private $routes = [];
|
||||
private $container;
|
||||
|
||||
public function __construct(\Core\Container $container) {
|
||||
public function __construct(Container $container) {
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace Helpers;
|
||||
namespace Api\Helpers;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
namespace Services;
|
||||
|
||||
use Core\Config;
|
||||
use Core\HttpClient;
|
||||
|
||||
class ExternalApiService
|
||||
{
|
||||
private $config;
|
||||
private $httpClient;
|
||||
|
||||
public function __construct(Config $config, HttpClient $httpClient)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->httpClient = $httpClient;
|
||||
}
|
||||
|
||||
public function getStatus()
|
||||
{
|
||||
$url = rtrim($this->config->get('elixforms_api_base_url'), '/') . '/status';
|
||||
$token = $this->config->secret('elixforms_api_token');
|
||||
$headers = ['Authorization' => 'Bearer ' . $token];
|
||||
return $this->httpClient->get($url, $headers);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace ElixForms;
|
||||
|
||||
use ElixForms<Auth\ElixFormsAuthenticationClient as AuthClient;
|
||||
use ElixForms\Auth\ElixFormsAuthenticationClient as AuthClient;
|
||||
use ElixForms\Exceptions\ElixFormsException;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Core\RateLimiter\FileRateLimiter;
|
||||
use Api\Core\RateLimiter\FileRateLimiter;
|
||||
|
||||
final class RateLimiterTest extends TestCase {
|
||||
private $dir;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
<?php
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Core\RateLimiter\RateLimiterInterface;
|
||||
use Core\RateLimiter\InMemoryRateLimiter;
|
||||
use Api\Core\RateLimiter\InMemoryRateLimiter;
|
||||
|
||||
final class SomeControllerTest extends TestCase {
|
||||
public function testRateLimitedPath() {
|
||||
|
||||
Reference in New Issue
Block a user