<?php
class ConfigDryRun {
private $config;
private $timeout;
private $startTime;
private $isRunning = false;
private $output = [];
private $error = null;
public function __construct(array $config, int $timeout) {
$this->config = $config;
$this->timeout = $timeout;
$this->startTime = time();
}
public function run(): bool {
if ($this->isRunning) {
return true; // Already running
}
$this->isRunning = true;
$this->output = [];
$this->error = null;
$this->executeConfig();
$this->isRunning = false;
return true; // Indicate successful execution
}
private function executeConfig(): void {
$keys = array_keys($this->config);
foreach ($keys as $key) {
$value = $this->config[$key];
// Simulate execution with a timeout
$result = $this->executeValue($key, $value);
if ($result === false) {
$this->error = "Error executing config value for key: " . $key . ". Error: " . $result;
return; // Stop execution on error
}
$this->output[$key] = $result;
}
}
private function executeValue(string $key, $value): ?string {
$startTime = time();
$timeout = $this->timeout;
if (is_callable($value)) {
$result = call_user_func($value); // Execute function
} elseif (is_string($value)) {
$result = "String Value: " . $value; // Simulate string value
} elseif (is_array($value)) {
$result = "Array Value: " . json_encode($value); //Simulate array
} else {
$result = "Unsupported value type.";
}
if (time() - $startTime > $timeout) {
$this->error = "Timeout exceeded for key: " . $key;
return false;
}
return $result;
}
public function getOutput(): array {
return $this->output;
}
public function getError(): string|null {
return $this->error;
}
}
?>
Add your comment