Skip to content

Basic Usage

Supian M edited this page May 16, 2021 · 5 revisions

Usage

Basic Usage

use Octopy\Inotify\Inotify;
use Octopy\Inotify\Contract\Event;
use Octopy\Inotify\Contract\Watcher;

$inotify = new Inotify('foo.txt', function (Event $event) {

    $event->on(IN_MODIFY, function (Watcher $watcher) {
        // do something
    });
    
    $event->on(IN_DELETE, function (Watcher $watcher) {
        // do something
    });
});

$inotify->watch();

Dependency Injection

Sometimes you want to use dependency injection class instead of the classic way.

use Octopy\Inotify\Inotify;
use Octopy\Inotify\Contract\Event;
use Octopy\Inotify\Contract\Watcher;

class InotifyCommand extends \Illuminate\Console\Command
{
    /**
     * @param Inotify $inotify
     */
    public function __construct(protected Inotify $inotify)
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $this->inotify->source('foobar.txt');
        
        $this->inotify->event(function (Event $event) {
        
            $event->on(IN_MODIFY, function (Watcher $watcher) {
                // do something
            });
            
            $event->on(IN_DELETE, function (Watcher $watcher) {
                // do something
            });
        });

        $this->inotify->watch();
    }
}
Clone this wiki locally