Skip to content

Commit

Permalink
BREAKING CHANGE: upgrade to PHP 8.0 and PHP-Casbin 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobmod committed Oct 26, 2024
1 parent 2d975d4 commit b120bc5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [ 7.1, 7.2, 7.3, 7.4 ]
php: [ 8.0, 8.1, 8.2, 8.3 ]
stability: [ prefer-lowest, prefer-stable ]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
],
"license": "Apache-2.0",
"require": {
"yiisoft/yii2": "~2.0.14",
"casbin/casbin": "~3.1"
"php": ">=8.0",
"yiisoft/yii2": "~2.0.49",
"casbin/casbin": "~4.0"
},
"require-dev": {
"phpunit/phpunit": "~7.0|~8.0|~9.0|~10.5",
"phpunit/phpunit": "~9.0",
"php-coveralls/php-coveralls": "^2.1",
"yiisoft/yii2-app-basic": "~2.0.14"
"yiisoft/yii2-app-basic": "~2.0.49"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 8 additions & 0 deletions config/permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
'config_text' => '',
],

// Yii-casbin logger.
'log' => [
// changes whether YiiPermission will log messages to the Logger.
'enabled' => false,
// Casbin Logger, Supported: \Psr\Log\LoggerInterface|string
'logger' => 'log',
],

// Yii-casbin adapter .
'adapter' => \yii\permission\Adapter::class,

Expand Down
52 changes: 24 additions & 28 deletions phpunit.xml.dist → phpunit.xml
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/test.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-html" target="build/html"/>
</logging>
<php>
<env name="DB_DATABASE" value="casbin"/>
</php>
</phpunit>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/test.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<php>
<env name="DB_DATABASE" value="casbin"/>
</php>
</phpunit>
16 changes: 15 additions & 1 deletion src/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use yii\base\Component;
use Casbin\Model\Model;
use Casbin\Enforcer;
use Casbin\Log\Log;
use Casbin\Log\Logger\DefaultLogger;
use yii\permission\models\CasbinRule;
use Yii;

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

public $config = [];

public $logger = null;

public function __construct($config = [])
{
$this->config = $this->mergeConfig(
Expand All @@ -38,6 +42,16 @@ public function __construct($config = [])
} elseif ('text' == $this->config['model']['config_type']) {
$this->model->loadModelFromText($this->config['model']['config_text']);
}

if ($logger = $this->config['log']['logger']) {
if ($logger === 'log') {
$this->logger = new DefaultLogger();
} else {
$this->logger = new DefaultLogger(Yii::$container->get($logger));
}

Log::setLogger($this->logger);
}
}

/**
Expand Down Expand Up @@ -68,7 +82,7 @@ public function enforcer($newInstance = false)
{
if ($newInstance || is_null($this->enforcer)) {
$this->init();
$this->enforcer = new Enforcer($this->model, $this->adapter);
$this->enforcer = new Enforcer($this->model, $this->adapter, $this->logger, !is_null($this->logger));
}

return $this->enforcer;
Expand Down

0 comments on commit b120bc5

Please sign in to comment.