|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaraCrafts\UrlShortener; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use GuzzleHttp\ClientInterface; |
|
7
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use LaraCrafts\UrlShortener\Contracts\Factory as FactoryContract; |
|
12
|
|
|
use LaraCrafts\UrlShortener\Http\BitLyShortener; |
|
13
|
|
|
use LaraCrafts\UrlShortener\Http\FirebaseShortener; |
|
14
|
|
|
use LaraCrafts\UrlShortener\Http\IsGdShortener; |
|
15
|
|
|
use LaraCrafts\UrlShortener\Http\OuoIoShortener; |
|
16
|
|
|
use LaraCrafts\UrlShortener\Http\PolrShortener; |
|
17
|
|
|
use LaraCrafts\UrlShortener\Http\ShorteStShortener; |
|
18
|
|
|
use LaraCrafts\UrlShortener\Http\TinyUrlShortener; |
|
19
|
|
|
|
|
20
|
|
|
class UrlShortenerManager implements FactoryContract |
|
21
|
|
|
{ |
|
22
|
|
|
protected $app; |
|
23
|
|
|
protected $customCreators; |
|
24
|
|
|
protected $shorteners; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create a new URL shortener manager instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
288 |
|
public function __construct(Application $app) |
|
33
|
|
|
{ |
|
34
|
288 |
|
$this->app = $app; |
|
35
|
288 |
|
$this->customCreators = []; |
|
36
|
288 |
|
$this->shorteners = []; |
|
37
|
288 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Call a custom driver creator. |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $config |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
24 |
|
protected function callCustomCreator(array $config) |
|
46
|
|
|
{ |
|
47
|
24 |
|
return $this->customCreators[$config['driver']]($this->app, $config); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create an instance of the Bit.ly driver. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $config |
|
54
|
|
|
* @return \LaraCrafts\UrlShortener\Http\BitLyShortener |
|
55
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
56
|
|
|
*/ |
|
57
|
24 |
|
protected function createBitLyDriver(array $config) |
|
58
|
|
|
{ |
|
59
|
24 |
|
return new BitLyShortener( |
|
60
|
24 |
|
$this->app->make(ClientInterface::class), |
|
61
|
24 |
|
Arr::get($config, 'token'), |
|
|
|
|
|
|
62
|
24 |
|
Arr::get($config, 'domain', 'bit.ly') |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Create an instance of the Firebase driver. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $config |
|
70
|
|
|
* @return \LaraCrafts\UrlShortener\Http\FirebaseShortener |
|
71
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
72
|
|
|
*/ |
|
73
|
24 |
|
protected function createFirebaseDriver(array $config) |
|
74
|
|
|
{ |
|
75
|
24 |
|
return new FirebaseShortener( |
|
76
|
24 |
|
$this->app->make(ClientInterface::class), |
|
77
|
24 |
|
Arr::get($config, 'token'), |
|
|
|
|
|
|
78
|
24 |
|
Arr::get($config, 'prefix'), |
|
|
|
|
|
|
79
|
24 |
|
Arr::get($config, 'suffix') |
|
|
|
|
|
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Create an instance of the Is.gd driver. |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $config |
|
87
|
|
|
* @return \LaraCrafts\UrlShortener\Http\IsGdShortener |
|
88
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
89
|
|
|
*/ |
|
90
|
48 |
|
protected function createIsGdDriver(array $config) |
|
91
|
|
|
{ |
|
92
|
48 |
|
return new IsGdShortener( |
|
93
|
48 |
|
$this->app->make(ClientInterface::class), |
|
94
|
48 |
|
Arr::get($config, 'base_uri'), |
|
95
|
48 |
|
Arr::get($config, 'statistics') |
|
|
|
|
|
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Create an instance of the Ouo.io driver. |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $config |
|
103
|
|
|
* @return \LaraCrafts\UrlShortener\Http\OuoIoShortener |
|
104
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
105
|
|
|
*/ |
|
106
|
24 |
|
protected function createOuoIoDriver(array $config) |
|
107
|
|
|
{ |
|
108
|
24 |
|
return new OuoIoShortener( |
|
109
|
24 |
|
$this->app->make(ClientInterface::class), |
|
110
|
24 |
|
Arr::get($config, 'token') |
|
|
|
|
|
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Create an instance of the Polr driver. |
|
116
|
|
|
* |
|
117
|
|
|
* @param array $config |
|
118
|
|
|
* @return \LaraCrafts\UrlShortener\Http\PolrShortener |
|
119
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
120
|
|
|
*/ |
|
121
|
24 |
|
protected function createPolrDriver(array $config) |
|
122
|
|
|
{ |
|
123
|
24 |
|
return new PolrShortener( |
|
124
|
24 |
|
$this->app->make(ClientInterface::class), |
|
125
|
24 |
|
Arr::get($config, 'token'), |
|
|
|
|
|
|
126
|
24 |
|
Arr::get($config, 'prefix') |
|
|
|
|
|
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Create an instance of the Shorte.st driver. |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $config |
|
134
|
|
|
* @return \LaraCrafts\UrlShortener\Http\ShorteStShortener |
|
135
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
136
|
|
|
*/ |
|
137
|
24 |
|
protected function createShorteStDriver(array $config) |
|
138
|
|
|
{ |
|
139
|
24 |
|
return new ShorteStShortener( |
|
140
|
24 |
|
$this->app->make(ClientInterface::class), |
|
141
|
24 |
|
Arr::get($config, 'token') |
|
|
|
|
|
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Create an instance of the TinyURL driver. |
|
147
|
|
|
* |
|
148
|
|
|
* @return \LaraCrafts\UrlShortener\Http\TinyUrlShortener |
|
149
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
150
|
|
|
*/ |
|
151
|
48 |
|
protected function createTinyUrlDriver() |
|
152
|
|
|
{ |
|
153
|
48 |
|
return new TinyUrlShortener($this->app->make(ClientInterface::class)); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get a URL shortener driver instance. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string|null $name |
|
160
|
|
|
* @return \LaraCrafts\UrlShortener\Contracts\Shortener |
|
161
|
|
|
*/ |
|
162
|
24 |
|
public function driver(string $name = null) |
|
163
|
|
|
{ |
|
164
|
24 |
|
return $this->shortener($name); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Register a custom driver creator closure. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $name |
|
171
|
|
|
* @param \Closure $callback |
|
172
|
|
|
* @return $this |
|
173
|
|
|
*/ |
|
174
|
24 |
|
public function extend(string $name, Closure $callback) |
|
175
|
|
|
{ |
|
176
|
24 |
|
$this->customCreators[$name] = $callback->bindTo($this, $this); |
|
177
|
|
|
|
|
178
|
24 |
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get the default URL shortener driver name. |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
48 |
|
public function getDefaultDriver() |
|
187
|
|
|
{ |
|
188
|
48 |
|
return $this->app['config']['url-shortener.default']; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Get the URL shortener configuration. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $name |
|
195
|
|
|
* @return array|null |
|
196
|
|
|
*/ |
|
197
|
240 |
|
protected function getShortenerConfig(string $name) |
|
198
|
|
|
{ |
|
199
|
240 |
|
return $this->app['config']["url-shortener.shorteners.$name"]; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Resolve the given URL shortener. |
|
204
|
|
|
* |
|
205
|
|
|
* @param string $name |
|
206
|
|
|
* @return \LaraCrafts\UrlShortener\Contracts\Shortener |
|
207
|
|
|
*/ |
|
208
|
240 |
|
protected function resolve(string $name) |
|
209
|
|
|
{ |
|
210
|
240 |
|
$config = $this->getShortenerConfig($name); |
|
211
|
|
|
|
|
212
|
240 |
|
if (is_null($config) || !array_key_exists('driver', $config)) { |
|
213
|
|
|
throw new InvalidArgumentException("URL shortener [{$name}] is not defined"); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
240 |
|
if (array_key_exists($config['driver'], $this->customCreators)) { |
|
217
|
24 |
|
return $this->callCustomCreator($config); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
216 |
|
$driverMethod = 'create' . Str::studly($config['driver']) . 'Driver'; |
|
221
|
|
|
|
|
222
|
216 |
|
if (method_exists($this, $driverMethod)) { |
|
223
|
216 |
|
return $this->$driverMethod($config); |
|
224
|
|
|
} |
|
225
|
|
|
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported"); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Set the default URL shortener driver name. |
|
230
|
|
|
* |
|
231
|
|
|
* @param string $name |
|
232
|
|
|
* @return $this |
|
233
|
|
|
*/ |
|
234
|
24 |
|
public function setDefaultDriver(string $name) |
|
235
|
|
|
{ |
|
236
|
24 |
|
$this->app['config']['url-shortener.default'] = $name; |
|
237
|
|
|
|
|
238
|
24 |
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* {@inheritDoc} |
|
243
|
|
|
*/ |
|
244
|
240 |
|
public function shortener(string $name = null) |
|
245
|
|
|
{ |
|
246
|
240 |
|
$name = $name ?: $this->getDefaultDriver(); |
|
247
|
|
|
|
|
248
|
240 |
|
if (array_key_exists($name, $this->shorteners)) { |
|
249
|
|
|
return $this->shorteners[$name]; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
240 |
|
return $this->shorteners[$name] = $this->resolve($name); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|