1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RuthgerIdema\UrlRewrite; |
6
|
|
|
|
7
|
|
|
use RuthgerIdema\UrlRewrite\Facades\UrlRewrite; |
8
|
|
|
use RuthgerIdema\UrlRewrite\Http\UrlRewriteController; |
9
|
|
|
use RuthgerIdema\UrlRewrite\Repositories\Interfaces\UrlRewriteInterface; |
10
|
|
|
|
11
|
|
|
class ServiceProvider extends \Illuminate\Support\ServiceProvider |
12
|
|
|
{ |
13
|
|
|
public function boot(): void |
14
|
|
|
{ |
15
|
|
|
if ($this->app->runningInConsole()) { |
16
|
|
|
if (! class_exists('CreateUrlRewriteTable')) { |
17
|
|
|
$timestamp = date('Y_m_d_His'); |
18
|
|
|
$this->publishes([ |
19
|
|
|
__DIR__.'/../database/migrations/create_url_rewrites_table.php.stub' => database_path('migrations/'.$timestamp.'_create_url_rewrites_table.php'), |
20
|
|
|
], 'migrations'); |
21
|
|
|
} |
22
|
|
|
$this->publishes([ |
23
|
|
|
__DIR__.'/../config/url-rewrite.php' => config_path('url-rewrite.php'), |
24
|
|
|
], 'config'); |
25
|
|
|
$this->publishes([ |
26
|
|
|
__DIR__.'/../nova/UrlRewrite.php.stub' => app_path('Nova/UrlRewrite.php'), |
27
|
|
|
], 'nova'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'urlrewrites'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function register(): void |
34
|
|
|
{ |
35
|
|
|
$this->mergeConfigFrom( |
36
|
|
|
__DIR__.'/../config/url-rewrite.php', |
37
|
|
|
'url-rewrite' |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$this->registerRepository(); |
41
|
|
|
$this->registerFacade(); |
42
|
|
|
$this->registerRouteMacro(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function registerRouteMacro(): void |
46
|
|
|
{ |
47
|
|
|
$queryParam = '.*'; |
48
|
|
|
|
49
|
|
|
if (class_exists('Laravel\\Nova\\Nova')) { |
50
|
|
|
$novaPath = ltrim($this->app['config']['nova']['path'], '/'); |
51
|
|
|
$queryParam = "^(?!$novaPath).*"; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$router = $this->app['router']; |
55
|
|
|
$router->macro('rewrites', function () use ($router, $queryParam) { |
56
|
|
|
$router->get('{url}', '\\'.UrlRewriteController::class)->where('url', $queryParam)->name('url.rewrite'); |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function registerRepository(): void |
61
|
|
|
{ |
62
|
|
|
$this->app->singleton(UrlRewriteInterface::class, function () { |
63
|
|
|
$urlRewriteConfig = $this->app['config']['url-rewrite']; |
64
|
|
|
$repositoryClass = $urlRewriteConfig['repository']; |
65
|
|
|
$modelClass = $urlRewriteConfig['model']; |
66
|
|
|
|
67
|
|
|
$repository = new $repositoryClass(new $modelClass); |
68
|
|
|
|
69
|
|
|
if (! $urlRewriteConfig['cache']) { |
70
|
|
|
return $repository; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$cacheClass = $urlRewriteConfig['cache-decorator']; |
74
|
|
|
|
75
|
|
|
return new $cacheClass($repository, $this->app['cache.store']); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function registerFacade(): void |
80
|
|
|
{ |
81
|
|
|
$this->app->bind(UrlRewrite::class, function () { |
82
|
|
|
return $this->app->make(UrlRewriteInterface::class); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|