2017 © Pedro Peláez
 

library resource-watcher

Simple PHP resource watcher library - no Laravel dependencies

image

allejo/resource-watcher

Simple PHP resource watcher library - no Laravel dependencies

  • Sunday, February 19, 2017
  • by allejo
  • Repository
  • 1 Watchers
  • 3 Stars
  • 186 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 33 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Resource Watcher

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., (*1)

This is a fork of Jason Lewis's project without Laravel dependencies and uses the Symfony Filesystem instead. This fork is intended to be used for those who do not want to add yet another Filesystem object from another project., (*2)

Installation

Use composer to add this project as a dependency., (*3)

composer require allejo/resource-watcher

Usage

The Resource Watcher is best used from a console. An example of a console command can be found in the watcher file. This file is commented to give you an idea of how to configure and use a resource watcher. Once you've customized the command to your liking you can run it from your console., (*4)

$ php watcher

Any changes you make to the resource will be outputted to the console., (*5)

Quick Overview

To watch resources you first need an instance of JasonLewis\ResourceWatcher\Watcher. This class has a few dependencies (JasonLewis\ResourceWatcher\Tracker and Symfony\Component\Filesystem\Filesystem) that must also be instantiated., (*6)

$files = new Symfony\Component\Filesystem\Filesystem;
$tracker = new JasonLewis\ResourceWatcher\Tracker;

$watcher = new JasonLewis\ResourceWatcher\Watcher($tracker, $files);

Now that we have our watcher we can create a listener for a given resource., (*7)

$listener = $watcher->watch('path/to/resource');

When you watch a resource an instance of JasonLewis\ResourceWatcher\Listener is returned. With this we can now listen for certain events on a resource., (*8)

There are three events we can listen for: modify, create, and delete. The callback you give to the listener receives two parameters, the first being an implementation of JasonLewis\ResourceWatcher\Resource\ResourceInterface and the second being the absolute path to the resource., (*9)

$listener->modify(function($resource, $path) {
    echo "{$path} has been modified.".PHP_EOL;
});

You can use the alias methods as well., (*10)

$listener->onModify(function($resource, $path) {
    echo "{$path} has been modified.".PHP_EOL;
});

You can also listen for any of these events. This time the callback receives a different set of parameters, the first being an instance of JasonLewis\ResourceWatcher\Event and the remaining two being the same as before., (*11)

$listener->anything(function($event, $resource, $path) {

});

Remember that each call to $watcher->watch() will return an instance of JasonLewis\ResourceWatcher\Listener, so be sure you attach listeners to the right one!, (*12)

Once you're watching some resources and have your listeners set up you can start the watching process., (*13)

$watcher->start();

By default the watcher will poll for changes every second. You can adjust this by passing in an optional first parameter to the start method. The polling interval is given in microseconds, so 1,000,000 microseconds is 1 second. The watch will continue until such time that it's aborted from the console. To set a timeout pass in the number of microseconds before the watch will abort as the second parameter., (*14)

The start method can also be given a callback as an optional third parameter. This callback will be fired before checking for any changes to resources., (*15)

$watcher->start(1000000, null, function($watcher) {
    // Perhaps perform some other check and then stop the watch.
    $watcher->stop();
});

License

Resource Watcher is released under the 2-clause BSD license. See the LICENSE for more details., (*16)

The Versions

19/02 2017

dev-master

9999999-dev

Simple PHP resource watcher library - no Laravel dependencies

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

symfony resource assets

19/02 2017

v1.0.1

1.0.1.0

Simple PHP resource watcher library - no Laravel dependencies

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

symfony resource assets

13/02 2017

v1.0.0

1.0.0.0

Simple PHP resource watcher library - no Laravel dependencies

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

symfony resource assets