1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PhpUnitWatcher; |
4
|
|
|
|
5
|
|
|
use Clue\React\Stdio\Stdio; |
6
|
|
|
use React\EventLoop\Factory; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
use Spatie\PhpUnitWatcher\Screens\Phpunit; |
9
|
|
|
use Yosymfony\ResourceWatcher\ResourceWatcher; |
10
|
|
|
use Yosymfony\ResourceWatcher\ResourceCacheMemory; |
11
|
|
|
|
12
|
|
|
class Watcher |
13
|
|
|
{ |
14
|
|
|
/** @var \Symfony\Component\Finder\Finder */ |
15
|
|
|
protected $finder; |
16
|
|
|
|
17
|
|
|
/** @var $string */ |
18
|
|
|
protected $phpunitArguments; |
19
|
|
|
|
20
|
|
|
/** @var bool */ |
21
|
|
|
protected $notifications; |
22
|
|
|
|
23
|
|
|
/** @var \React\EventLoop\LibEventLoop */ |
24
|
|
|
protected $loop; |
25
|
|
|
|
26
|
|
|
/** @var \Spatie\PhpUnitWatcher\Terminal */ |
27
|
|
|
protected $terminal; |
28
|
|
|
|
29
|
|
|
public function __construct(Finder $finder) |
30
|
|
|
{ |
31
|
|
|
$this->finder = $finder; |
32
|
|
|
|
33
|
|
|
$this->loop = Factory::create(); |
34
|
|
|
|
35
|
|
|
$this->terminal = new Terminal(new Stdio($this->loop)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function usePhpunitArguments(string $arguments) |
39
|
|
|
{ |
40
|
|
|
$this->phpunitArguments = $arguments; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setNotifications(bool $status) { |
46
|
|
|
$this->notifications = $status; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function startWatching() |
50
|
|
|
{ |
51
|
|
|
$this->terminal->displayScreen(new Phpunit($this->phpunitArguments, $this->notifications), false); |
52
|
|
|
|
53
|
|
|
$watcher = new ResourceWatcher(new ResourceCacheMemory()); |
54
|
|
|
|
55
|
|
|
$watcher->setFinder($this->finder); |
56
|
|
|
|
57
|
|
|
$this->loop->addPeriodicTimer(1 / 4, function () use ($watcher) { |
58
|
|
|
if (! $this->terminal->isDisplayingScreen(Phpunit::class)) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$watcher->findChanges(); |
63
|
|
|
|
64
|
|
|
if ($watcher->hasChanges()) { |
65
|
|
|
$this->terminal->refreshScreen(); |
66
|
|
|
} |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
$this->loop->run(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|