1: <?php declare(strict_types = 1);
2:
3: namespace ApiGen\Scheduler;
4:
5: use ApiGen\Scheduler;
6: use ApiGen\Task\Task;
7: use ApiGen\Task\TaskHandler;
8: use SplQueue;
9:
10:
11: /**
12: * @template TTask of Task
13: * @template TResult
14: * @implements Scheduler<TTask, TResult>
15: */
16: class SimpleScheduler implements Scheduler
17: {
18: /** @var SplQueue<TTask> */
19: protected SplQueue $tasks;
20:
21:
22: /**
23: * @param TaskHandler<TTask, TResult> $handler
24: */
25: public function __construct(
26: protected TaskHandler $handler,
27: ) {
28: $this->tasks = new SplQueue();
29: }
30:
31:
32: /**
33: * @param TTask $task
34: */
35: public function schedule(Task $task): void
36: {
37: $this->tasks->enqueue($task);
38: }
39:
40:
41: /**
42: * @return iterable<TTask, TResult>
43: */
44: public function process(): iterable
45: {
46: while (!$this->tasks->isEmpty()) {
47: $task = $this->tasks->dequeue();
48: $result = $this->handler->handle($task);
49: yield $task => $result;
50: }
51: }
52: }
53: