|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fomvasss\UrlAliases; |
|
4
|
|
|
|
|
5
|
|
|
use Fomvasss\UrlAliases\Middleware\UrlAliasMiddleware; |
|
6
|
|
|
use Illuminate\Contracts\Http\Kernel; |
|
7
|
|
|
|
|
8
|
|
|
class ServiceProvider extends \Illuminate\Support\ServiceProvider |
|
9
|
|
|
{ |
|
10
|
|
|
protected $defer = false; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Bootstrap the application services. |
|
14
|
|
|
* |
|
15
|
|
|
* @return void |
|
16
|
|
|
*/ |
|
17
|
|
|
public function boot() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->publishConfig(); |
|
20
|
|
|
|
|
21
|
|
|
$this->publishMigrations(); |
|
22
|
|
|
|
|
23
|
|
|
$this->publishSeeder(); |
|
24
|
|
|
|
|
25
|
|
|
// $this->registerMiddleware(UrlAliasMiddleware::class); |
|
26
|
|
|
|
|
27
|
|
|
$this->makeBladeDirective(); |
|
28
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Register the application services. |
|
33
|
|
|
* |
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
|
|
public function register() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/url-aliases.php', 'url-aliases'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function publishConfig() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->publishes([ |
|
44
|
|
|
__DIR__ . '/../config/url-aliases.php' => config_path('url-aliases.php') |
|
|
|
|
|
|
45
|
|
|
], 'url-aliases-config'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function publishSeeder() |
|
49
|
|
|
{ |
|
50
|
|
|
$seedPath = __DIR__ . '/../database/seeds/UrlAliasesTableSeeder.php.stub'; |
|
51
|
|
|
$this->publishes([ |
|
52
|
|
|
$seedPath => database_path('seeds/UrlAliasesTableSeeder.php') |
|
|
|
|
|
|
53
|
|
|
], 'url-aliases-seeder'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function publishMigrations() |
|
57
|
|
|
{ |
|
58
|
|
|
if (! class_exists('CreateUrlAliasesTable')) { |
|
59
|
|
|
$timestamp = date('Y_m_d_His', time()); |
|
60
|
|
|
|
|
61
|
|
|
$migrationPath = __DIR__.'/../database/migrations/create_url_aliases_table.php'; |
|
62
|
|
|
$this->publishes([$migrationPath => database_path('/migrations/' . $timestamp . '_create_url_aliases_table.php'), |
|
|
|
|
|
|
63
|
|
|
], 'url-aliases-migrations'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function registerMiddleware($middleware) |
|
68
|
|
|
{ |
|
69
|
|
|
$kernel = $this->app[Kernel::class]; |
|
70
|
|
|
$kernel->pushMiddleware($middleware); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Make blade directive "@urlAliasCurrent()" |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function makeBladeDirective() |
|
77
|
|
|
{ |
|
78
|
|
|
\Blade::directive('urlAliasCurrent', function ($expression) { |
|
79
|
|
|
|
|
80
|
|
|
list($absolute) = explode(', ', $expression); |
|
81
|
|
|
|
|
82
|
|
|
$path = request()->server('ALIAS_REQUEST_URI', request()->path()); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if (($absolute || $absolute === '') && $absolute != 'false') { |
|
85
|
|
|
$path = url($path); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return "<?php echo('{$path}'); ?>"; |
|
89
|
|
|
}); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function checkMakeDir(string $path) |
|
93
|
|
|
{ |
|
94
|
|
|
if (!is_dir($path)) { |
|
95
|
|
|
mkdir($path, 0755, true); |
|
96
|
|
|
} |
|
97
|
|
|
return $path; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|