1313
1414use Casbin \Enforcer ;
1515use Casbin \Exceptions \CasbinException ;
16+ use Casbin \Log \Logger \DefaultLogger ;
1617use Casbin \Model \Model ;
18+ use Monolog \Handler \StreamHandler ;
19+ use Monolog \Logger ;
20+ use Psr \Log \LoggerInterface ;
1721use support \Container ;
1822use Casbin \WebmanPermission \Watcher \RedisWatcher ;
1923
@@ -67,10 +71,11 @@ class Permission
6771 protected static array $ _manager = [];
6872
6973 /**
70- * @param string|null $driver
74+ * @desc driver
75+ * @param string|null $driver
7176 * @return Enforcer
7277 * @throws CasbinException
73- * @author Lyt8384
78+ * @author Tinywan(ShaoBo Wan)
7479 */
7580 public static function driver (?string $ driver = null ): Enforcer
7681 {
@@ -87,7 +92,15 @@ public static function driver(?string $driver = null): Enforcer
8792 } elseif ('text ' == $ config ['model ' ]['config_type ' ]) {
8893 $ model ->loadModel ($ config ['model ' ]['config_text ' ]);
8994 }
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 ' ]);
91104
92105 $ watcher = new RedisWatcher (config ('redis.default ' ), $ driver );
93106 static ::$ _manager [$ driver ]->setWatcher ($ watcher );
@@ -112,7 +125,7 @@ public static function getAllDriver(): array
112125 * @return mixed
113126 * @author Tinywan(ShaoBo Wan)
114127 */
115- public static function getDefaultDriver ()
128+ public static function getDefaultDriver (): mixed
116129 {
117130 return self ::getConfig ('default ' );
118131 }
@@ -124,7 +137,7 @@ public static function getDefaultDriver()
124137 * @return mixed
125138 * @author Tinywan(ShaoBo Wan)
126139 */
127- public static function getConfig (string $ name = null , $ default = null )
140+ public static function getConfig (string $ name = null , $ default = null ): mixed
128141 {
129142 if (!is_null ($ name )) {
130143 return config ('plugin.casbin.webman-permission.permission. ' . $ name , $ default );
0 commit comments