Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
33.33% |
2 / 6 |
CRAP | |
64.52% |
20 / 31 |
| RouteServiceProvider | |
0.00% |
0 / 1 |
|
33.33% |
2 / 6 |
10.86 | |
64.52% |
20 / 31 |
| boot | |
0.00% |
0 / 1 |
2.01 | |
85.71% |
6 / 7 |
|||
| map | |
0.00% |
0 / 1 |
2.02 | |
83.33% |
5 / 6 |
|||
| mapServiceRoutes | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 7 |
|||
| getControllerNameSpace | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| mapApiRoutes | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
| mapWebRoutes | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\MicroService\Providers; |
| 4 | |
| 5 | use Illuminate\Support\Facades\Route; |
| 6 | use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; |
| 7 | |
| 8 | use Illuminate\Support\Facades\Log; |
| 9 | use Illuminate\Support\Str; |
| 10 | |
| 11 | use Illuminate\Http\Request; |
| 12 | use Illuminate\Support\Facades\RateLimiter; |
| 13 | use Illuminate\Cache\RateLimiting\Limit; |
| 14 | |
| 15 | class RouteServiceProvider extends ServiceProvider |
| 16 | { |
| 17 | /** |
| 18 | * This namespace is applied to your controller routes. |
| 19 | * |
| 20 | * In addition, it is set as the URL generator's root namespace. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $namespace = 'App\Http\Controllers'; |
| 25 | |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * Boot the service provider. |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public function boot() |
| 34 | { |
| 35 | RateLimiter::for('api', function (Request $request) { |
| 36 | return Limit::perMinute(env('RATE_LIMIT', 1000))->by(optional($request->user())->id ?: $request->ip()); |
| 37 | }); |
| 38 | |
| 39 | |
| 40 | $path = realpath(__DIR__.'/../../config/micro-services.php'); |
| 41 | $this->mergeConfigFrom($path, 'micro-services'); |
| 42 | |
| 43 | parent::boot(); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * Define the routes for the application. |
| 49 | * |
| 50 | * @return void |
| 51 | */ |
| 52 | public function map() |
| 53 | { |
| 54 | $serviceName = config('micro-services.name'); |
| 55 | if (!empty($serviceName)) { |
| 56 | $this->mapServiceRoutes($serviceName); |
| 57 | } else { |
| 58 | $this->mapApiRoutes(); |
| 59 | $this->mapWebRoutes(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Define the Service routes |
| 65 | * |
| 66 | * @param string $serviceName |
| 67 | */ |
| 68 | protected function mapServiceRoutes(string $serviceName) |
| 69 | { |
| 70 | $namespace = $this->getControllerNameSpace($serviceName); |
| 71 | $group = 'vendor/qwamplify/' . $serviceName . '/src/routes/api.php'; |
| 72 | |
| 73 | Route::prefix('api') |
| 74 | ->middleware('api') |
| 75 | ->namespace($namespace) |
| 76 | ->group(base_path($group)); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Define Namespace to use with controllers |
| 81 | * |
| 82 | * @param string $serviceName |
| 83 | * @return string |
| 84 | */ |
| 85 | protected function getControllerNameSpace(string $serviceName) |
| 86 | { |
| 87 | $namespaceService = str_replace(['service-', '-laravel'], ['', ''], $serviceName); |
| 88 | return 'Qmp\\Laravel\\' . Str::studly($namespaceService) . '\\Controllers'; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Define the "api" routes for the application. |
| 93 | * |
| 94 | * These routes are typically stateless. |
| 95 | * |
| 96 | * @return void |
| 97 | */ |
| 98 | protected function mapApiRoutes() |
| 99 | { |
| 100 | Route::prefix('api') |
| 101 | ->middleware('api') |
| 102 | ->namespace($this->namespace) |
| 103 | ->group(base_path('routes/api.php')); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Define the "web" routes for the application. |
| 108 | * |
| 109 | * These routes all receive session state, CSRF protection, etc. |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | protected function mapWebRoutes() |
| 114 | { |
| 115 | Route::middleware('web') |
| 116 | ->namespace($this->namespace) |
| 117 | ->group(base_path('routes/web.php')); |
| 118 | } |
| 119 | |
| 120 | } |