fix namespaces and references
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user