A resource watcher allows you to watch a resource for any changes. This means you can watch a directory and then listen for any changes to files within that directory or to the directory itself.
To install Resource Watcher add it to the requires
key of your composer.json
file.
"jasonlewis/resource-watcher": "1.0.*"
The Resource Watcher is best used from a console. An example command can be found in the watcher
file. Once you've customized the command you can call it from your console.
$ php watcher
Any changes you make to the resource will be outputted to the console.
To watch resources you first need an instance of JasonLewis\ResourceWatcher\Watcher
. This class has a few dependencies (JasonLewis\ResourceWatcher\Tracker
and Illuminate\Filesystem\Filesystem
) that must also be instantiated.
$files = new Illuminate\Filesystem\Filesystem;
$tracker = new JasonLewis\ResourceWatcher\Tracker;
$watcher = new JasonLewis\ResourceWatcher\ResourceWatcher($tracker, $files);
$listener = $watcher->watch('path/to/resource');
You can watch as many resources as you like.
When you watch a resource an instance of JasonLewis\ResourceWatcher\Listener
is returned. This allows you to listen for changes to each resource that's being watched.
There are three changes you can listen for: onModify
, onCreate
, and onDelete
.
$listener->onModify(function($resource)
{
echo "{$resource->getPath()} has been modified.".PHP_EOL;
});
Remember that each call to
$watcher->watch()
will return an instance ofJasonLewis\ResourceWatcher\Listener
, so be sure you attach listeners to the right one!
Once you're watching some resources and have any listeners setup you can start the watching process.
$watcher->startWatch();
Resource Watcher includes a service provider for the Laravel 4 framework. This service provider will bind an instance of JasonLewis\ResourceWatcher\Watcher
to the application container under the watcher
key.
$listener = $app['watcher']->watch('path/to/resource');
// Or if you don't have access to an instance of the application container.
$listener = App::make('watcher')->watch('path/to/resource');
Register JasonLewis\ResourceWatcher\Integration\LaravelServiceProvider
in the array of providers in app/config/app.php
.
Resource Watcher is released under the 2-clause BSD license. See the LICENSE
for more details.