11<?php
2+
23namespace tauthz ;
34
5+ use Casbin \Bridge \Logger \LoggerBridge ;
46use Casbin \Enforcer ;
57use Casbin \Model \Model ;
68use Casbin \Log \Log ;
79use think \Service ;
810use tauthz \command \Publish ;
911
1012/**
11- * Tauthz service
12- *
13+ * Tauthz service.
14+ *
13151416 */
1517class TauthzService extends Service
1618{
1719 /**
18- * Register service
20+ * Register service.
1921 *
2022 * @return void
2123 */
2224 public function register ()
2325 {
2426 // 注册数据迁移服务
2527 $ this ->app ->register (\think \migration \Service::class);
26-
28+
2729 // 绑定 Casbin决策器
2830 $ this ->app ->bind ('enforcer ' , function () {
29- $ default = $ this ->app ->config ->get (" tauthz.default " );
30-
31- $ config = $ this ->app ->config ->get (" tauthz.enforcers. " .$ default );
31+ $ default = $ this ->app ->config ->get (' tauthz.default ' );
32+
33+ $ config = $ this ->app ->config ->get (' tauthz.enforcers. ' .$ default );
3234 $ adapter = $ config ['adapter ' ];
33-
35+
3436 $ configType = $ config ['model ' ]['config_type ' ];
35-
37+
3638 $ model = new Model ();
3739 if ('file ' == $ configType ) {
3840 $ model ->loadModel ($ config ['model ' ]['config_file_path ' ]);
3941 } elseif ('text ' == $ configType ) {
4042 $ model ->loadModel ($ config ['model ' ]['config_text ' ]);
4143 }
42-
43- return new Enforcer ($ model , app ($ adapter ), $ this ->app ->config ->get (" tauthz.log.enabled " , false ));
44+
45+ return new Enforcer ($ model , app ($ adapter ), $ this ->app ->config ->get (' tauthz.log.enabled ' , false ));
4446 });
4547 }
4648
4749 /**
48- * Boot function
50+ * Boot function.
4951 *
5052 * @return void
5153 */
5254 public function boot ()
5355 {
54- $ this ->mergeConfigFrom (__DIR__ . '/../config/tauthz.php ' , 'tauthz ' );
56+ $ this ->mergeConfigFrom (__DIR__ . '/../config/tauthz.php ' , 'tauthz ' );
5557
5658 // 设置 Casbin Logger
57- if ($ logger = $ this ->app ->config ->get ("tauthz.log.logger " )){
58- Log::setLogger (new $ logger ($ this ->app ->log ));
59+ if ($ logger = $ this ->app ->config ->get ('tauthz.log.logger ' )) {
60+ if (is_string ($ logger )) {
61+ $ logger = $ this ->app ->make ($ logger );
62+ }
63+
64+ Log::setLogger (new LoggerBridge ($ logger ));
5965 }
6066
6167 $ this ->commands (['tauthz:publish ' => Publish::class]);
@@ -64,8 +70,9 @@ public function boot()
6470 /**
6571 * Merge the given configuration with the existing configuration.
6672 *
67- * @param string $path
68- * @param string $key
73+ * @param string $path
74+ * @param string $key
75+ *
6976 * @return void
7077 */
7178 protected function mergeConfigFrom (string $ path , string $ key )
@@ -74,4 +81,4 @@ protected function mergeConfigFrom(string $path, string $key)
7481
7582 $ this ->app ->config ->set (array_merge (require $ path , $ config ), $ key );
7683 }
77- }
84+ }
0 commit comments