fix namespaces and references
This commit is contained in:
+10
-9
@@ -4,14 +4,15 @@
|
|||||||
|
|
||||||
require_once __DIR__ . '/vendor/autoload.php';
|
require_once __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
use Core\Container;
|
use Api\Core\Container;
|
||||||
use Core\Config;
|
use Api\Core\Config;
|
||||||
use Core\RateLimiter\RateLimiterInterface;
|
use Api\Core\HttpClient;
|
||||||
use Core\RateLimiter\FileRateLimiter;
|
use Api\Core\RateLimiter\RateLimiterInterface;
|
||||||
use Core\RateLimiter\InMemoryRateLimiter;
|
use Api\Core\RateLimiter\FileRateLimiter;
|
||||||
use Psr\Log\LoggerInterface;
|
use Api\Core\RateLimiter\InMemoryRateLimiter;
|
||||||
use Core\LoggerFactory;
|
use Api\Core\Log\LoggerFactory;
|
||||||
use ElixForms\ElixFormsClient;
|
use ElixForms\ElixFormsClient;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
$container = new Container();
|
$container = new Container();
|
||||||
|
|
||||||
@@ -20,8 +21,8 @@ $container->singleton(Config::class, function() use ($config) {
|
|||||||
return $config;
|
return $config;
|
||||||
});
|
});
|
||||||
|
|
||||||
$container->singleton(\Core\HttpClient::class, function() {
|
$container->singleton(HttpClient::class, function() {
|
||||||
return new \Core\HttpClient();
|
return new HttpClient();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Rate limiter driver configurabile via config or env: 'file' or 'memory'
|
// 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';
|
$container = require __DIR__ . '/../bootstrap.php';
|
||||||
|
|
||||||
use Api\Auth\ApiTokenAuthenticator;
|
use Api\Auth\ApiTokenAuthenticator;
|
||||||
use Core\Router;
|
use Api\Core\Router;
|
||||||
use Core\Request;
|
use Api\Core\Request;
|
||||||
use Core\Response;
|
use Api\Core\Response;
|
||||||
use Core\RateLimiter\RateLimiterInterface;
|
use Api\Core\RateLimiter\RateLimiterInterface;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
$logger = $container->make(LoggerInterface::class);
|
$logger = $container->make(LoggerInterface::class);
|
||||||
@@ -48,10 +48,6 @@ $router->get('/users', 'UsersController@index');
|
|||||||
$router->post('/users', 'UsersController@create');
|
$router->post('/users', 'UsersController@create');
|
||||||
$router->get('/example', 'ExampleController@test');
|
$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
|
// Rate limiting by IP address
|
||||||
$key = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
|
$key = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
|
||||||
if (!$limiter->allow($key)) {
|
if (!$limiter->allow($key)) {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Api\Auth;
|
namespace Api\Auth;
|
||||||
|
|
||||||
use Core\Config;
|
use Api\Core\Config;
|
||||||
use Core\Request;
|
use Api\Core\Request;
|
||||||
|
|
||||||
class ApiTokenAuthenticator
|
class ApiTokenAuthenticator
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Controllers;
|
namespace Api\Controllers;
|
||||||
|
|
||||||
use Core\Request;
|
use Api\Core\Request;
|
||||||
use Core\Response;
|
use Api\Core\Response;
|
||||||
use ElixForms\ElixFormsClient;
|
use ElixForms\ElixFormsClient;
|
||||||
|
|
||||||
class ExampleController
|
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
|
<?php
|
||||||
namespace Controllers;
|
namespace Api\Controllers\v1;
|
||||||
|
|
||||||
use Core\Request;
|
use Api\Core\Request;
|
||||||
use Core\Response;
|
use Api\Core\Response;
|
||||||
|
|
||||||
class UsersController {
|
class UsersController {
|
||||||
private $externalService;
|
|
||||||
|
|
||||||
public function __construct(\Services\ExternalApiService $externalService) {
|
|
||||||
$this->externalService = $externalService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function index(Request $req, Response $res) {
|
public function index(Request $req, Response $res) {
|
||||||
return $res->json([
|
return $res->json([
|
||||||
'users' => ['Mario', 'Luigi']
|
'users' => ['Mario', 'Luigi']
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Core;
|
namespace Api\Core;
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
private $config;
|
private $config;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Core;
|
namespace Api\Core;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple service container / dependency injector.
|
* 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
|
<?php
|
||||||
namespace Core;
|
namespace Api\Core;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use GuzzleHttp\Exception\RequestException;
|
use GuzzleHttp\Exception\RequestException;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Core;
|
namespace Api\Core\Log;
|
||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Monolog\Handler\SyslogHandler;
|
use Monolog\Handler\SyslogHandler;
|
||||||
@@ -7,7 +7,7 @@ use Monolog\Processor\UidProcessor;
|
|||||||
use Monolog\Formatter\JsonFormatter;
|
use Monolog\Formatter\JsonFormatter;
|
||||||
|
|
||||||
class LoggerFactory {
|
class LoggerFactory {
|
||||||
public static function create(string $name = null): Logger {
|
public static function create(?string $name): Logger {
|
||||||
$ident = $name ?? 'api';
|
$ident = $name ?? 'api';
|
||||||
$logger = new Logger($ident);
|
$logger = new Logger($ident);
|
||||||
$handler = new SyslogHandler($ident, LOG_USER);
|
$handler = new SyslogHandler($ident, LOG_USER);
|
||||||
@@ -1,14 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Core\RateLimiter;
|
namespace Api\Core\RateLimiter;
|
||||||
|
|
||||||
use Core\Config;
|
|
||||||
|
|
||||||
class FileRateLimiter implements RateLimiterInterface {
|
class FileRateLimiter implements RateLimiterInterface {
|
||||||
private $dir;
|
private $dir;
|
||||||
private $requests;
|
private $requests;
|
||||||
private $window;
|
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';
|
$this->dir = $storageDir ?? sys_get_temp_dir() . '/api_rate_limit';
|
||||||
if (!is_dir($this->dir)) {
|
if (!is_dir($this->dir)) {
|
||||||
mkdir($this->dir, 0700, true);
|
mkdir($this->dir, 0700, true);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Core\RateLimiter;
|
namespace Api\Core\RateLimiter;
|
||||||
|
|
||||||
class InMemoryRateLimiter implements RateLimiterInterface {
|
class InMemoryRateLimiter implements RateLimiterInterface {
|
||||||
private $requests;
|
private $requests;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Core\RateLimiter;
|
namespace Api\Core\RateLimiter;
|
||||||
|
|
||||||
interface RateLimiterInterface {
|
interface RateLimiterInterface {
|
||||||
public function allow(string $key): bool;
|
public function allow(string $key): bool;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Core;
|
namespace Api\Core;
|
||||||
|
|
||||||
class Request {
|
class Request {
|
||||||
public function method() {
|
public function method() {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Core;
|
namespace Api\Core;
|
||||||
|
|
||||||
class Response {
|
class Response {
|
||||||
public function json($data, $status = 200) {
|
public function json($data, $status = 200) {
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Core;
|
namespace Api\Core;
|
||||||
|
|
||||||
class Router {
|
class Router {
|
||||||
private $routes = [];
|
private $routes = [];
|
||||||
|
private $container;
|
||||||
|
|
||||||
public function __construct(\Core\Container $container) {
|
public function __construct(Container $container) {
|
||||||
$this->container = $container;
|
$this->container = $container;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Helpers;
|
namespace Api\Helpers;
|
||||||
|
|
||||||
use Psr\Log\LoggerInterface;
|
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
|
<?php
|
||||||
namespace ElixForms;
|
namespace ElixForms;
|
||||||
|
|
||||||
use ElixForms<Auth\ElixFormsAuthenticationClient as AuthClient;
|
use ElixForms\Auth\ElixFormsAuthenticationClient as AuthClient;
|
||||||
use ElixForms\Exceptions\ElixFormsException;
|
use ElixForms\Exceptions\ElixFormsException;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Core\RateLimiter\FileRateLimiter;
|
use Api\Core\RateLimiter\FileRateLimiter;
|
||||||
|
|
||||||
final class RateLimiterTest extends TestCase {
|
final class RateLimiterTest extends TestCase {
|
||||||
private $dir;
|
private $dir;
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Core\RateLimiter\RateLimiterInterface;
|
use Api\Core\RateLimiter\InMemoryRateLimiter;
|
||||||
use Core\RateLimiter\InMemoryRateLimiter;
|
|
||||||
|
|
||||||
final class SomeControllerTest extends TestCase {
|
final class SomeControllerTest extends TestCase {
|
||||||
public function testRateLimitedPath() {
|
public function testRateLimitedPath() {
|
||||||
|
|||||||
Reference in New Issue
Block a user