1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Potievdev\SlimRbac\Component\Config; |
4
|
|
|
|
5
|
|
|
use Potievdev\SlimRbac\Exception\ConfigNotFoundException; |
6
|
|
|
|
7
|
|
|
class RbacConfig |
8
|
|
|
{ |
9
|
|
|
public const ATTRIBUTE_RESOURCE_TYPE = 'attribute'; |
10
|
|
|
public const HEADER_RESOURCE_TYPE = 'header'; |
11
|
|
|
public const COOKIE_RESOURCE_TYPE = 'cookie'; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $databaseDriver; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $databaseHost; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $databaseUser; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $databasePassword; |
24
|
|
|
|
25
|
|
|
/** @var int */ |
26
|
|
|
private $databasePort; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $databaseName; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $databaseCharset; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
private $userIdFieldName; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
private $userIdResourceType; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $databaseDriver |
42
|
|
|
* @param string $databaseHost |
43
|
|
|
* @param string $databaseUser |
44
|
|
|
* @param string $databasePassword |
45
|
|
|
* @param int $databasePort |
46
|
|
|
* @param string $databaseName |
47
|
|
|
* @param string $databaseCharset |
48
|
|
|
* @param string $userIdFieldName |
49
|
|
|
* @param string $userIdResourceType |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
string $databaseDriver, |
53
|
|
|
string $databaseHost, |
54
|
|
|
string $databaseUser, |
55
|
|
|
string $databasePassword, |
56
|
|
|
int $databasePort, |
57
|
|
|
string $databaseName, |
58
|
|
|
string $databaseCharset, |
59
|
|
|
string $userIdFieldName, |
60
|
|
|
string $userIdResourceType |
61
|
|
|
) { |
62
|
|
|
$this->databaseDriver = $databaseDriver; |
63
|
|
|
$this->databaseHost = $databaseHost; |
64
|
|
|
$this->databaseUser = $databaseUser; |
65
|
|
|
$this->databasePassword = $databasePassword; |
66
|
|
|
$this->databasePort = $databasePort; |
67
|
|
|
$this->databaseName = $databaseName; |
68
|
|
|
$this->databaseCharset = $databaseCharset; |
69
|
|
|
$this->userIdFieldName = $userIdFieldName; |
70
|
|
|
$this->userIdResourceType = $userIdResourceType; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @throws ConfigNotFoundException |
75
|
|
|
*/ |
76
|
|
|
public static function createFromConfigFile(): RbacConfig |
77
|
|
|
{ |
78
|
|
|
return self::createFromConfigs(RbacConfigLoader::loadConfigs()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public static function createFromConfigs(array $configs): RbacConfig |
82
|
|
|
{ |
83
|
|
|
return new self( |
84
|
|
|
$configs['database']['driver'], |
85
|
|
|
$configs['database']['host'], |
86
|
|
|
$configs['database']['user'], |
87
|
|
|
$configs['database']['password'], |
88
|
|
|
$configs['database']['port'], |
89
|
|
|
$configs['database']['dbname'], |
90
|
|
|
$configs['database']['charset'], |
91
|
|
|
$configs['userId']['fieldName'], |
92
|
|
|
$configs['userId']['resourceType'], |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getDatabaseDriver(): string |
100
|
|
|
{ |
101
|
|
|
return $this->databaseDriver; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getDatabaseHost(): string |
108
|
|
|
{ |
109
|
|
|
return $this->databaseHost; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getDatabaseUser(): string |
116
|
|
|
{ |
117
|
|
|
return $this->databaseUser; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getDatabasePassword(): string |
124
|
|
|
{ |
125
|
|
|
return $this->databasePassword; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
|
|
public function getDatabasePort(): int |
132
|
|
|
{ |
133
|
|
|
return $this->databasePort; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function getDatabaseName(): string |
140
|
|
|
{ |
141
|
|
|
return $this->databaseName; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getDatabaseCharset(): string |
148
|
|
|
{ |
149
|
|
|
return $this->databaseCharset; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function getUserIdFieldName(): string |
156
|
|
|
{ |
157
|
|
|
return $this->userIdFieldName; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getUserIdResourceType(): string |
164
|
|
|
{ |
165
|
|
|
return $this->userIdResourceType; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |