32 lines
814 B
PHP
32 lines
814 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\BusProNet\Transport;
|
|
|
|
use App\Service\RequestIdGenerator;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
/**
|
|
* Ids for BPN requests: the id of the current HTTP request, or a fresh one on the CLI, with a
|
|
* running number, so the log entries and dumps of one page load can be found together.
|
|
*/
|
|
class RequestIdSequence
|
|
{
|
|
private int $counter = 0;
|
|
|
|
public function __construct(
|
|
private readonly RequestStack $requestStack,
|
|
private readonly RequestIdGenerator $requestIdGenerator,
|
|
) {
|
|
}
|
|
|
|
public function next(): string
|
|
{
|
|
$baseId = $this->requestStack->getMainRequest()?->attributes->get('request_id')
|
|
?? $this->requestIdGenerator->generateBaseId();
|
|
|
|
return $baseId.'_'.++$this->counter;
|
|
}
|
|
}
|