-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuthManager.php
More file actions
54 lines (47 loc) · 1.37 KB
/
AuthManager.php
File metadata and controls
54 lines (47 loc) · 1.37 KB
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
<?php
namespace WebmanTech\Auth;
use InvalidArgumentException;
use WebmanTech\Auth\Guard\Guard;
use WebmanTech\Auth\Helper\ConfigHelper;
use WebmanTech\Auth\Interfaces\GuardInterface;
class AuthManager
{
protected array $guards = [];
/**
* @param string|null $name
* @return GuardInterface
*/
public function guard(?string $name = null): GuardInterface
{
$name ??= (string)ConfigHelper::get(('auth.default'));
if (!isset($this->guards[$name])) {
$this->guards[$name] = $this->createGuard($this->getConfig($name));
}
return $this->guards[$name];
}
/**
* @param string $name
* @return array
*/
protected function getConfig(string $name): array
{
$config = (array)ConfigHelper::get("auth.guards.{$name}");
if (!$config) {
throw new InvalidArgumentException($name . 'not exist in auth.guards');
}
return $config;
}
/**
* @param array $config
* @return GuardInterface
*/
protected function createGuard(array $config): GuardInterface
{
$guardClass = $config['class'] ?? Guard::class;
$guard = new $guardClass($config);
if (!$guard instanceof GuardInterface) {
throw new InvalidArgumentException('class 必须是 GuardInterface 的实现');
}
return $guard;
}
}