Skip to content

Commit 73fd86a

Browse files
committed
BREAKING CHANGE: upgrade to PHP 8.0 and PHP-Casbin 4.0
1 parent 2d975d4 commit 73fd86a

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
],
2020
"license": "Apache-2.0",
2121
"require": {
22+
"php": ">=8.0",
2223
"yiisoft/yii2": "~2.0.14",
23-
"casbin/casbin": "~3.1"
24+
"casbin/casbin": "~4.0"
2425
},
2526
"require-dev": {
26-
"phpunit/phpunit": "~7.0|~8.0|~9.0|~10.5",
27+
"phpunit/phpunit": "~9.0|~10.5",
2728
"php-coveralls/php-coveralls": "^2.1",
2829
"yiisoft/yii2-app-basic": "~2.0.14"
2930
},

config/permission.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
'config_text' => '',
1515
],
1616

17+
// Yii-casbin logger.
18+
'log' => [
19+
// changes whether YiiPermission will log messages to the Logger.
20+
'enabled' => false,
21+
// Casbin Logger, Supported: \Psr\Log\LoggerInterface|string
22+
'logger' => 'log',
23+
],
24+
1725
// Yii-casbin adapter .
1826
'adapter' => \yii\permission\Adapter::class,
1927

src/Permission.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use yii\base\Component;
66
use Casbin\Model\Model;
77
use Casbin\Enforcer;
8+
use Casbin\Log\Log;
9+
use Casbin\Log\Logger\DefaultLogger;
810
use yii\permission\models\CasbinRule;
911
use Yii;
1012

@@ -23,6 +25,8 @@ class Permission extends Component
2325

2426
public $config = [];
2527

28+
public $logger = null;
29+
2630
public function __construct($config = [])
2731
{
2832
$this->config = $this->mergeConfig(
@@ -38,6 +42,16 @@ public function __construct($config = [])
3842
} elseif ('text' == $this->config['model']['config_type']) {
3943
$this->model->loadModelFromText($this->config['model']['config_text']);
4044
}
45+
46+
if ($logger = $this->config['log']['logger']) {
47+
if ($logger === 'log') {
48+
$this->logger = new DefaultLogger();
49+
} else {
50+
$this->logger = new DefaultLogger(Yii::$container->get($logger));
51+
}
52+
53+
Log::setLogger($this->logger);
54+
}
4155
}
4256

4357
/**
@@ -68,7 +82,7 @@ public function enforcer($newInstance = false)
6882
{
6983
if ($newInstance || is_null($this->enforcer)) {
7084
$this->init();
71-
$this->enforcer = new Enforcer($this->model, $this->adapter);
85+
$this->enforcer = new Enforcer($this->model, $this->adapter, $this->logger, !is_null($this->logger));
7286
}
7387

7488
return $this->enforcer;

0 commit comments

Comments
 (0)