13
13
14
14
use Casbin \Enforcer ;
15
15
use Casbin \Exceptions \CasbinException ;
16
+ use Casbin \Log \Logger \DefaultLogger ;
16
17
use Casbin \Model \Model ;
18
+ use Monolog \Handler \StreamHandler ;
19
+ use Monolog \Logger ;
20
+ use Psr \Log \LoggerInterface ;
17
21
use support \Container ;
18
22
use Casbin \WebmanPermission \Watcher \RedisWatcher ;
19
23
@@ -67,10 +71,11 @@ class Permission
67
71
protected static array $ _manager = [];
68
72
69
73
/**
70
- * @param string|null $driver
74
+ * @desc driver
75
+ * @param string|null $driver
71
76
* @return Enforcer
72
77
* @throws CasbinException
73
- * @author Lyt8384
78
+ * @author Tinywan(ShaoBo Wan)
74
79
*/
75
80
public static function driver (?string $ driver = null ): Enforcer
76
81
{
@@ -87,7 +92,15 @@ public static function driver(?string $driver = null): Enforcer
87
92
} elseif ('text ' == $ config ['model ' ]['config_type ' ]) {
88
93
$ model ->loadModel ($ config ['model ' ]['config_text ' ]);
89
94
}
90
- static ::$ _manager [$ driver ] = new Enforcer ($ model , Container::make ($ config ['adapter ' ], [$ driver ]), false );
95
+ $ logConfig = self ::getConfig ('log ' );
96
+ $ logger = null ;
97
+ if (true === $ logConfig ['enabled ' ]) {
98
+ /** @var LoggerInterface $casbinLogger 创建一个 Monolog 日志记录器 */
99
+ $ casbinLogger = new Logger ($ logConfig ['logger ' ]);
100
+ $ casbinLogger ->pushHandler (new StreamHandler ($ logConfig ['path ' ], Logger::DEBUG ));
101
+ $ logger = new DefaultLogger ($ casbinLogger );
102
+ }
103
+ static ::$ _manager [$ driver ] = new Enforcer ($ model , Container::make ($ config ['adapter ' ], [$ driver ]), $ logger , $ logConfig ['enabled ' ]);
91
104
92
105
$ watcher = new RedisWatcher (config ('redis.default ' ), $ driver );
93
106
static ::$ _manager [$ driver ]->setWatcher ($ watcher );
@@ -112,7 +125,7 @@ public static function getAllDriver(): array
112
125
* @return mixed
113
126
* @author Tinywan(ShaoBo Wan)
114
127
*/
115
- public static function getDefaultDriver ()
128
+ public static function getDefaultDriver (): mixed
116
129
{
117
130
return self ::getConfig ('default ' );
118
131
}
@@ -124,7 +137,7 @@ public static function getDefaultDriver()
124
137
* @return mixed
125
138
* @author Tinywan(ShaoBo Wan)
126
139
*/
127
- public static function getConfig (string $ name = null , $ default = null )
140
+ public static function getConfig (string $ name = null , $ default = null ): mixed
128
141
{
129
142
if (!is_null ($ name )) {
130
143
return config ('plugin.casbin.webman-permission.permission. ' . $ name , $ default );
0 commit comments