1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Utility; |
4
|
|
|
|
5
|
|
|
use ByTIC\Hello\Models\AccessTokens\Tokens; |
6
|
|
|
use ByTIC\Hello\Models\AuthCodes\AuthCodes; |
7
|
|
|
use ByTIC\Hello\Models\Clients\Clients; |
8
|
|
|
use ByTIC\Hello\Models\RefreshTokens\RefreshTokens; |
9
|
|
|
use ByTIC\Hello\Models\Scopes\Scopes; |
10
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
11
|
|
|
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; |
12
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; |
13
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; |
14
|
|
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; |
15
|
|
|
use League\OAuth2\Server\Repositories\UserRepositoryInterface; |
16
|
|
|
use Nip\Records\Locator\ModelLocator; |
17
|
|
|
use Nip\Records\RecordManager; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ModelsHelper |
21
|
|
|
* @package ByTIC\Hello\Utility |
22
|
|
|
*/ |
23
|
|
|
class ModelsHelper |
24
|
|
|
{ |
25
|
|
|
protected static $resolved = []; |
26
|
|
|
|
27
|
|
|
protected static $repositories = [ |
28
|
|
|
AccessTokenRepositoryInterface::class => Tokens::class, |
29
|
|
|
ClientRepositoryInterface::class => Clients::class, |
30
|
|
|
RefreshTokenRepositoryInterface::class => RefreshTokens::class, |
31
|
|
|
ScopeRepositoryInterface::class => Scopes::class, |
32
|
|
|
AuthCodeRepositoryInterface::class => AuthCodes::class, |
33
|
|
|
UserRepositoryInterface::class => null, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return Clients|\Nip\Records\AbstractModels\RecordManager |
38
|
|
|
*/ |
39
|
9 |
|
public static function clients() |
40
|
|
|
{ |
41
|
9 |
|
return static::getRepository(ClientRepositoryInterface::class); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param RecordManager $manager |
46
|
|
|
*/ |
47
|
7 |
|
public static function useClientsManager(RecordManager $manager) |
48
|
|
|
{ |
49
|
7 |
|
static::setRepository(ClientRepositoryInterface::class, $manager); |
50
|
7 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Tokens|\Nip\Records\AbstractModels\RecordManager |
54
|
|
|
*/ |
55
|
|
|
public static function accessTokens() |
56
|
|
|
{ |
57
|
|
|
return static::getRepository(AccessTokenRepositoryInterface::class); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return Scopes|\Nip\Records\AbstractModels\RecordManager |
62
|
|
|
*/ |
63
|
|
|
public static function scopes() |
64
|
|
|
{ |
65
|
|
|
return static::getRepository(ScopeRepositoryInterface::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
1 |
|
public static function repositories() |
72
|
|
|
{ |
73
|
1 |
|
return static::$repositories; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
3 |
|
public static function reset() |
80
|
|
|
{ |
81
|
3 |
|
return static::$resolved =[]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $alias |
86
|
|
|
* @return \Nip\Records\AbstractModels\RecordManager |
87
|
|
|
*/ |
88
|
9 |
|
protected static function getRepository($alias) |
89
|
|
|
{ |
90
|
9 |
|
if (!isset(static::$resolved[$alias])) { |
91
|
2 |
|
static::$resolved[$alias] = static::generateRepository($alias); |
92
|
|
|
} |
93
|
9 |
|
return static::$resolved[$alias]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $interface |
98
|
|
|
* @return \Nip\Records\AbstractModels\RecordManager |
99
|
|
|
*/ |
100
|
2 |
|
protected static function generateRepository($interface) |
101
|
|
|
{ |
102
|
2 |
|
$class = ConfigHelper::get('repositories.' . $interface, false); |
103
|
2 |
|
if (!empty($class) && class_exists($class)) { |
|
|
|
|
104
|
1 |
|
return ModelLocator::get($class); |
105
|
|
|
} |
106
|
1 |
|
return ModelLocator::get(static::$repositories[$interface]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $interface |
111
|
|
|
* @param RecordManager $manager |
112
|
|
|
*/ |
113
|
7 |
|
protected static function setRepository($interface, $manager) |
114
|
|
|
{ |
115
|
7 |
|
static::$resolved[$interface] = $manager; |
116
|
7 |
|
ModelLocator::set($interface, $manager); |
117
|
7 |
|
} |
118
|
|
|
} |
119
|
|
|
|