/
Factory.php
115 lines (95 loc) · 2.37 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
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
<?php
namespace Orchestra\Authorization;
use Illuminate\Contracts\Auth\Guard;
use Orchestra\Contracts\Authorization\Authorization as AuthorizationContract;
use Orchestra\Contracts\Authorization\Factory as FactoryContract;
use Orchestra\Contracts\Memory\Provider;
class Factory implements FactoryContract
{
/**
* Auth instance.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $auth;
/**
* Cache ACL instance so we can reuse it on multiple request.
*
* @var array
*/
protected $drivers = [];
/**
* Construct a new Environment.
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Initiate a new ACL Container instance.
*/
public function make(string $name = null, ?Provider $memory = null): AuthorizationContract
{
$name = $name ?? 'default';
if (! isset($this->drivers[$name])) {
$this->drivers[$name] = (new Authorization($name, $memory))->setAuthenticator($this->auth);
}
return $this->drivers[$name];
}
/**
* Register an ACL Container instance with Closure.
*
* @param string|callable $name
*/
public function register($name, ?callable $callback = null): AuthorizationContract
{
if (\is_callable($name)) {
$callback = $name;
$name = null;
}
$instance = $this->make($name);
$callback($instance);
return $instance;
}
/**
* Manipulate and synchronize roles.
*
* @return mixed
*/
public function __call(string $method, array $parameters)
{
$response = [];
foreach ($this->drivers as $acl) {
$response[] = $acl->{$method}(...$parameters);
}
return $response;
}
/**
* Shutdown/finish all ACL.
*
* @return $this
*/
public function finish()
{
// Re-sync before shutting down.
foreach ($this->drivers as $acl) {
$acl->sync();
}
$this->drivers = [];
return $this;
}
/**
* Get all ACL instances.
*/
public function all(): array
{
return $this->drivers;
}
/**
* Get ACL instance by name.
*/
public function get(string $name): ?AuthorizationContract
{
return $this->drivers[$name] ?? null;
}
}