1 | <?php namespace Arcanedev\Hasher; |
||
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\Foundation\Application $app |
||
36 | */ |
||
37 | 30 | public function __construct($app) |
|
38 | { |
||
39 | 30 | parent::__construct($app); |
|
40 | |||
41 | 30 | $this->buildDrivers(); |
|
42 | 30 | } |
|
43 | |||
44 | /* ----------------------------------------------------------------- |
||
45 | | Getters & Setters |
||
46 | | ----------------------------------------------------------------- |
||
47 | */ |
||
48 | |||
49 | /** |
||
50 | * Get the default driver name. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | 15 | public function getDefaultDriver() |
|
55 | { |
||
56 | 15 | return $this->getHasherConfig('default.driver'); |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get the default driver connection. |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | 30 | public function getDefaultOption() |
|
65 | { |
||
66 | 30 | return $this->option ?? $this->getHasherConfig('default.option', 'main'); |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Set the hasher option. |
||
71 | * |
||
72 | * @param string $option |
||
73 | * |
||
74 | * @return \Arcanedev\Hasher\HashManager |
||
75 | */ |
||
76 | 12 | public function option($option = null) |
|
77 | { |
||
78 | 12 | if ( ! is_null($option)) { |
|
79 | 12 | $this->option = $option; |
|
80 | 12 | $this->buildDrivers(); |
|
81 | } |
||
82 | |||
83 | 12 | 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 | 6 | public function with($option = null, $driver = null) |
|
100 | { |
||
101 | 6 | 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 | 18 | public function driver($driver = null) |
|
112 | { |
||
113 | 18 | return parent::driver($driver); |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Build all hash drivers. |
||
118 | */ |
||
119 | 30 | private function buildDrivers() |
|
120 | { |
||
121 | 30 | $drivers = $this->getHasherConfig('drivers', []); |
|
122 | |||
123 | 30 | foreach ($drivers as $name => $driver) { |
|
124 | 30 | $this->buildDriver($name, $driver); |
|
125 | } |
||
126 | 30 | } |
|
127 | |||
128 | /** |
||
129 | * Build the driver. |
||
130 | * |
||
131 | * @param string $name |
||
132 | * @param array $driver |
||
133 | * |
||
134 | * @return \Arcanedev\Hasher\Contracts\HashDriver |
||
135 | */ |
||
136 | 30 | private function buildDriver($name, array $driver) |
|
137 | { |
||
138 | 30 | return $this->drivers[$name] = new $driver['driver']( |
|
139 | 30 | Arr::get($driver, 'options.'.$this->getDefaultOption(), []) |
|
140 | ); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Encode the value. |
||
145 | * |
||
146 | * @param mixed $value |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 3 | public function encode($value) |
|
151 | { |
||
152 | 3 | return $this->driver()->encode($value); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Decode the hashed value. |
||
157 | * |
||
158 | * @param string $hashed |
||
159 | * |
||
160 | * @return mixed |
||
161 | */ |
||
162 | 3 | public function decode($hashed) |
|
163 | { |
||
164 | 3 | return $this->driver()->decode($hashed); |
|
165 | } |
||
166 | |||
167 | /* ----------------------------------------------------------------- |
||
168 | | Other Methods |
||
169 | | ----------------------------------------------------------------- |
||
170 | */ |
||
171 | |||
172 | /** |
||
173 | * Get the hasher config. |
||
174 | * |
||
175 | * @param string $name |
||
176 | * @param mixed|null $default |
||
177 | * |
||
178 | * @return mixed |
||
179 | */ |
||
180 | 30 | protected function getHasherConfig($name, $default = null) |
|
184 | } |
||
185 |