casbin/workerman-redis-watcher

Workerman Redis watcher for PHP-Casbin.

v1.0.0 2021-11-09 10:30 UTC

This package is auto-updated.

Last update: 2024-10-29 16:15:40 UTC


README

Latest Stable Version Total Downloads License

Workerman Redis watcher for PHP-Casbin, Casbin is a powerful and efficient open-source access control library.

Installation

Via Composer.

composer require casbin/workerman-redis-watcher

Usage

require dirname(__FILE__) . '/../vendor/autoload.php';

use Casbin\Enforcer;
use CasbinWatcher\WorkermanRedis\Watcher;
use Workerman\Worker;


$worker = new Worker();
$worker->count = 2;
$worker->onWorkerStart = function () {

    // Initialize the Watcher.
    $watcher = new Watcher([
        'host' => '127.0.0.1',
        'password' => '',
        'port' => 6379,
        'database' => 0,
    ]);

    // Initialize the Enforcer.
    $enforcer = new Enforcer("path/to/model.conf", "path/to/policy.csv");

    $enforcer->setWatcher($watcher);
    
    // Set callback, sets the callback function that the watcher will call,
    // When the policy in DB has been changed by other instances.
    // A classic callback is $enforcer->loadPolicy().
    $watcher->setUpdateCallback(function () use ($enforcer) {
        echo "Now should reload all policies." . PHP_EOL;
        $enforcer->loadPolicy();
    });
};

Worker::runAll();

Getting Help

License

This project is licensed under the Apache 2.0 license.