Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
55 / 55 |
| RouteRegistry | |
100.00% |
1 / 1 |
|
100.00% |
8 / 8 |
18 | |
100.00% |
55 / 55 |
| bind | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| getServices | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getLatestVersion | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| create | |
100.00% |
1 / 1 |
2 | |
100.00% |
7 / 7 |
|||
| includeIsolates | |
100.00% |
1 / 1 |
4 | |
100.00% |
14 / 14 |
|||
| includeResource | |
100.00% |
1 / 1 |
3 | |
100.00% |
9 / 9 |
|||
| createRoute | |
100.00% |
1 / 1 |
4 | |
100.00% |
9 / 9 |
|||
| createResource | |
100.00% |
1 / 1 |
1 | |
100.00% |
8 / 8 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\ApiGateway\Services\Route; |
| 4 | |
| 5 | use Illuminate\Support\Facades\Redis; |
| 6 | use \Qmp\Laravel\AuthConnector\Middleware\AuthConnector; |
| 7 | |
| 8 | class RouteRegistry |
| 9 | { |
| 10 | protected $latest = null; |
| 11 | |
| 12 | protected $servicePath = ''; |
| 13 | |
| 14 | protected $serviceName = ''; |
| 15 | |
| 16 | public function bind() { |
| 17 | $this->getServices()->each(function ($service) { |
| 18 | $this->create($service); |
| 19 | }); |
| 20 | } |
| 21 | |
| 22 | protected function getServices() { |
| 23 | return collect(config('api_gateway.routes')); |
| 24 | } |
| 25 | |
| 26 | protected function getLatestVersion() { |
| 27 | if (!$this->latest) { |
| 28 | $this->latest = config('api_gateway.latest'); |
| 29 | } |
| 30 | return $this->latest; |
| 31 | } |
| 32 | |
| 33 | protected function create($service) { |
| 34 | $this->servicePath = $service['path'] ?? ''; |
| 35 | $this->serviceName = $service['name'] ?? ''; |
| 36 | $prefix = $service['version'] ?? ''; |
| 37 | |
| 38 | foreach ($service['resources'] as $resource) { |
| 39 | $this->includeIsolates($resource, $prefix); |
| 40 | $this->includeResource($resource, $prefix); |
| 41 | } |
| 42 | |
| 43 | } |
| 44 | |
| 45 | protected function includeIsolates($resource, $prefix) |
| 46 | { |
| 47 | if (isset($resource['routes'])) { |
| 48 | foreach ($resource['routes'] as $route) { |
| 49 | $routePath = $route['path'] ?? ''; |
| 50 | $method = strtolower($route['method'] ?? 'get'); |
| 51 | $action = $route['action'] ?? 'GatewayController@requestApi'; |
| 52 | $name = $route['name'] ?? false; |
| 53 | $private = $route['private'] ?? true; |
| 54 | $middleware = $route['midleware'] ?? []; |
| 55 | |
| 56 | $route = $prefix . $this->servicePath . $routePath; |
| 57 | $this->createRoute($prefix, $route, $method, $action, $name, $private, $middleware); |
| 58 | if ($prefix === $this->getLatestVersion()) { |
| 59 | $routeLatest = 'latest' . $this->servicePath . $routePath; |
| 60 | $this->createRoute('latest', $routeLatest, $method, $action, $name, $private, $middleware); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | protected function includeResource($resource, $prefix) |
| 67 | { |
| 68 | $resourceApi = $resource['resource_api'] ?? false; |
| 69 | if ($resourceApi) { |
| 70 | $middleware = $resource['middleware'] ?? []; |
| 71 | $middleware[] = AuthConnector::class; |
| 72 | $controller = $resource['controller'] ?? 'GatewayController'; |
| 73 | |
| 74 | $this->createResource($prefix, $middleware, $resourceApi, $controller); |
| 75 | |
| 76 | if ($prefix === $this->getLatestVersion()) { |
| 77 | $this->createResource('latest', $middleware, $resourceApi, $controller); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | protected function createRoute($prefix, $route, $method, $action, $name, $private, Array $middleware) |
| 83 | { |
| 84 | $routeApp = \Route::$method($route, $action); |
| 85 | |
| 86 | if ($name) { |
| 87 | $name = collect([$prefix, $name])->filter()->join('.'); |
| 88 | $routeApp->name($name); |
| 89 | } |
| 90 | |
| 91 | if ($private) { |
| 92 | $middleware[] = AuthConnector::class; |
| 93 | } |
| 94 | |
| 95 | if (count($middleware) >= 1) { |
| 96 | $routeApp->middleware(...$middleware); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | protected function createResource($prefix, $middleware, $resourceApi, $controller = 'GatewayController') |
| 101 | { |
| 102 | \Route::group([ |
| 103 | 'prefix' => $prefix . $this->servicePath, |
| 104 | 'middleware' => $middleware |
| 105 | ], function () use($prefix, $resourceApi, $controller) { |
| 106 | $as =collect([$prefix, $this->serviceName])->filter()->join('.') ; |
| 107 | \Route::apiResource($resourceApi, $controller, ['as' => $as]); |
| 108 | }); |
| 109 | } |
| 110 | } |