Skip to content

Commit 7538450

Browse files
committed
Use psr3-bridge.
1 parent a4c291e commit 7538450

File tree

3 files changed

+24
-88
lines changed

3 files changed

+24
-88
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
],
2121
"require": {
2222
"codeigniter4/framework": "^4@rc",
23-
"casbin/casbin": "^1.0"
23+
"casbin/casbin": "^2.0",
24+
"casbin/psr3-bridge": "^1.1"
2425
},
2526
"autoload": {
2627
"psr-4": {
@@ -36,4 +37,4 @@
3637
"Casbin\\CodeIgniter\\Tests\\": "tests/"
3738
}
3839
}
39-
}
40+
}

src/Adapters/DatabaseAdapter.php

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Casbin\CodeIgniter\Adapters;
46

7+
use Casbin\Model\Model;
58
use Casbin\Persist\Adapter;
69
use Casbin\Persist\AdapterHelper;
710
use Casbin\CodeIgniter\Models\RuleModel;
@@ -46,7 +49,7 @@ public function __construct(array $config)
4649
* @param string $ptype
4750
* @param array $rule
4851
*/
49-
public function savePolicyLine($ptype, array $rule)
52+
public function savePolicyLine(string $ptype, array $rule): void
5053
{
5154
$col['ptype'] = $ptype;
5255
foreach ($rule as $key => $value) {
@@ -60,12 +63,10 @@ public function savePolicyLine($ptype, array $rule)
6063
* loads all policy rules from the storage.
6164
*
6265
* @param Model $model
63-
*
64-
* @return mixed
6566
*/
66-
public function loadPolicy($model)
67+
public function loadPolicy(Model $model): void
6768
{
68-
$rows = $this->model->getAllFromCache();
69+
$rows = $this->model->getAllFromCache();
6970

7071
foreach ($rows as $row) {
7172
$line = implode(', ', array_filter($row, function ($val) {
@@ -79,39 +80,33 @@ public function loadPolicy($model)
7980
* saves all policy rules to the storage.
8081
*
8182
* @param Model $model
82-
*
83-
* @return bool
8483
*/
85-
public function savePolicy($model)
84+
public function savePolicy(Model $model): void
8685
{
87-
foreach ($model->model['p'] as $ptype => $ast) {
86+
foreach ($model['p'] as $ptype => $ast) {
8887
foreach ($ast->policy as $rule) {
8988
$this->savePolicyLine($ptype, $rule);
9089
}
9190
}
9291

93-
foreach ($model->model['g'] as $ptype => $ast) {
92+
foreach ($model['g'] as $ptype => $ast) {
9493
foreach ($ast->policy as $rule) {
9594
$this->savePolicyLine($ptype, $rule);
9695
}
9796
}
98-
99-
return true;
10097
}
10198

10299
/**
103-
* Adds a policy rule to the storage.
100+
* adds a policy rule to the storage.
104101
* This is part of the Auto-Save feature.
105102
*
106103
* @param string $sec
107104
* @param string $ptype
108105
* @param array $rule
109-
*
110-
* @return mixed
111106
*/
112-
public function addPolicy($sec, $ptype, $rule)
107+
public function addPolicy(string $sec, string $ptype, array $rule): void
113108
{
114-
return $this->savePolicyLine($ptype, $rule);
109+
$this->savePolicyLine($ptype, $rule);
115110
}
116111

117112
/**
@@ -120,10 +115,8 @@ public function addPolicy($sec, $ptype, $rule)
120115
* @param string $sec
121116
* @param string $ptype
122117
* @param array $rule
123-
*
124-
* @return mixed
125118
*/
126-
public function removePolicy($sec, $ptype, $rule)
119+
public function removePolicy(string $sec, string $ptype, array $rule): void
127120
{
128121
$count = 0;
129122

@@ -138,8 +131,6 @@ public function removePolicy($sec, $ptype, $rule)
138131
++$count;
139132
}
140133
}
141-
142-
return $count;
143134
}
144135

145136
/**
@@ -149,11 +140,9 @@ public function removePolicy($sec, $ptype, $rule)
149140
* @param string $sec
150141
* @param string $ptype
151142
* @param int $fieldIndex
152-
* @param mixed ...$fieldValues
153-
*
154-
* @return mixed
143+
* @param string ...$fieldValues
155144
*/
156-
public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
145+
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
157146
{
158147
$count = 0;
159148

@@ -171,7 +160,5 @@ public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
171160
++$count;
172161
}
173162
}
174-
175-
return $count;
176163
}
177164
}

src/Logger.php

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,16 @@
33
namespace Casbin\CodeIgniter;
44

55
use Casbin\Log\Logger as LoggerContract;
6+
use Casbin\Bridge\Logger\LoggerBridge;
7+
use Config\Services;
68

7-
class Logger implements LoggerContract
9+
class Logger extends LoggerBridge implements LoggerContract
810
{
9-
public $enable = false;
10-
11-
/**
12-
* controls whether print the message.
13-
*
14-
* @param bool $enable
15-
*/
16-
public function enableLog($enable)
17-
{
18-
$this->enable = $enable;
19-
}
20-
21-
/**
22-
* returns if logger is enabled.
23-
*
24-
* @return bool
25-
*/
26-
public function isEnabled()
27-
{
28-
return $this->enable;
29-
}
30-
3111
/**
32-
* formats using the default formats for its operands and logs the message.
33-
*
34-
* @param mixed ...$v
35-
*
36-
* @return mixed
12+
* LoggerBridge constructor.
3713
*/
38-
public function write(...$v)
14+
public function __construct()
3915
{
40-
if (!$this->enable) {
41-
return;
42-
}
43-
$content = '';
44-
foreach ($v as $value) {
45-
if (\is_array($value) || \is_object($value)) {
46-
$value = json_encode($value);
47-
}
48-
$content .= $value;
49-
}
50-
51-
log_message('info', $content);
52-
}
53-
54-
/**
55-
* formats according to a format specifier and logs the message.
56-
*
57-
* @param $format
58-
* @param mixed ...$v
59-
*
60-
* @return mixed
61-
*/
62-
public function writef($format, ...$v)
63-
{
64-
if (!$this->enable) {
65-
return;
66-
}
67-
68-
log_message('info', sprintf($format, ...$v));
16+
parent::__construct(Services::logger(true));
6917
}
7018
}

0 commit comments

Comments
 (0)