-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factory.php
59 lines (50 loc) · 1.42 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact [email protected]
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
namespace Mine\Casbin;
use Casbin\Enforcer;
use Casbin\Model\Model;
use Casbin\Persist\Adapter;
use Hyperf\Contract\ConfigInterface;
use Psr\Container\ContainerInterface;
final class Factory
{
public function __construct(
private readonly ConfigInterface $config,
private readonly ContainerInterface $container
) {}
public function __invoke()
{
return $this->enforcer();
}
public function enforcer()
{
return new Enforcer(
$this->getModel(),
$this->getAdapter(),
(bool) $this->config->get('permission.log.enabled', false)
);
}
private function getModel(): Model
{
$model = new Model();
if ($this->config->get('permission.model.type') === 'file') {
$model->loadModel($this->config->get('permission.model.path'));
}
if ($this->config->get('permission.model.type') === 'test') {
$model->loadModelFromText($this->config->get('permission.model.text'));
}
return $model;
}
private function getAdapter(): Adapter
{
return $this->container->get($this->config->get('permission.adapter'));
}
}