1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraCrafts\GeoRoutes; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Http\Kernel; |
6
|
|
|
use Illuminate\Routing\Route; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use LaraCrafts\GeoRoutes\Http\Middleware\GeoMiddleware; |
9
|
|
|
use LaraCrafts\GeoRoutes\Http\Middleware\GeoRoutesMiddleware; |
10
|
|
|
|
11
|
|
|
class GeoRoutesServiceProvider extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Bootstrap any package services. |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
100 |
|
public function boot() |
19
|
|
|
{ |
20
|
100 |
|
$this->mergeConfigFrom(__DIR__ . '/../config/global.php', 'geo-routes.global'); |
21
|
100 |
|
$this->mergeConfigFrom(__DIR__ . '/../config/routes.php', 'geo-routes.routes'); |
22
|
100 |
|
$this->publishes([__DIR__ . '/../config' => config_path('geo-routes')], 'config'); |
23
|
|
|
|
24
|
100 |
|
$this->registerMacros(); |
25
|
100 |
|
$this->registerGlobalMiddleware(); |
26
|
100 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Register any package services. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
100 |
|
public function register() |
34
|
|
|
{ |
35
|
100 |
|
$router = $this->app->make('router'); |
36
|
|
|
|
37
|
100 |
|
if (method_exists($router, 'aliasMiddleware')) { |
38
|
100 |
|
$router->aliasMiddleware('geo', GeoRoutesMiddleware::class); |
39
|
|
|
} else { |
40
|
|
|
$router->middleware('geo', GeoRoutesMiddleware::class); |
41
|
|
|
} |
42
|
100 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Register the global middleware. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
100 |
|
protected function registerGlobalMiddleware() |
50
|
|
|
{ |
51
|
100 |
|
if (!$this->app['config']['geo-routes.global.enabled']) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
100 |
|
$this->app->make(Kernel::class)->pushMiddleware(GeoMiddleware::class); |
56
|
100 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Register the route macros. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
60 |
|
protected function registerMacros() |
64
|
|
|
{ |
65
|
40 |
|
Route::macro('allowFrom', function (string ...$countries) { |
66
|
10 |
|
return new GeoRoute($this, $countries, 'allow'); |
|
|
|
|
67
|
100 |
|
}); |
68
|
|
|
|
69
|
40 |
|
Route::macro('denyFrom', function (string ...$countries) { |
70
|
10 |
|
return new GeoRoute($this, $countries, 'deny'); |
|
|
|
|
71
|
100 |
|
}); |
72
|
|
|
|
73
|
40 |
|
Route::macro('from', function (string ...$countries) { |
74
|
10 |
|
return new GeoRoute($this, $countries, 'allow'); |
|
|
|
|
75
|
100 |
|
}); |
76
|
100 |
|
} |
77
|
|
|
} |
78
|
|
|
|