<?php
namespace Robo\Task\Base;
-use Lurker\Event\FilesystemEvent;
use Lurker\ResourceWatcher;
use Robo\Result;
use Robo\Task\BaseTask;
/**
* Runs task when specified file or dir was changed.
* Uses Lurker library.
+ * Monitor third parameter takes Lurker filesystem events types to watch.
+ * By default its set to MODIFY event.
*
* ``` php
* <?php
* $this->taskWatch()
- * ->monitor('composer.json', function() {
- * $this->taskComposerUpdate()->run();
- * })->monitor('src', function() {
- * $this->taskExec('phpunit')->run();
- * })->run();
+ * ->monitor(
+ * 'composer.json',
+ * function() {
+ * $this->taskComposerUpdate()->run();
+ * }
+ * )->monitor(
+ * 'src',
+ * function() {
+ * $this->taskExec('phpunit')->run();
+ * },
+ * \Lurker\Event\FilesystemEvent::ALL
+ * )->monitor(
+ * 'migrations',
+ * function() {
+ * //do something
+ * },
+ * [
+ * \Lurker\Event\FilesystemEvent::CREATE,
+ * \Lurker\Event\FilesystemEvent::DELETE
+ * ]
+ * )->run();
* ?>
* ```
*/
/**
* @param string|string[] $paths
* @param \Closure $callable
+ * @param int|int[] $events
*
* @return $this
*/
- public function monitor($paths, \Closure $callable)
+ public function monitor($paths, \Closure $callable, $events = 2)
{
- if (!is_array($paths)) {
- $paths = [$paths];
- }
- $this->monitor[] = [$paths, $callable];
+ $this->monitor[] = [(array)$paths, $callable, (array)$events];
return $this;
}
$closure = $monitor[1];
$closure->bindTo($this->bindTo);
foreach ($monitor[0] as $i => $dir) {
- $watcher->track("fs.$k.$i", $dir, FilesystemEvent::MODIFY);
+ foreach ($monitor[2] as $j => $event) {
+ $watcher->track("fs.$k.$i.$j", $dir, $event);
+ $watcher->addListener("fs.$k.$i.$j", $closure);
+ }
$this->printTaskInfo('Watching {dir} for changes...', ['dir' => $dir]);
- $watcher->addListener("fs.$k.$i", $closure);
}
}