Skip to content

Commit

Permalink
fix: Improve some coding
Browse files Browse the repository at this point in the history
  • Loading branch information
leeqvip committed Dec 23, 2024
1 parent 3b34d67 commit b068fb4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
37 changes: 17 additions & 20 deletions src/adapter/DatabaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
use tauthz\model\Rule;
use tauthz\cache\CacheHandlerContract;
use Casbin\Model\Model;
use Casbin\Persist\Adapter;
use Casbin\Persist\AdapterHelper;
use Casbin\Persist\UpdatableAdapter;
use Casbin\Persist\BatchAdapter;
use Casbin\Persist\FilteredAdapter;
use Casbin\Persist\{Adapter, AdapterHelper, UpdatableAdapter, BatchAdapter, FilteredAdapter};
use Casbin\Persist\Adapters\Filter;
use Casbin\Exceptions\InvalidFilterTypeException;
use tauthz\traits\Configurable;
Expand All @@ -27,21 +23,21 @@ class DatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter, Filter
/**
* @var bool
*/
private $filtered = false;
private bool $filtered = false;

/**
* Rules model.
*
* @var Rule
*/
protected $model;
protected Rule $model;

/**
* Cache Handler.
*
* @var CacheHandlerContract
*/
protected $cacheHandler;
protected CacheHandlerContract $cacheHandler;

/**
* the DatabaseAdapter constructor.
Expand Down Expand Up @@ -84,12 +80,13 @@ public function filterRule(array $rule): array
*
* @return void
*/
public function savePolicyLine($ptype, array $rule)
public function savePolicyLine(string $ptype, array $rule): void
{
$col['ptype'] = $ptype;
foreach ($rule as $key => $value) {
$col['v'.strval($key).''] = $value;
$col['v' . strval($key) . ''] = $value;
}

$this->cacheHandler->cachePolicies($this->model)->insert($col);
}

Expand All @@ -100,7 +97,8 @@ public function savePolicyLine($ptype, array $rule)
*/
public function loadPolicy(Model $model): void
{
$rows = $this->cacheHandler->cachePolicies($this->model)->field(['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'])->select()->toArray();
$rows = $this->cacheHandler->cachePolicies($this->model)->field(['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'])
->select()->toArray();
foreach ($rows as $row) {
$this->loadPolicyArray($this->filterRule($row), $model);
}
Expand Down Expand Up @@ -160,6 +158,7 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
$cols[$i++] = $temp;
$temp = [];
}

$this->cacheHandler->cachePolicies($this->model)->insertAll($cols);
}

Expand All @@ -177,7 +176,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
$instance = $this->model->where('ptype', $ptype);

foreach ($rule as $key => $value) {
$instance->where('v'.strval($key), $value);
$instance->where('v' . strval($key), $value);
}

foreach ($instance->select() as $model) {
Expand Down Expand Up @@ -234,7 +233,7 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
++$count;
}
}

return $removedRules;
}

Expand Down Expand Up @@ -271,7 +270,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $

foreach ($newPolicy as $key => $value) {
$column = 'v' . strval($key);
$instance->$column = $value;
$instance->{$column} = $value;
}

$instance->save();
Expand Down Expand Up @@ -360,14 +359,12 @@ public function loadFilteredPolicy(Model $model, $filter): void
}
$rows = $instance->select()->hidden(['id'])->toArray();
foreach ($rows as $row) {
$row = array_filter($row, function ($value) {
return !is_null($value) && $value !== '';
});
$line = implode(', ', array_filter($row, function ($val) {
return '' != $val && !is_null($val);
}));
$row = array_filter($row, fn ($value) => !is_null($value) && $value !== '');
$line = implode(', ', array_filter($row, fn ($val) => '' != $val && !is_null($val)));

$this->loadPolicyLine(trim($line), $model);
}

$this->setFiltered(true);
}
}
4 changes: 1 addition & 3 deletions src/command/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace tauthz\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\{Command, Input, Output};

/**
* 发布配置文件、迁移文件指令
Expand Down
3 changes: 2 additions & 1 deletion src/facade/Enforcer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
class Enforcer extends Facade
{
/**
* 获取当前Facade对应类名(或者已经绑定的容器对象标识)
* 获取当前 Facade 对应类名(或者已经绑定的容器对象标识)
*
* @access protected
* @return string
*/
Expand Down
6 changes: 3 additions & 3 deletions src/model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class Rule extends Model implements Arrayable
];
/**
* 架构函数
* @access public
* @param array $data 数据
*
* @param array|object $data 数据
*/
public function __construct($data = [])
public function __construct(array|object $data = [])
{
$this->connection = $this->config('database.connection') ?: '';
$this->table = $this->config('database.rules_table');
Expand Down

0 comments on commit b068fb4

Please sign in to comment.