Files
michaelschiemer/src/Framework/Async/AsyncOperationFactory.php
Michael Schiemer 55a330b223 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
2025-08-11 20:13:26 +02:00

129 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Async;
use App\Framework\Core\ValueObjects\Duration;
use App\Framework\DateTime\Clock;
use App\Framework\DateTime\Timer;
use Fiber;
/**
* Factory für häufig verwendete asynchrone Operationen
*/
final readonly class AsyncOperationFactory
{
public function __construct(
private FiberManager $fiberManager,
private Clock $clock,
private Timer $timer
) {
}
/**
* Erstellt eine asynchrone Dateileseoperation
*/
public function readFile(string $path): Fiber
{
return $this->fiberManager->async(
fn () => file_get_contents($path) ?: throw new \RuntimeException("Failed to read file: $path")
);
}
/**
* Erstellt eine asynchrone Dateischreiboperation
*/
public function writeFile(string $path, string $content): Fiber
{
return $this->fiberManager->async(
fn () => file_put_contents($path, $content) ?: throw new \RuntimeException("Failed to write file: $path")
);
}
/**
* Erstellt eine asynchrone Verzeichnisleseoperation
*/
public function listDirectory(string $path): Fiber
{
return $this->fiberManager->async(
fn () => scandir($path) ?: throw new \RuntimeException("Failed to list directory: $path")
);
}
/**
* Erstellt eine asynchrone HTTP-Request-Operation (Platzhalter)
*/
public function httpRequest(string $url, array $options = []): Fiber
{
return $this->fiberManager->async(function () use ($url, $options) {
// Hier würde eine echte HTTP-Client-Integration stehen
// Für jetzt nur ein Platzhalter
return ['url' => $url, 'options' => $options, 'response' => 'async response'];
});
}
/**
* Erstellt eine asynchrone Datenbank-Query-Operation (Platzhalter)
*/
public function databaseQuery(string $query, array $params = []): Fiber
{
return $this->fiberManager->async(function () use ($query, $params) {
// Hier würde eine echte Datenbank-Integration stehen
return ['query' => $query, 'params' => $params, 'result' => 'async db result'];
});
}
/**
* Erstellt eine asynchrone Sleep-Operation
* @deprecated Use sleepDuration() instead
*/
public function sleep(float $seconds): Fiber
{
return $this->sleepDuration(Duration::fromSeconds($seconds));
}
/**
* Create async sleep operation using Duration
*/
public function sleepDuration(Duration $duration): Fiber
{
return $this->fiberManager->async(function () use ($duration) {
$this->timer->sleep($duration);
return $duration;
});
}
/**
* Measure execution time of an operation
*/
public function measureExecution(callable $operation): Fiber
{
return $this->fiberManager->async(function () use ($operation) {
$startTime = $this->clock->time();
$result = $operation();
$endTime = $this->clock->time();
$duration = $startTime->diff($endTime);
return [
'result' => $result,
'duration' => $duration,
'start_time' => $startTime,
'end_time' => $endTime,
'milliseconds' => $duration->toMilliseconds(),
];
});
}
/**
* Create operation with timeout
*/
public function withTimeout(callable $operation, Duration $timeout): Fiber
{
return $this->fiberManager->async(function () use ($operation, $timeout) {
return $this->fiberManager->withTimeoutDuration($operation, $timeout);
});
}
}