1. <?php
  2. class ConfigDryRun {
  3. private $config;
  4. private $timeout;
  5. private $startTime;
  6. private $isRunning = false;
  7. private $output = [];
  8. private $error = null;
  9. public function __construct(array $config, int $timeout) {
  10. $this->config = $config;
  11. $this->timeout = $timeout;
  12. $this->startTime = time();
  13. }
  14. public function run(): bool {
  15. if ($this->isRunning) {
  16. return true; // Already running
  17. }
  18. $this->isRunning = true;
  19. $this->output = [];
  20. $this->error = null;
  21. $this->executeConfig();
  22. $this->isRunning = false;
  23. return true; // Indicate successful execution
  24. }
  25. private function executeConfig(): void {
  26. $keys = array_keys($this->config);
  27. foreach ($keys as $key) {
  28. $value = $this->config[$key];
  29. // Simulate execution with a timeout
  30. $result = $this->executeValue($key, $value);
  31. if ($result === false) {
  32. $this->error = "Error executing config value for key: " . $key . ". Error: " . $result;
  33. return; // Stop execution on error
  34. }
  35. $this->output[$key] = $result;
  36. }
  37. }
  38. private function executeValue(string $key, $value): ?string {
  39. $startTime = time();
  40. $timeout = $this->timeout;
  41. if (is_callable($value)) {
  42. $result = call_user_func($value); // Execute function
  43. } elseif (is_string($value)) {
  44. $result = "String Value: " . $value; // Simulate string value
  45. } elseif (is_array($value)) {
  46. $result = "Array Value: " . json_encode($value); //Simulate array
  47. } else {
  48. $result = "Unsupported value type.";
  49. }
  50. if (time() - $startTime > $timeout) {
  51. $this->error = "Timeout exceeded for key: " . $key;
  52. return false;
  53. }
  54. return $result;
  55. }
  56. public function getOutput(): array {
  57. return $this->output;
  58. }
  59. public function getError(): string|null {
  60. return $this->error;
  61. }
  62. }
  63. ?>

Add your comment