1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fouladgar\MobileVerification; |
4
|
|
|
|
5
|
|
|
use Fouladgar\MobileVerification\Contracts\SMSClient; |
6
|
|
|
use Fouladgar\MobileVerification\Exceptions\SMSClientNotFoundException; |
7
|
|
|
use Fouladgar\MobileVerification\Http\Middleware\EnsureMobileIsVerified; |
8
|
|
|
use Fouladgar\MobileVerification\Tokens\DatabaseTokenRepository; |
9
|
|
|
use Fouladgar\MobileVerification\Tokens\TokenBroker; |
10
|
|
|
use Fouladgar\MobileVerification\Tokens\TokenBrokerInterface; |
11
|
|
|
use Fouladgar\MobileVerification\Tokens\TokenRepositoryInterface; |
12
|
|
|
use Illuminate\Database\ConnectionInterface; |
13
|
|
|
use Illuminate\Routing\Router; |
14
|
|
|
use Illuminate\Support\Facades\Route; |
15
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
16
|
|
|
use Throwable; |
17
|
|
|
|
18
|
|
|
class ServiceProvider extends BaseServiceProvider |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Perform post-registration booting of services. |
22
|
|
|
* |
23
|
|
|
* @param Router $router |
24
|
|
|
*/ |
25
|
|
|
public function boot(Router $router): void |
26
|
|
|
{ |
27
|
|
|
$this->registerRoutes(); |
28
|
|
|
|
29
|
|
|
$this->loadAssetsFrom(); |
30
|
|
|
|
31
|
|
|
$this->registerPublishing(); |
32
|
|
|
|
33
|
|
|
$router->aliasMiddleware('mobile.verified', EnsureMobileIsVerified::class); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Register any package services. |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function register(): void |
42
|
|
|
{ |
43
|
|
|
$this->app->register(EventServiceProvider::class); |
44
|
|
|
|
45
|
|
|
$this->mergeConfigFrom($this->getConfig(), 'mobile_verifier'); |
46
|
|
|
|
47
|
|
|
$this->registerBindings(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Register the package routes. |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
protected function registerRoutes(): void |
56
|
|
|
{ |
57
|
|
|
Route::group($this->routeConfiguration(), function () { |
58
|
|
|
$this->loadRoutesFrom(__DIR__.'/Http/routes.php'); |
59
|
|
|
}); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Load and register package assets. |
64
|
|
|
*/ |
65
|
|
|
protected function loadAssetsFrom(): void |
66
|
|
|
{ |
67
|
|
|
$this->loadMigrationsFrom(__DIR__.'/../database/migrations'); |
68
|
|
|
|
69
|
|
|
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'MobileVerification'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Register the package's publishable resources. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
protected function registerPublishing(): void |
78
|
|
|
{ |
79
|
|
|
$this->publishes([$this->getConfig() => config_path('mobile_verifier.php')], 'config'); |
80
|
|
|
|
81
|
|
|
$this->publishes([ |
82
|
|
|
__DIR__.'/../resources/lang' => resource_path('lang/vendor/MobileVerification'), |
83
|
|
|
], 'lang'); |
84
|
|
|
|
85
|
|
|
$this->publishes([__DIR__.'/../database/migrations' => database_path('migrations')], 'migrations'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Register any package bindings. |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
protected function registerBindings(): void |
94
|
|
|
{ |
95
|
|
|
$this->app->singleton(SMSClient::class, static function ($app) { |
96
|
|
|
try { |
97
|
|
|
return $app->make(config('mobile_verifier.sms_client')); |
98
|
|
|
} catch (Throwable $e) { |
99
|
|
|
throw new SMSClientNotFoundException(); |
100
|
|
|
} |
101
|
|
|
}); |
102
|
|
|
|
103
|
|
|
$this->app->bind(TokenRepositoryInterface::class, static function ($app) { |
104
|
|
|
return new DatabaseTokenRepository( |
105
|
|
|
$app->make(ConnectionInterface::class), |
106
|
|
|
config('mobile_verifier.token_table', 'mobile_verification_tokens'), |
107
|
|
|
config('mobile_verifier.token_lifetime', 5) |
108
|
|
|
); |
109
|
|
|
}); |
110
|
|
|
|
111
|
|
|
$this->app->bind(TokenBrokerInterface::class, TokenBroker::class); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the config file path. |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
protected function getConfig(): string |
120
|
|
|
{ |
121
|
|
|
return __DIR__.'/../config/config.php'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the Telescope route group configuration array. |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
private function routeConfiguration(): array |
130
|
|
|
{ |
131
|
|
|
return [ |
132
|
|
|
'namespace' => config('mobile_verifier.controller_namespace', 'Fouladgar\MobileVerification\Http\Controllers'), |
133
|
|
|
'prefix' => config('mobile_verifier.routes_prefix', 'auth'), |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|