1 | <?php |
||||
2 | |||||
3 | namespace LaraCrafts\GeoRoutes; |
||||
4 | |||||
5 | use Closure; |
||||
6 | use Illuminate\Contracts\Http\Kernel; |
||||
7 | use Illuminate\Routing\Route; |
||||
8 | use Illuminate\Routing\Router; |
||||
9 | use Illuminate\Support\ServiceProvider; |
||||
10 | use LaraCrafts\GeoRoutes\Console\Commands\RouteListCommand; |
||||
11 | use LaraCrafts\GeoRoutes\Http\Middleware\GeoMiddleware; |
||||
12 | use LaraCrafts\GeoRoutes\Http\Middleware\GeoRoutesMiddleware; |
||||
13 | |||||
14 | class GeoRoutesServiceProvider extends ServiceProvider |
||||
15 | { |
||||
16 | /** |
||||
17 | * Bootstrap any package services. |
||||
18 | * |
||||
19 | * @return void |
||||
20 | */ |
||||
21 | 480 | public function boot() |
|||
22 | { |
||||
23 | 480 | $this->mergeConfigFrom(__DIR__ . '/../config/global.php', 'geo-routes.global'); |
|||
24 | 480 | $this->mergeConfigFrom(__DIR__ . '/../config/routes.php', 'geo-routes.routes'); |
|||
25 | 480 | $this->publishes([__DIR__ . '/../config' => config_path('geo-routes')], 'config'); |
|||
26 | 480 | $this->registerMacros(); |
|||
27 | 480 | $this->registerGlobalMiddleware(); |
|||
28 | 120 | $this->app->extend('command.route.list', function ($command, $app) { |
|||
29 | return new RouteListCommand($app['router']); |
||||
30 | 480 | }); |
|||
31 | 480 | } |
|||
32 | |||||
33 | /** |
||||
34 | * Register any package services. |
||||
35 | * |
||||
36 | * @return void |
||||
37 | */ |
||||
38 | 480 | public function register() |
|||
39 | { |
||||
40 | 480 | $router = $this->app->make('router'); |
|||
41 | |||||
42 | 120 | $this->app->singleton('georoutes.callbacks', function () { |
|||
43 | 16 | $registrar = new CallbackRegistrar(); |
|||
44 | 16 | $registrar->loadCallbacks(config('geo-routes.routes.callbacks')); |
|||
45 | |||||
46 | 16 | return $registrar; |
|||
47 | 480 | }); |
|||
48 | |||||
49 | 480 | if (method_exists($router, 'aliasMiddleware')) { |
|||
50 | 480 | $router->aliasMiddleware('geo', GeoRoutesMiddleware::class); |
|||
51 | } else { |
||||
52 | $router->middleware('geo', GeoRoutesMiddleware::class); |
||||
53 | } |
||||
54 | 480 | } |
|||
55 | |||||
56 | /** |
||||
57 | * Register the global middleware. |
||||
58 | * |
||||
59 | * @return void |
||||
60 | */ |
||||
61 | 480 | protected function registerGlobalMiddleware() |
|||
62 | { |
||||
63 | 480 | if (!$this->app['config']['geo-routes.global.enabled']) { |
|||
64 | return; |
||||
65 | } |
||||
66 | |||||
67 | 480 | $this->app->make(Kernel::class)->pushMiddleware(GeoMiddleware::class); |
|||
68 | 480 | } |
|||
69 | |||||
70 | /** |
||||
71 | * Register the route macros. |
||||
72 | * |
||||
73 | * @return void |
||||
74 | */ |
||||
75 | 360 | protected function registerMacros() |
|||
76 | { |
||||
77 | 120 | Route::macro('allowFrom', function (string ...$countries) { |
|||
78 | 16 | return new GeoRoute($this, $countries, 'allow'); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
79 | 480 | }); |
|||
80 | |||||
81 | 120 | Route::macro('denyFrom', function (string ...$countries) { |
|||
82 | 16 | return new GeoRoute($this, $countries, 'deny'); |
|||
0 ignored issues
–
show
$this of type LaraCrafts\GeoRoutes\GeoRoutesServiceProvider is incompatible with the type Illuminate\Routing\Route expected by parameter $route of LaraCrafts\GeoRoutes\GeoRoute::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
83 | 480 | }); |
|||
84 | |||||
85 | 120 | Route::macro('from', function (string ...$countries) { |
|||
86 | 16 | return new GeoRoute($this, $countries, 'allow'); |
|||
0 ignored issues
–
show
$this of type LaraCrafts\GeoRoutes\GeoRoutesServiceProvider is incompatible with the type Illuminate\Routing\Route expected by parameter $route of LaraCrafts\GeoRoutes\GeoRoute::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
87 | 480 | }); |
|||
88 | |||||
89 | 120 | Route::macro('geo', function (array $attributes, Closure $routes) { |
|||
90 | return new GeoGroup($attributes, $routes); |
||||
91 | 480 | }); |
|||
92 | |||||
93 | 120 | Router::macro('geo', function (array $attributes, Closure $routes) { |
|||
94 | 16 | return new GeoGroup($attributes, $routes); |
|||
95 | 480 | }); |
|||
96 | 480 | } |
|||
97 | } |
||||
98 |