1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PhpUnitWatcher; |
4
|
|
|
|
5
|
|
|
use Clue\React\Stdio\Stdio; |
6
|
|
|
use React\EventLoop\Factory; |
7
|
|
|
use React\Stream\ThroughStream; |
8
|
|
|
use Spatie\PhpUnitWatcher\Screens\Phpunit; |
9
|
|
|
use Symfony\Component\Finder\Finder; |
10
|
|
|
use Yosymfony\ResourceWatcher\Crc32ContentHash; |
11
|
|
|
use Yosymfony\ResourceWatcher\ResourceCacheMemory; |
12
|
|
|
use Yosymfony\ResourceWatcher\ResourceWatcher; |
13
|
|
|
|
14
|
|
|
class Watcher |
15
|
|
|
{ |
16
|
|
|
/** @var \Symfony\Component\Finder\Finder */ |
17
|
|
|
protected $finder; |
18
|
|
|
|
19
|
|
|
/** @var \React\EventLoop\LibEventLoop */ |
20
|
|
|
protected $loop; |
21
|
|
|
|
22
|
|
|
/** @var \Spatie\PhpUnitWatcher\Terminal */ |
23
|
|
|
protected $terminal; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
protected $options; |
27
|
|
|
|
28
|
|
|
public function __construct(Finder $finder, array $options) |
29
|
|
|
{ |
30
|
|
|
$this->finder = $finder; |
31
|
|
|
|
32
|
|
|
$this->loop = Factory::create(); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$this->terminal = new Terminal($this->buildStdio()); |
35
|
|
|
|
36
|
|
|
$this->options = $options; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function startWatching() |
40
|
|
|
{ |
41
|
|
|
$this->terminal->displayScreen(new Phpunit($this->options), false); |
42
|
|
|
|
43
|
|
|
$watcher = new ResourceWatcher(new ResourceCacheMemory(), $this->finder, new Crc32ContentHash()); |
44
|
|
|
|
45
|
|
|
$this->loop->addPeriodicTimer(1 / 4, function () use ($watcher) { |
46
|
|
|
if (! $this->terminal->isDisplayingScreen(Phpunit::class)) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($watcher->findChanges()->hasChanges()) { |
51
|
|
|
$this->terminal->refreshScreen(); |
52
|
|
|
} |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
$this->loop->run(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function buildStdio() |
59
|
|
|
{ |
60
|
|
|
$output = null; |
61
|
|
|
|
62
|
|
|
if (OS::isOnWindows()) { |
63
|
|
|
// Interaction on windows is currently not supported |
64
|
|
|
fclose(STDIN); |
65
|
|
|
|
66
|
|
|
// Simple fix for windows compatibility since we don't write a lot of data at once |
67
|
|
|
// https://github.com/clue/reactphp-stdio/issues/83#issuecomment-546678609 |
68
|
|
|
$output = new ThroughStream(static function ($data) { |
69
|
|
|
echo $data; |
70
|
|
|
}); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return new Stdio($this->loop, null, $output); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..