1
1
<?php
2
+
2
3
namespace tauthz ;
3
4
5
+ use Casbin \Bridge \Logger \LoggerBridge ;
4
6
use Casbin \Enforcer ;
5
7
use Casbin \Model \Model ;
6
8
use Casbin \Log \Log ;
7
9
use think \Service ;
8
10
use tauthz \command \Publish ;
9
11
10
12
/**
11
- * Tauthz service
12
- *
13
+ * Tauthz service.
14
+ *
13
15
14
16
*/
15
17
class TauthzService extends Service
16
18
{
17
19
/**
18
- * Register service
20
+ * Register service.
19
21
*
20
22
* @return void
21
23
*/
22
24
public function register ()
23
25
{
24
26
// 注册数据迁移服务
25
27
$ this ->app ->register (\think \migration \Service::class);
26
-
28
+
27
29
// 绑定 Casbin决策器
28
30
$ 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 );
32
34
$ adapter = $ config ['adapter ' ];
33
-
35
+
34
36
$ configType = $ config ['model ' ]['config_type ' ];
35
-
37
+
36
38
$ model = new Model ();
37
39
if ('file ' == $ configType ) {
38
40
$ model ->loadModel ($ config ['model ' ]['config_file_path ' ]);
39
41
} elseif ('text ' == $ configType ) {
40
42
$ model ->loadModel ($ config ['model ' ]['config_text ' ]);
41
43
}
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 ));
44
46
});
45
47
}
46
48
47
49
/**
48
- * Boot function
50
+ * Boot function.
49
51
*
50
52
* @return void
51
53
*/
52
54
public function boot ()
53
55
{
54
- $ this ->mergeConfigFrom (__DIR__ . '/../config/tauthz.php ' , 'tauthz ' );
56
+ $ this ->mergeConfigFrom (__DIR__ . '/../config/tauthz.php ' , 'tauthz ' );
55
57
56
58
// 设置 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 ));
59
65
}
60
66
61
67
$ this ->commands (['tauthz:publish ' => Publish::class]);
@@ -64,8 +70,9 @@ public function boot()
64
70
/**
65
71
* Merge the given configuration with the existing configuration.
66
72
*
67
- * @param string $path
68
- * @param string $key
73
+ * @param string $path
74
+ * @param string $key
75
+ *
69
76
* @return void
70
77
*/
71
78
protected function mergeConfigFrom (string $ path , string $ key )
@@ -74,4 +81,4 @@ protected function mergeConfigFrom(string $path, string $key)
74
81
75
82
$ this ->app ->config ->set (array_merge (require $ path , $ config ), $ key );
76
83
}
77
- }
84
+ }
0 commit comments