|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Limsum; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Contracts\Container\Container; |
|
7
|
|
|
use Illuminate\Support\Facades\Route; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use Limsum\Contracts\LimsumUrlMaker; |
|
11
|
|
|
use Limsum\Http\Controllers\ColorBlockController; |
|
12
|
|
|
use Limsum\UrlMakers\ColorBlockUrl; |
|
13
|
|
|
use Limsum\UrlMakers\LoremPicsumUrl; |
|
14
|
|
|
|
|
15
|
|
|
class LipsumManager |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The application instance. |
|
20
|
|
|
* |
|
21
|
|
|
* @var Container |
|
22
|
|
|
*/ |
|
23
|
|
|
protected Container $app; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The array of resolved image drivers. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected array $drivers = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The registered custom driver creators. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected array $customCreators = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a new manager instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Container $app |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
26 |
|
public function __construct(Container $app) |
|
47
|
|
|
{ |
|
48
|
26 |
|
$this->app = $app; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string|null $prefix |
|
53
|
|
|
* @param string|null $namePrefix |
|
54
|
|
|
*/ |
|
55
|
16 |
|
public function routes(?string $prefix = null, ?string $namePrefix = null): void |
|
56
|
|
|
{ |
|
57
|
16 |
|
$prefix = $prefix ?? $this->app['config']['lorem-image.route.prefix']; |
|
58
|
16 |
|
$namePrefix = $namePrefix ?? $this->app['config']['lorem-image.route.name_prefix']; |
|
59
|
|
|
|
|
60
|
16 |
|
Route::prefix($prefix) |
|
61
|
16 |
|
->as("{$namePrefix}.") |
|
62
|
16 |
|
->where([ |
|
63
|
16 |
|
'extension' => '(' . implode('|', config('lorem-image.drivers.color-block.extensions')) . ')', |
|
64
|
|
|
]) |
|
65
|
16 |
|
->group(function () { |
|
66
|
16 |
|
Route::get('color-block.{extension}', ColorBlockController::class) |
|
67
|
16 |
|
->name('color-block'); |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get a driver instance. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string|null $name |
|
75
|
|
|
* |
|
76
|
|
|
* @return LimsumUrlMaker |
|
77
|
|
|
*/ |
|
78
|
25 |
|
public function driver(?string $name = null): LimsumUrlMaker |
|
79
|
|
|
{ |
|
80
|
25 |
|
$name = $name ?: $this->getDefaultDriver(); |
|
81
|
|
|
|
|
82
|
25 |
|
return $this->drivers[ $name ] = $this->get($name); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Resolve the given broadcaster. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $name |
|
89
|
|
|
* |
|
90
|
|
|
* @return LimsumUrlMaker |
|
91
|
|
|
* |
|
92
|
|
|
* @throws InvalidArgumentException |
|
93
|
|
|
*/ |
|
94
|
25 |
|
protected function resolve(string $name): LimsumUrlMaker |
|
95
|
|
|
{ |
|
96
|
25 |
|
$config = $this->getConfig($name) ?? []; |
|
97
|
|
|
|
|
98
|
25 |
|
if (isset($this->customCreators[ $name ])) { |
|
99
|
1 |
|
return $this->customCreators[ $name ]($config); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
24 |
|
$driverMethod = 'create' . ucfirst(Str::camel($name)) . 'Driver'; |
|
103
|
|
|
|
|
104
|
24 |
|
if (!method_exists($this, $driverMethod)) { |
|
105
|
1 |
|
throw new InvalidArgumentException("Driver [{$name}] is not supported."); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
23 |
|
return $this->{$driverMethod}($config); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param array $config |
|
113
|
|
|
* |
|
114
|
|
|
* @return LimsumUrlMaker |
|
115
|
|
|
*/ |
|
116
|
11 |
|
protected function createColorBlockDriver(array $config = []): LimsumUrlMaker |
|
117
|
|
|
{ |
|
118
|
11 |
|
return new ColorBlockUrl($config); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param array $config |
|
123
|
|
|
* |
|
124
|
|
|
* @return LimsumUrlMaker |
|
125
|
|
|
*/ |
|
126
|
12 |
|
protected function createLoremPicsumDriver(array $config = []): LimsumUrlMaker |
|
127
|
|
|
{ |
|
128
|
12 |
|
return new LoremPicsumUrl($config); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Register a custom driver creator Closure. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $driver |
|
135
|
|
|
* @param Closure $callback |
|
136
|
|
|
* |
|
137
|
|
|
* @return static |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function extend(string $driver, Closure $callback): static |
|
140
|
|
|
{ |
|
141
|
1 |
|
$this->customCreators[ $driver ] = $callback; |
|
142
|
|
|
|
|
143
|
1 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the connection configuration. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string|null $name |
|
150
|
|
|
* |
|
151
|
|
|
* @return array|null |
|
152
|
|
|
*/ |
|
153
|
25 |
|
protected function getConfig(?string $name): ?array |
|
154
|
|
|
{ |
|
155
|
25 |
|
if (!is_null($name) && $name !== 'null') { |
|
156
|
24 |
|
return $this->app['config']["lorem-image.drivers.{$name}"]; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
1 |
|
return null; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get the default driver name. |
|
164
|
|
|
* |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
4 |
|
public function getDefaultDriver(): string |
|
168
|
|
|
{ |
|
169
|
4 |
|
return $this->app['config']['lorem-image.default.driver']; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Set the default driver name. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $name |
|
176
|
|
|
* |
|
177
|
|
|
* @return static |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function setDefaultDriver(string $name): static |
|
180
|
|
|
{ |
|
181
|
1 |
|
$this->app['config']['lorem-image.default.driver'] = $name; |
|
182
|
|
|
|
|
183
|
1 |
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Attempt to get the connection from the local cache. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $name |
|
190
|
|
|
* |
|
191
|
|
|
* @return LimsumUrlMaker |
|
192
|
|
|
*/ |
|
193
|
25 |
|
protected function get(string $name): LimsumUrlMaker |
|
194
|
|
|
{ |
|
195
|
25 |
|
return $this->drivers[ $name ] ?? $this->resolve($name); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Delete resolved driver. |
|
200
|
|
|
* |
|
201
|
|
|
* @param string|null $name |
|
202
|
|
|
* |
|
203
|
|
|
* @return void |
|
204
|
|
|
*/ |
|
205
|
1 |
|
public function purge(?string $name = null) |
|
206
|
|
|
{ |
|
207
|
1 |
|
$name = $name ?? $this->getDefaultDriver(); |
|
208
|
|
|
|
|
209
|
1 |
|
unset($this->drivers[ $name ]); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Forget all of the resolved driver instances. |
|
214
|
|
|
* |
|
215
|
|
|
* @return static |
|
216
|
|
|
*/ |
|
217
|
1 |
|
public function forgetDrivers(): static |
|
218
|
|
|
{ |
|
219
|
1 |
|
$this->drivers = []; |
|
220
|
|
|
|
|
221
|
1 |
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Get the application instance used by the manager. |
|
226
|
|
|
* |
|
227
|
|
|
* @return Container |
|
228
|
|
|
*/ |
|
229
|
1 |
|
public function getApplication(): Container |
|
230
|
|
|
{ |
|
231
|
1 |
|
return $this->app; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Set the application instance used by the manager. |
|
236
|
|
|
* |
|
237
|
|
|
* @param Container $app |
|
238
|
|
|
* |
|
239
|
|
|
* @return static |
|
240
|
|
|
*/ |
|
241
|
1 |
|
public function setApplication(Container $app): static |
|
242
|
|
|
{ |
|
243
|
1 |
|
$this->app = $app; |
|
244
|
|
|
|
|
245
|
1 |
|
return $this; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Dynamically call the default driver instance. |
|
250
|
|
|
* |
|
251
|
|
|
* @param string $method |
|
252
|
|
|
* @param array $parameters |
|
253
|
|
|
* |
|
254
|
|
|
* @return mixed |
|
255
|
|
|
*/ |
|
256
|
4 |
|
public function __call($method, $parameters) |
|
257
|
|
|
{ |
|
258
|
4 |
|
return $this->driver()->$method(...$parameters); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|