1
|
|
|
<?php namespace Arcanedev\Hasher; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Hasher\Contracts\HashManager as HashManagerContract; |
4
|
|
|
use Illuminate\Contracts\Container\Container; |
5
|
|
|
use Illuminate\Support\Manager; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class HashManager |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\Hasher |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class HashManager extends Manager implements HashManagerContract |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Properties |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The driver option. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $option; |
26
|
|
|
|
27
|
|
|
/* ----------------------------------------------------------------- |
28
|
|
|
| Constructor |
29
|
|
|
| ----------------------------------------------------------------- |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* HashManager constructor. |
34
|
|
|
* |
35
|
|
|
* @param \Illuminate\Contracts\Container\Container $container |
36
|
|
|
*/ |
37
|
20 |
|
public function __construct(Container $container) |
38
|
|
|
{ |
39
|
20 |
|
parent::__construct($container); |
40
|
|
|
|
41
|
20 |
|
$this->buildDrivers(); |
42
|
20 |
|
} |
43
|
|
|
|
44
|
|
|
/* ----------------------------------------------------------------- |
45
|
|
|
| Getters & Setters |
46
|
|
|
| ----------------------------------------------------------------- |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the default driver name. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
10 |
|
public function getDefaultDriver() |
55
|
|
|
{ |
56
|
10 |
|
return $this->config('default.driver'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the default driver connection. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
20 |
|
public function getDefaultOption() |
65
|
|
|
{ |
66
|
20 |
|
return $this->option ?? $this->config('default.option', 'main'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the hasher option. |
71
|
|
|
* |
72
|
|
|
* @param string $option |
73
|
|
|
* |
74
|
|
|
* @return \Arcanedev\Hasher\HashManager |
75
|
|
|
*/ |
76
|
8 |
|
public function option($option = null) |
77
|
|
|
{ |
78
|
8 |
|
if ($this->option !== $option) { |
79
|
8 |
|
$this->option = $option; |
80
|
8 |
|
$this->buildDrivers(); |
81
|
|
|
} |
82
|
|
|
|
83
|
8 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/* ----------------------------------------------------------------- |
87
|
|
|
| Main Methods |
88
|
|
|
| ----------------------------------------------------------------- |
89
|
|
|
*/ |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get a driver instance. |
93
|
|
|
* |
94
|
|
|
* @param string $driver |
95
|
|
|
* @param string|null $option |
96
|
|
|
* |
97
|
|
|
* @return \Arcanedev\Hasher\Contracts\HashDriver |
98
|
|
|
*/ |
99
|
4 |
|
public function with($option = null, $driver = null) |
100
|
|
|
{ |
101
|
4 |
|
return $this->option($option)->driver($driver); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get a driver instance. |
106
|
|
|
* |
107
|
|
|
* @param string $driver |
108
|
|
|
* |
109
|
|
|
* @return \Arcanedev\Hasher\Contracts\HashDriver |
110
|
|
|
*/ |
111
|
12 |
|
public function driver($driver = null) |
112
|
|
|
{ |
113
|
12 |
|
return parent::driver($driver); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Build all hash drivers. |
118
|
|
|
*/ |
119
|
20 |
|
private function buildDrivers(): void |
120
|
|
|
{ |
121
|
20 |
|
foreach ($this->config('drivers', []) as $name => $driver) { |
122
|
20 |
|
$this->buildDriver($name, $driver); |
123
|
|
|
} |
124
|
20 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Build the driver. |
128
|
|
|
* |
129
|
|
|
* @param string $name |
130
|
|
|
* @param array $params |
131
|
|
|
*/ |
132
|
20 |
|
private function buildDriver(string $name, array $params): void |
133
|
|
|
{ |
134
|
20 |
|
$this->drivers[$name] = $this->container->make($params['driver'], [ |
135
|
20 |
|
'options' => $params['options'][$this->getDefaultOption()] ?? [], |
136
|
|
|
]); |
137
|
20 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Encode the value. |
141
|
|
|
* |
142
|
|
|
* @param mixed $value |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
2 |
|
public function encode($value) |
147
|
|
|
{ |
148
|
2 |
|
return $this->driver()->encode($value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Decode the hashed value. |
153
|
|
|
* |
154
|
|
|
* @param string $hashed |
155
|
|
|
* |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
2 |
|
public function decode($hashed) |
159
|
|
|
{ |
160
|
2 |
|
return $this->driver()->decode($hashed); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/* ----------------------------------------------------------------- |
164
|
|
|
| Other Methods |
165
|
|
|
| ----------------------------------------------------------------- |
166
|
|
|
*/ |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the hasher config. |
170
|
|
|
* |
171
|
|
|
* @param string $name |
172
|
|
|
* @param mixed|null $default |
173
|
|
|
* |
174
|
|
|
* @return mixed |
175
|
|
|
*/ |
176
|
20 |
|
protected function config(string $name, $default = null) |
177
|
|
|
{ |
178
|
20 |
|
return $this->config->get("hasher.$name", $default); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|