2 namespace Robo\Task\Base;
4 use Lurker\Event\FilesystemEvent;
5 use Lurker\ResourceWatcher;
7 use Robo\Task\BaseTask;
10 * Runs task when specified file or dir was changed.
11 * Uses Lurker library.
16 * ->monitor('composer.json', function() {
17 * $this->taskComposerUpdate()->run();
18 * })->monitor('src', function() {
19 * $this->taskExec('phpunit')->run();
24 class Watch extends BaseTask
34 protected $monitor = [];
44 public function __construct($bindTo)
46 $this->bindTo = $bindTo;
50 * @param string|string[] $paths
51 * @param \Closure $callable
55 public function monitor($paths, \Closure $callable)
57 if (!is_array($paths)) {
60 $this->monitor[] = [$paths, $callable];
69 if (!class_exists('Lurker\\ResourceWatcher')) {
70 return Result::errorMissingPackage($this, 'ResourceWatcher', 'henrikbjorn/lurker');
73 $watcher = new ResourceWatcher();
75 foreach ($this->monitor as $k => $monitor) {
76 /** @var \Closure $closure */
77 $closure = $monitor[1];
78 $closure->bindTo($this->bindTo);
79 foreach ($monitor[0] as $i => $dir) {
80 $watcher->track("fs.$k.$i", $dir, FilesystemEvent::MODIFY);
81 $this->printTaskInfo('Watching {dir} for changes...', ['dir' => $dir]);
82 $watcher->addListener("fs.$k.$i", $closure);
87 return Result::success($this);