2 namespace Robo\Task\Base;
4 use Lurker\ResourceWatcher;
6 use Robo\Task\BaseTask;
9 * Runs task when specified file or dir was changed.
10 * Uses Lurker library.
11 * Monitor third parameter takes Lurker filesystem events types to watch.
12 * By default its set to MODIFY event.
20 * $this->taskComposerUpdate()->run();
25 * $this->taskExec('phpunit')->run();
27 * \Lurker\Event\FilesystemEvent::ALL
34 * \Lurker\Event\FilesystemEvent::CREATE,
35 * \Lurker\Event\FilesystemEvent::DELETE
41 class Watch extends BaseTask
51 protected $monitor = [];
61 public function __construct($bindTo)
63 $this->bindTo = $bindTo;
67 * @param string|string[] $paths
68 * @param \Closure $callable
69 * @param int|int[] $events
73 public function monitor($paths, \Closure $callable, $events = 2)
75 $this->monitor[] = [(array)$paths, $callable, (array)$events];
84 if (!class_exists('Lurker\\ResourceWatcher')) {
85 return Result::errorMissingPackage($this, 'ResourceWatcher', 'henrikbjorn/lurker');
88 $watcher = new ResourceWatcher();
90 foreach ($this->monitor as $k => $monitor) {
91 /** @var \Closure $closure */
92 $closure = $monitor[1];
93 $closure->bindTo($this->bindTo);
94 foreach ($monitor[0] as $i => $dir) {
95 foreach ($monitor[2] as $j => $event) {
96 $watcher->track("fs.$k.$i.$j", $dir, $event);
97 $watcher->addListener("fs.$k.$i.$j", $closure);
99 $this->printTaskInfo('Watching {dir} for changes...', ['dir' => $dir]);
104 return Result::success($this);