Skip to content

Commit 13ec82a

Browse files
committed
use psr3-bridge
1 parent e83e5b6 commit 13ec82a

File tree

4 files changed

+32
-58
lines changed

4 files changed

+32
-58
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
"casbin/casbin": "~1.0",
2424
"topthink/framework": "6.0.*-dev",
2525
"topthink/think-orm": "2.0.*-dev",
26-
"topthink/think-migration": "^3.0"
26+
"topthink/think-migration": "^3.0",
27+
"casbin/psr3-bridge": "^1.0"
2728
},
2829
"require-dev": {
2930
"phpunit/phpunit": "~7.0",
3031
"php-coveralls/php-coveralls": "^2.1",
31-
"mockery/mockery": "^1.0",
3232
"topthink/think": "6.0.*-dev"
3333
},
3434
"autoload": {
@@ -51,4 +51,4 @@
5151
]
5252
}
5353
}
54-
}
54+
}

config/tauthz.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
return [
34
/*
45
*Default Tauthz enforcer
@@ -8,8 +9,8 @@
89
'log' => [
910
// changes whether Lauthz will log messages to the Logger.
1011
'enabled' => false,
11-
// Casbin Logger
12-
'logger' => tauthz\Logger::class,
12+
// Casbin Logger, Supported: \Psr\Log\LoggerInterface|string
13+
'logger' => 'log',
1314
],
1415

1516
'enforcers' => [
@@ -40,4 +41,4 @@
4041
],
4142
],
4243
],
43-
];
44+
];

src/TauthzService.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,67 @@
11
<?php
2+
23
namespace tauthz;
34

5+
use Casbin\Bridge\Logger\LoggerBridge;
46
use Casbin\Enforcer;
57
use Casbin\Model\Model;
68
use Casbin\Log\Log;
79
use think\Service;
810
use tauthz\command\Publish;
911

1012
/**
11-
* Tauthz service
12-
*
13+
* Tauthz service.
14+
*
1315
1416
*/
1517
class 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+
}

tests/LoggerTest.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)