Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/JasonLewis/ResourceWatcher/Watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,33 @@ public function __construct(Tracker $tracker, Filesystem $files)
/**
* Register a resource to be watched.
*
* @param string $resource
* @param string|array $resources
* @return \JasonLewis\ResourceWatcher\Listener
*/
public function watch($resource)
public function watch($resources)
{
if (! $this->files->exists($resource)) {
throw new RuntimeException('Resource must exist before you can watch it.');
} elseif ($this->files->isDirectory($resource)) {
$resource = new DirectoryResource(new SplFileInfo($resource), $this->files);

$resource->setupDirectory();
} else {
$resource = new FileResource(new SplFileInfo($resource), $this->files);
if (!is_array($resources)) {
$resources = array($resources);
}

// The listener gives users the ability to bind listeners on the events
// created when watching a file or directory. We'll give the listener
// to the tracker so the tracker can fire any bound listeners.
$listener = new Listener;

$this->tracker->register($resource, $listener);
foreach ($resources as $resource) {
if (! $this->files->exists($resource)) {
throw new RuntimeException('Resource must exist before you can watch it.');
} elseif ($this->files->isDirectory($resource)) {
$resource = new DirectoryResource(new SplFileInfo($resource), $this->files);

$resource->setupDirectory();
} else {
$resource = new FileResource(new SplFileInfo($resource), $this->files);
}

$this->tracker->register($resource, $listener);
}

return $listener;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/WatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public function testWatchDirectoryResource()
$this->assertInstanceOf('JasonLewis\ResourceWatcher\Resource\DirectoryResource', $resource[0]);
}

public function testWatchMultipleDirectoryResource()
{
$this->files->shouldReceive('exists')->times(16)->andReturn(true);
$this->files->shouldReceive('isDirectory')->times(2)->andReturn(true);
$this->files->shouldReceive('lastModified')->times(14)->andReturn(time());

$this->watcher->watch(array(__DIR__,__DIR__));

$tracked = $this->watcher->getTracker()->getTracked();
$resource = array_pop($tracked);
$this->assertInstanceOf('JasonLewis\ResourceWatcher\Resource\DirectoryResource', $resource[0]);
}

public function testWatchFileResource()
{
Expand Down