Lines 96.42% 27 / 28
Functions and Methods 75.00% 3 / 4
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
SourceWatcher 96.42% 27 / 28 75.00% 3 / 4 13 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 startWatching 100.00% 15 / 15 100.00% 1 / 1 4
 stopWatching 100.00% 2 / 2 100.00% 1 / 1 1
 scanForChanges 90.00% 9 / 10 0.00% 0 / 1 7.05
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker\Reload;
6
7use Psr\Log\LoggerInterface;
8use Stewart\Contracts\Time\Duration;
9use Stewart\Runtime\Broker\DaemonLaunchOptions;
10use Stewart\Runtime\Time\RepeatingTimer;
11use Stewart\Support\Time\Timers;
12use Throwable;
13
14final class SourceWatcher
15{
16    private const int SCAN_INTERVAL_SECONDS = 1;
17
18    private ?RepeatingTimer $timer = null;
19
20    private ?SourcePaths $paths = null;
21
22    private ?SourceChangeTracker $sources = null;
23
24    /** @var array<string, SourceChangeTracker> */
25    private array $restartOnlyFiles = [];
26
27    public function __construct(
28        private readonly DaemonLaunchOptions $launch,
29        private readonly SourcePathFinder $pathFinder,
30        private readonly SourceScanner $scanner,
31        private readonly ReloadCoordinator $reloads,
32        private readonly Timers $timers,
33        private readonly LoggerInterface $logger,
34    ) {}
35
36    public function startWatching(): void
37    {
38        if (!$this->launch->watch || $this->timer !== null) {
39            return;
40        }
41
42        $this->paths = $this->pathFinder->findSourcePaths();
43        $this->sources = new SourceChangeTracker($this->scanner->scanFingerprint($this->paths->reloading));
44        $this->restartOnlyFiles = [];
45
46        foreach ($this->paths->restartOnly as $file) {
47            $this->restartOnlyFiles[$file] = new SourceChangeTracker($this->scanner->scanFingerprint([$file]));
48        }
49
50        $this->timer = new RepeatingTimer(
51            $this->timers,
52            Duration::seconds(self::SCAN_INTERVAL_SECONDS),
53            $this->scanForChanges(...),
54            fn(Throwable $e) => $this->logger->warning('Looking for changed files failed', ['exception' => $e]),
55        );
56        $this->timer->start();
57        $this->logger->info('Watching for changes; a saved edit reloads the workers', ['paths' => $this->paths->reloading]);
58    }
59
60    public function stopWatching(): void
61    {
62        $this->timer?->stop();
63        $this->timer = null;
64    }
65
66    public function scanForChanges(): void
67    {
68        if ($this->paths === null || $this->sources === null) {
69            return;
70        }
71
72        $settled = $this->sources->findSettledChange($this->scanner->scanFingerprint($this->paths->reloading));
73
74        // Only a reload that started takes the change; one refused now is asked for again on the next scan.
75        if ($settled !== null && $this->reloads->requestReload(ReloadTrigger::Watch) === ReloadRequestOutcome::Started) {
76            $this->sources->markApplied($settled);
77        }
78
79        foreach ($this->restartOnlyFiles as $file => $tracker) {
80            $changed = $tracker->findSettledChange($this->scanner->scanFingerprint([$file]));
81
82            if ($changed !== null) {
83                $tracker->markApplied($changed);
84                $this->logger->warning('A changed file needs a restart of Stewart; a reload does not apply it', ['file' => $file]);
85            }
86        }
87    }
88}