Skip to content

Commit 145f97b

Browse files
authored
BREAKING CHANGE: upgrade to PHP 8.0 and PHP-Casbin 4.0 (#20)
1 parent 2d975d4 commit 145f97b

File tree

5 files changed

+57
-34
lines changed

5 files changed

+57
-34
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
strategy:
2424
fail-fast: true
2525
matrix:
26-
php: [ 7.1, 7.2, 7.3, 7.4 ]
26+
php: [ 8.0, 8.1, 8.2, 8.3 ]
2727
stability: [ prefer-lowest, prefer-stable ]
2828

2929
name: PHP ${{ matrix.php }} - ${{ matrix.stability }}

composer.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
],
2020
"license": "Apache-2.0",
2121
"require": {
22-
"yiisoft/yii2": "~2.0.14",
23-
"casbin/casbin": "~3.1"
22+
"php": ">=8.0",
23+
"yiisoft/yii2": "~2.0.49",
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",
2728
"php-coveralls/php-coveralls": "^2.1",
28-
"yiisoft/yii2-app-basic": "~2.0.14"
29+
"yiisoft/yii2-app-basic": "~2.0.49"
2930
},
3031
"autoload": {
3132
"psr-4": {

config/permission.php

+8
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

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="tests/test.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false">
11-
<testsuites>
12-
<testsuite name="Application Test Suite">
13-
<directory>./tests/</directory>
14-
</testsuite>
15-
</testsuites>
16-
<filter>
17-
<whitelist processUncoveredFilesFromWhitelist="true">
18-
<directory suffix=".php">./src</directory>
19-
</whitelist>
20-
</filter>
21-
<logging>
22-
<log type="coverage-clover" target="build/logs/clover.xml"/>
23-
<log type="coverage-html" target="build/html"/>
24-
</logging>
25-
<php>
26-
<env name="DB_DATABASE" value="casbin"/>
27-
</php>
28-
</phpunit>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="tests/test.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Application Test Suite">
13+
<directory>./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">./src</directory>
19+
</whitelist>
20+
</filter>
21+
<logging>
22+
<log type="coverage-clover" target="build/logs/clover.xml"/>
23+
<log type="coverage-html" target="build/html"/>
24+
</logging>
25+
<php>
26+
<env name="DB_DATABASE" value="casbin"/>
27+
</php>
28+
</phpunit>

src/Permission.php

+15-1
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)