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 | public function boot() |
||||
19 | { |
||||
20 | $this->mergeConfigFrom(__DIR__ . '/../config/url-shortener.php', 'url-shortener'); |
||||
21 | $this->publishAssets(); |
||||
22 | $this->registerMacros(); |
||||
23 | } |
||||
24 | |||||
25 | /** |
||||
26 | * Publish package assets. |
||||
27 | * |
||||
28 | * @return void |
||||
29 | */ |
||||
30 | protected function publishAssets() |
||||
31 | { |
||||
32 | if (!$this->app->runningInConsole() || Str::contains($this->app->version(), 'Lumen')) { |
||||
33 | return; |
||||
34 | } |
||||
35 | |||||
36 | $this->publishes([ |
||||
37 | __DIR__ . '/../config/url-shortener.php' => config_path('url-shortener.php'), |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
38 | ]); |
||||
39 | } |
||||
40 | |||||
41 | /** |
||||
42 | * Register package services. |
||||
43 | * |
||||
44 | * @return void |
||||
45 | */ |
||||
46 | public function register() |
||||
47 | { |
||||
48 | $this->app->alias('url.shortener', UrlShortenerManager::class); |
||||
49 | $this->app->bindIf(ClientInterface::class, Client::class); |
||||
50 | |||||
51 | $this->app->singleton('url.shortener', function ($app) { |
||||
52 | return new UrlShortenerManager($app); |
||||
53 | }); |
||||
54 | } |
||||
55 | |||||
56 | /** |
||||
57 | * Register UrlGenerator macro's. |
||||
58 | * |
||||
59 | * @return void |
||||
60 | */ |
||||
61 | protected function registerMacros() |
||||
62 | { |
||||
63 | if (!class_exists(UrlGenerator::class) || !method_exists(UrlGenerator::class, 'macro')) { |
||||
64 | return; |
||||
65 | } |
||||
66 | |||||
67 | UrlGenerator::macro('shorten', function (...$parameters) { |
||||
68 | return app('url.shortener')->shorten(...$parameters); |
||||
0 ignored issues
–
show
The function
app was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
69 | }); |
||||
70 | |||||
71 | UrlGenerator::macro('shortenAsync', function (...$parameters) { |
||||
72 | return app('url.shortener')->shortenAsync(...$parameters); |
||||
0 ignored issues
–
show
The function
app was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
73 | }); |
||||
74 | |||||
75 | UrlGenerator::macro('shortener', function () { |
||||
76 | return app('url.shortener'); |
||||
0 ignored issues
–
show
The function
app was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
77 | }); |
||||
78 | } |
||||
79 | } |
||||
80 |