-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAcl.php
146 lines (128 loc) · 2.91 KB
/
Acl.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Another ACL (Access Control List)
*
* @category A
* @package A\Acl
* @copyright Copyright (c) 2017 Alvan
* @license The MIT License (MIT)
*/
namespace A;
class Acl
{
/**
* @var A\Acl\Comparer
*/
protected $_comparer;
/**
* @var A\Acl\Discover
*/
protected $_discover;
/**
* @var A\Acl\Registry
*/
protected $_registry;
/**
* @var mixed
*/
protected $_instance = Acl\Instance::class;
/**
* @param A\Acl\Comparer|null $comparer
* @return void
*/
public function setComparer(?Acl\Comparer $comparer) : void
{
$this->_comparer = $comparer;
}
/**
* @return A\Acl\Comparer|null
*/
public function getComparer() : ?Acl\Comparer
{
return $this->_comparer;
}
/**
* @param A\Acl\Discover|null $discover
* @return void
*/
public function setDiscover(?Acl\Discover $discover) : void
{
$this->_discover = $discover;
}
/**
* @return A\Acl\Discover|null
*/
public function getDiscover() : ?Acl\Discover
{
return $this->_discover;
}
/**
* @param A\Acl\Registry $registry
* @return void
*/
public function setRegistry(Acl\Registry $registry) : void
{
$this->_registry = $registry;
}
/**
* @return A\Acl\Registry
*/
public function getRegistry() : Acl\Registry
{
return $this->_registry ?? $this->_registry = new Acl\Registry;
}
/**
* @param mixed $instance
* @return mixed
*/
public function mapInstance($instance = null)
{
if (isset($instance))
{
$this->_instance = $instance;
}
return $this->_instance;
}
/**
* @param mixed $accessor
* @param mixed $resource
* @return int|null
*/
public function access($accessor, $resource) : ?int
{
return ($instance = $this->locate($accessor)) ? $instance->access($this, $accessor, $resource) : null;
}
/**
* @param mixed $accessor
* @return A\Acl\Instance
*/
public function create($accessor = null) : Acl\Instance
{
$instance = new $this->_instance;
if (isset($accessor))
{
$this->getRegistry()[(string)$accessor] = $instance;
}
return $instance;
}
/**
* @param mixed $accessor
* @return A\Acl\Instance|null
*/
public function locate($accessor) : ?Acl\Instance
{
$instance = $this->getRegistry()[(string)$accessor] ?? null;
if (!$instance)
{
if ($discover = $this->getDiscover())
{
$instance = $discover->discover($this, $accessor);
if ($instance)
{
$this->getRegistry()[(string)$accessor] = $instance;
}
}
}
return $instance;
}
}