Enable Discovery debug logging for production troubleshooting
- Add DISCOVERY_LOG_LEVEL=debug - Add DISCOVERY_SHOW_PROGRESS=true - Temporary changes for debugging InitializerProcessor fixes on production
This commit is contained in:
32
src/Framework/Random/RandomGenerator.php
Normal file
32
src/Framework/Random/RandomGenerator.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\Random;
|
||||
|
||||
interface RandomGenerator
|
||||
{
|
||||
/**
|
||||
* Generate cryptographically secure random bytes.
|
||||
*
|
||||
* @param int $length Number of bytes to generate
|
||||
* @return string Random bytes
|
||||
*/
|
||||
public function bytes(int $length): string;
|
||||
|
||||
/**
|
||||
* Generate a random integer between min and max (inclusive).
|
||||
*
|
||||
* @param int $min Minimum value (inclusive)
|
||||
* @param int $max Maximum value (inclusive)
|
||||
* @return int Random integer
|
||||
*/
|
||||
public function int(int $min, int $max): int;
|
||||
|
||||
/**
|
||||
* Generate a random float between 0.0 and 1.0 (0.0 inclusive, 1.0 exclusive).
|
||||
*
|
||||
* @return float Random float between 0.0 and 1.0
|
||||
*/
|
||||
public function float(): float;
|
||||
}
|
||||
32
src/Framework/Random/RandomGeneratorInitializer.php
Normal file
32
src/Framework/Random/RandomGeneratorInitializer.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\Random;
|
||||
|
||||
use App\Framework\DI\Container;
|
||||
use App\Framework\DI\Initializer;
|
||||
use App\Framework\Security\CsrfTokenGenerator;
|
||||
|
||||
final readonly class RandomGeneratorInitializer
|
||||
{
|
||||
public function __construct(
|
||||
private Container $container
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize random-related services in the container.
|
||||
*/
|
||||
#[Initializer]
|
||||
public function __invoke(): void
|
||||
{
|
||||
// Register core RandomGenerator
|
||||
$this->container->singleton(RandomGenerator::class, SecureRandomGenerator::class);
|
||||
|
||||
// Register specific generators that depend on RandomGenerator
|
||||
$this->container->singleton(CsrfTokenGenerator::class, function (Container $container): CsrfTokenGenerator {
|
||||
return new CsrfTokenGenerator($container->get(RandomGenerator::class));
|
||||
});
|
||||
}
|
||||
}
|
||||
34
src/Framework/Random/SecureRandomGenerator.php
Normal file
34
src/Framework/Random/SecureRandomGenerator.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\Random;
|
||||
|
||||
use Random\Engine\Secure;
|
||||
use Random\IntervalBoundary;
|
||||
use Random\Randomizer;
|
||||
|
||||
final readonly class SecureRandomGenerator implements RandomGenerator
|
||||
{
|
||||
private Randomizer $randomizer;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->randomizer = new Randomizer(new Secure());
|
||||
}
|
||||
|
||||
public function bytes(int $length): string
|
||||
{
|
||||
return $this->randomizer->getBytes($length);
|
||||
}
|
||||
|
||||
public function int(int $min, int $max): int
|
||||
{
|
||||
return $this->randomizer->getInt($min, $max);
|
||||
}
|
||||
|
||||
public function float(): float
|
||||
{
|
||||
return $this->randomizer->getFloat(0, 1, IntervalBoundary::ClosedOpen);
|
||||
}
|
||||
}
|
||||
59
src/Framework/Random/TestableRandomGenerator.php
Normal file
59
src/Framework/Random/TestableRandomGenerator.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\Random;
|
||||
|
||||
/**
|
||||
* Deterministic random generator for testing purposes.
|
||||
*
|
||||
* This generator produces predictable output which is useful for testing
|
||||
* scenarios where you need consistent, reproducible random values.
|
||||
*/
|
||||
final class TestableRandomGenerator implements RandomGenerator
|
||||
{
|
||||
private int $counter = 0;
|
||||
|
||||
public function __construct(
|
||||
private readonly string $seed = 'test'
|
||||
) {
|
||||
}
|
||||
|
||||
public function bytes(int $length): string
|
||||
{
|
||||
$result = '';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$result .= chr(($this->getNextValue() % 256));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function int(int $min, int $max): int
|
||||
{
|
||||
$range = $max - $min + 1;
|
||||
|
||||
return $min + ($this->getNextValue() % $range);
|
||||
}
|
||||
|
||||
public function float(): float
|
||||
{
|
||||
return ($this->getNextValue() % 1000000) / 1000000.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the generator to its initial state.
|
||||
*/
|
||||
public function reset(): void
|
||||
{
|
||||
$this->counter = 0;
|
||||
}
|
||||
|
||||
private function getNextValue(): int
|
||||
{
|
||||
$value = crc32($this->seed . $this->counter);
|
||||
$this->counter++;
|
||||
|
||||
return abs($value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user