|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Neurony\Redirects; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
6
|
|
|
use Illuminate\Routing\Router; |
|
7
|
|
|
use Illuminate\Support\Facades\File; |
|
8
|
|
|
use Illuminate\Support\Facades\Route; |
|
9
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
|
10
|
|
|
use Neurony\Redirects\Contracts\RedirectModelContract; |
|
11
|
|
|
use Neurony\Redirects\Middleware\RedirectRequests; |
|
12
|
|
|
use Neurony\Redirects\Models\Redirect; |
|
13
|
|
|
|
|
14
|
|
|
class ServiceProvider extends BaseServiceProvider |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Router |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $router; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create a new service provider instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @param Application $app |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(Application $app) |
|
27
|
|
|
{ |
|
28
|
|
|
parent::__construct($app); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Bootstrap the application services. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Router $router |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function boot(Router $router) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->router = $router; |
|
40
|
|
|
|
|
41
|
|
|
$this->publishConfigs(); |
|
42
|
|
|
$this->publishMigrations(); |
|
43
|
|
|
$this->registerMiddleware(); |
|
44
|
|
|
$this->registerRouteBindings(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Register the application services. |
|
49
|
|
|
* |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
public function register() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->registerBindings(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function publishConfigs() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->publishes([ |
|
63
|
|
|
__DIR__.'/../config/redirects.php' => config_path('redirects.php'), |
|
64
|
|
|
], 'config'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return void |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function publishMigrations() |
|
71
|
|
|
{ |
|
72
|
|
|
if (empty(File::glob(database_path('migrations/*_create_redirects_table.php')))) { |
|
73
|
|
|
$timestamp = date('Y_m_d_His', time()); |
|
74
|
|
|
$migration = database_path("migrations/{$timestamp}_create_redirects_table.php"); |
|
75
|
|
|
|
|
76
|
|
|
$this->publishes([ |
|
77
|
|
|
__DIR__.'/../database/migrations/create_redirects_table.php.stub' => $migration, |
|
78
|
|
|
], 'migrations'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function registerMiddleware() |
|
86
|
|
|
{ |
|
87
|
|
|
$this->router->aliasMiddleware('redirect.requests', RedirectRequests::class); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return void |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function registerRouteBindings() |
|
94
|
|
|
{ |
|
95
|
|
|
Route::model('redirect', RedirectModelContract::class); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function registerBindings() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->app->bind(RedirectModelContract::class, $this->config['redirects']['redirect_model'] ?? Redirect::class); |
|
|
|
|
|
|
104
|
|
|
$this->app->alias(RedirectModelContract::class, 'redirect.model'); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: