1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Rbac\Rules\Container; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Yiisoft\Definitions\Exception\CircularReferenceException; |
9
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
10
|
|
|
use Yiisoft\Definitions\Exception\NotInstantiableException; |
11
|
|
|
use Yiisoft\Factory\Factory; |
12
|
|
|
use Yiisoft\Factory\NotFoundException; |
13
|
|
|
use Yiisoft\Rbac\Exception\RuleInterfaceNotImplementedException; |
14
|
|
|
use Yiisoft\Rbac\Exception\RuleNotFoundException; |
15
|
|
|
use Yiisoft\Rbac\RuleInterface; |
16
|
|
|
use Yiisoft\Rbac\RuleFactoryInterface; |
17
|
|
|
|
18
|
|
|
use function array_key_exists; |
19
|
|
|
|
20
|
|
|
final class RulesContainer implements RuleFactoryInterface |
21
|
|
|
{ |
22
|
|
|
private Factory $factory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var RuleInterface[] |
26
|
|
|
* @psalm-var array<string,RuleInterface> |
27
|
|
|
*/ |
28
|
|
|
private array $instances = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @psalm-param array<string, mixed> $definitions |
32
|
|
|
* |
33
|
|
|
* @throws InvalidConfigException |
34
|
|
|
*/ |
35
|
8 |
|
public function __construct(ContainerInterface $container, array $definitions = [], bool $validate = true) |
36
|
|
|
{ |
37
|
8 |
|
$this->factory = new Factory($container, $definitions, $validate); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws RuleInterfaceNotImplementedException |
42
|
|
|
* @throws RuleNotFoundException |
43
|
|
|
* @throws CircularReferenceException |
44
|
|
|
* @throws InvalidConfigException |
45
|
|
|
* @throws NotInstantiableException |
46
|
|
|
*/ |
47
|
6 |
|
public function create(string $name): RuleInterface |
48
|
|
|
{ |
49
|
6 |
|
if (!array_key_exists($name, $this->instances)) { |
50
|
|
|
try { |
51
|
6 |
|
$rule = $this->factory->create($name); |
52
|
3 |
|
} catch (NotFoundException $e) { |
53
|
1 |
|
throw new RuleNotFoundException($name, 0, $e); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
if (!$rule instanceof RuleInterface) { |
57
|
1 |
|
throw new RuleInterfaceNotImplementedException($name); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
$this->instances[$name] = $rule; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
return $this->instances[$name]; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|