This commit is contained in:
2026-01-18 23:53:31 +03:00
parent 17eec3ebc0
commit 6a7325f0fd
334 changed files with 1074 additions and 680 deletions

View File

@@ -1,24 +1,16 @@
<?php
/*
// MODULE_CONTRACT: Provides a Saloon middleware for logging API requests and responses, with sensitive data masking.
// RESPONSIBILITY: To intercept and log outgoing API requests and incoming API responses, providing visibility into the communication flow for debugging and monitoring purposes, while also sanitizing sensitive information from logs to maintain security and compliance.
// INPUTS: Psr\Log\LoggerInterface for logging; Saloon `Request` and `Response` objects.
// OUTPUTS: Logs detailed request and response information (with masked sensitive data) to the configured logger.
// LINKS: Core/AlfaBankConnector.php, Sbp/SbpConnector.php, README.md#logging
*/
declare(strict_types=1);
namespace Apoloz\AlfaBank\Http\Middleware;
// <SECTION name="module_imports" intent="Import necessary classes for logging and Saloon HTTP components.">
// <BELIEF_STATE "These imports are fundamental for the logging middleware. `LoggerInterface` from PSR-3 defines the contract for logging, allowing flexibility in the choice of logging implementation. `Request` and `Response` from Saloon are the HTTP objects that this middleware will observe and log, providing insights into the API communication lifecycle.">
use Psr\Log\LoggerInterface;
use Saloon\Contracts\RequestMiddleware;
use Saloon\Contracts\ResponseMiddleware;
use Saloon\Http\PendingRequest;
use Saloon\Http\Response;
// </SECTION>
// <SECTION name="class_definition" intent="Define the API request and response logging middleware.">
// <BELIEF_STATE "The `LoggingMiddleware` plays a crucial role in the observability of the SDK. By implementing `onRequest` and `onResponse` methods, it captures detailed information about HTTP interactions, including headers and body. Crucially, it incorporates sensitive data masking to prevent credentials and personal information from being exposed in logs, balancing transparency with security and compliance requirements.">
class LoggingMiddleware
class LoggingMiddleware implements RequestMiddleware, ResponseMiddleware
{
// <SECTION name="constructor_definition" intent="Initialize the middleware with a logger instance.">
// <BELIEF_STATE "The constructor injects a `LoggerInterface` instance, making the logging functionality easily testable and configurable. This adheres to dependency inversion principles, allowing the middleware to log messages without being tightly coupled to a specific logging implementation.">

View File

@@ -1,26 +1,17 @@
<?php
/*
// MODULE_CONTRACT: Provides a Saloon middleware to ensure each API request includes a unique `X-Request-ID` header.
// RESPONSIBILITY: To automatically generate and attach a UUID-based `X-Request-ID` header to outgoing API requests if one is not already present, thereby facilitating request tracking, idempotency, and debugging across distributed systems.
// INPUTS: Saloon `PendingRequest` object.
// OUTPUTS: Modified `PendingRequest` with an `X-Request-ID` header.
// LINKS: Core/AlfaBankConnector.php, Sbp/SbpConnector.php, README.md#request-tracking
*/
declare(strict_types=1);
namespace Apoloz\AlfaBank\Http\Middleware;
// <SECTION name="module_imports" intent="Import necessary classes for Saloon PendingRequest.">
// <BELIEF_STATE "These imports are critical for the middleware's operation. `PendingRequest` from Saloon represents the request being prepared for dispatch, allowing modification before sending.">
use Apoloz\AlfaBank\Utils\Uuid;
use Saloon\Contracts\RequestMiddleware;
use Saloon\Http\PendingRequest;
// </SECTION>
// <SECTION name="class_definition" intent="Define the Request ID generation middleware.">
// <BELIEF_STATE "The `RequestIdMiddleware` is a lightweight yet crucial component for API observability and idempotency. It ensures that every outgoing request is uniquely identifiable through an `X-Request-ID` header. This is invaluable for tracing requests through complex microservice architectures, debugging issues, and preventing duplicate processing on the server-side, adhering to best practices for API communication.">
class RequestIdMiddleware
class RequestIdMiddleware implements RequestMiddleware
{
// <SECTION name="invoke_method" intent="Add a unique X-Request-ID header to the pending request.">
// <BELIEF_STATE "The `__invoke` method is executed for each outgoing API request. It first checks if an `X-Request-ID` header already exists. If not, it generates a new unique ID and adds it to the request headers. This conditional logic ensures that existing IDs are respected while guaranteeing that every request has a unique identifier, making request tracking reliable and automatic.">
public function __invoke(PendingRequest $pendingRequest): void
{
if (!$pendingRequest->headers()->get('X-Request-ID')) {