1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraCrafts\UrlShortener; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use Illuminate\Routing\UrlGenerator; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
|
11
|
|
|
class UrlShortenerServiceProvider extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Boot package services. |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
52 |
|
public function boot() |
19
|
|
|
{ |
20
|
52 |
|
$this->mergeConfigFrom(__DIR__ . '/../config/url-shortener.php', 'url-shortener'); |
21
|
52 |
|
$this->publishAssets(); |
22
|
52 |
|
$this->registerMacros(); |
23
|
52 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Publish package assets. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
52 |
|
protected function publishAssets() |
31
|
|
|
{ |
32
|
52 |
|
if (!$this->app->runningInConsole() || Str::contains($this->app->version(), 'Lumen')) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
52 |
|
$this->publishes([ |
37
|
52 |
|
__DIR__ . '/../config/url-shortener.php' => config_path('url-shortener.php'), |
38
|
|
|
]); |
39
|
52 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Register package services. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
52 |
|
public function register() |
47
|
|
|
{ |
48
|
52 |
|
$this->app->alias('url.shortener', UrlShortenerManager::class); |
49
|
52 |
|
$this->app->bindIf(ClientInterface::class, Client::class); |
50
|
|
|
|
51
|
48 |
|
$this->app->singleton('url.shortener', function ($app) { |
52
|
29 |
|
return new UrlShortenerManager($app); |
53
|
52 |
|
}); |
54
|
52 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Register UrlGenerator macro's. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
52 |
|
protected function registerMacros() |
62
|
|
|
{ |
63
|
52 |
|
if (!class_exists(UrlGenerator::class) || !method_exists(UrlGenerator::class, 'macro')) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
48 |
|
UrlGenerator::macro('shorten', function (...$parameters) { |
68
|
|
|
return app('url.shortener')->shorten(...$parameters); |
|
|
|
|
69
|
52 |
|
}); |
70
|
|
|
|
71
|
48 |
|
UrlGenerator::macro('shortenAsync', function (...$parameters) { |
72
|
|
|
return app('url.shortener')->shortenAsync(...$parameters); |
|
|
|
|
73
|
52 |
|
}); |
74
|
|
|
|
75
|
48 |
|
UrlGenerator::macro('shortener', function () { |
76
|
|
|
return app('url.shortener'); |
77
|
52 |
|
}); |
78
|
10 |
|
} |
79
|
|
|
} |
80
|
|
|
|