| 1 | <?php |
||
| 8 | class SynchronousProcess implements Runnable |
||
| 9 | { |
||
| 10 | protected $id; |
||
| 11 | |||
| 12 | protected $task; |
||
| 13 | |||
| 14 | protected $output; |
||
| 15 | protected $errorOutput; |
||
| 16 | protected $executionTime; |
||
| 17 | |||
| 18 | use ProcessCallbacks; |
||
| 19 | |||
| 20 | public function __construct(callable $task, int $id) |
||
| 25 | |||
| 26 | public static function create(callable $task, int $id): self |
||
| 30 | |||
| 31 | public function getId(): int |
||
| 35 | |||
| 36 | public function getPid(): ?int |
||
| 40 | |||
| 41 | public function start() |
||
| 42 | { |
||
| 43 | $startTime = microtime(true); |
||
| 44 | |||
| 45 | try { |
||
| 46 | $this->output = $this->task instanceof Task |
||
| 47 | ? $this->task->run() |
||
| 48 | : call_user_func($this->task); |
||
| 49 | } catch (Throwable $throwable) { |
||
| 50 | $this->errorOutput = $throwable; |
||
| 51 | } finally { |
||
| 52 | $this->executionTime = microtime(true) - $startTime; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | public function stop() |
||
| 59 | |||
| 60 | public function getOutput() |
||
| 64 | |||
| 65 | public function getErrorOutput() |
||
| 69 | |||
| 70 | public function getCurrentExecutionTime(): float |
||
| 74 | |||
| 75 | protected function resolveErrorOutput(): Throwable |
||
| 79 | } |
||
| 80 |