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