|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CasbinWatcher\SwooleRedis; |
| 4 | + |
| 5 | +use Casbin\Persist\Watcher as WatcherContract; |
| 6 | +use Redis; |
| 7 | +use Closure; |
| 8 | + |
| 9 | +class Watcher implements WatcherContract |
| 10 | +{ |
| 11 | + public ?Closure $callback = null; |
| 12 | + |
| 13 | + private $pubClient; |
| 14 | + |
| 15 | + private $subClient; |
| 16 | + |
| 17 | + private $channel; |
| 18 | + |
| 19 | + /** |
| 20 | + * The config of Watcher. |
| 21 | + * |
| 22 | + * @param array $config |
| 23 | + * [ |
| 24 | + * 'host' => '127.0.0.1', |
| 25 | + * 'password' => '', |
| 26 | + * 'port' => 6379, |
| 27 | + * 'database' => 0, |
| 28 | + * 'channel' => '/casbin', |
| 29 | + * ] |
| 30 | + */ |
| 31 | + public function __construct(array $config) |
| 32 | + { |
| 33 | + $this->pubClient = $this->createRedisClient($config); |
| 34 | + $this->subClient = $this->createRedisClient($config); |
| 35 | + $this->channel = $config['channel'] ?? '/casbin'; |
| 36 | + |
| 37 | + go(function () { |
| 38 | + $this->subClient->subscribe([$this->channel], function ($redis, $channel, $message) { |
| 39 | + if ($this->callback) { |
| 40 | + go($this->callback); |
| 41 | + } |
| 42 | + }); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Sets the callback function that the watcher will call when the policy in DB has been changed by other instances. |
| 48 | + * A classic callback is loadPolicy() method of Enforcer class. |
| 49 | + * |
| 50 | + * @param Closure $func |
| 51 | + */ |
| 52 | + public function setUpdateCallback(Closure $func): void |
| 53 | + { |
| 54 | + $this->callback = $func; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Update calls the update callback of other instances to synchronize their policy. |
| 59 | + * It is usually called after changing the policy in DB, like savePolicy() method of Enforcer class, |
| 60 | + * addPolicy(), removePolicy(), etc. |
| 61 | + */ |
| 62 | + public function update(): void |
| 63 | + { |
| 64 | + $this->pubClient->publish($this->channel, 'casbin rules updated'); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Close stops and releases the watcher, the callback function will not be called any more. |
| 69 | + */ |
| 70 | + public function close(): void |
| 71 | + { |
| 72 | + $this->pubClient->close(); |
| 73 | + $this->subClient->close(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Create redis client |
| 78 | + * |
| 79 | + * @param array $config |
| 80 | + * @return Redis |
| 81 | + */ |
| 82 | + private function createRedisClient(array $config): Redis |
| 83 | + { |
| 84 | + $config['host'] = $config['host'] ?? '127.0.0.1'; |
| 85 | + $config['port'] = $config['port'] ?? 6379; |
| 86 | + $config['password'] = $config['password'] ?? ''; |
| 87 | + $config['database'] = $config['database'] ?? 0; |
| 88 | + |
| 89 | + $client = new Redis(); |
| 90 | + $client->pconnect($config['host'], $config['port']); |
| 91 | + if (!empty($config['password'])) { |
| 92 | + $client->auth($config['password']); |
| 93 | + } |
| 94 | + |
| 95 | + if (isset($config['database'])) { |
| 96 | + $client->select((int) $config['database']); |
| 97 | + } |
| 98 | + |
| 99 | + $client->setOption(Redis::OPT_READ_TIMEOUT, -1); |
| 100 | + |
| 101 | + return $client; |
| 102 | + } |
| 103 | +} |
0 commit comments