Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
88.89% |
8 / 9 |
CRAP | |
94.55% |
52 / 55 |
RouteTrait | |
0.00% |
0 / 1 |
|
88.89% |
8 / 9 |
19.06 | |
94.55% |
52 / 55 |
__construct | |
100.00% |
1 / 1 |
3 | |
100.00% |
9 / 9 |
|||
add | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
specific | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
|||
single | |
100.00% |
1 / 1 |
2 | |
100.00% |
9 / 9 |
|||
resource | |
100.00% |
1 / 1 |
3 | |
100.00% |
7 / 7 |
|||
params | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
noPermission | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
noPermissionArray | |
0.00% |
0 / 1 |
6.14 | |
84.21% |
16 / 19 |
|||
format | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
1 | <?php |
2 | |
3 | |
4 | namespace Qmp\Laravel\ApiGateway\Services\Route; |
5 | |
6 | use Illuminate\Support\Collection; |
7 | |
8 | trait RouteTrait |
9 | { |
10 | protected $options = []; |
11 | |
12 | public function __construct() |
13 | { |
14 | $this->options = new \stdClass(); |
15 | |
16 | Collection::macro('recursive', function () { |
17 | return $this->map(function ($value) { |
18 | if (is_array($value) || is_object($value)) { |
19 | return (new Collection($value))->recursive(); |
20 | } |
21 | |
22 | return $value; |
23 | }); |
24 | }); |
25 | } |
26 | |
27 | protected function add($key, $value) |
28 | { |
29 | $this->options->$key = $value; |
30 | |
31 | return $this; |
32 | } |
33 | |
34 | public function specific($method, $path, $action, $name = null, $private = true, array $middleware = []) |
35 | { |
36 | return $this->add('path', $path) |
37 | ->add('method', $method) |
38 | ->add('action', $action) |
39 | ->add('name', $name) |
40 | ->add('private', $private) |
41 | ->add('middleware', $middleware); |
42 | } |
43 | |
44 | public function single($method, $path, $name, $resourceName = null, $externalRoute = false, $private = true, array $middleware = []) |
45 | { |
46 | if (!$resourceName) { |
47 | $resourceName = $path; |
48 | } |
49 | |
50 | return $this->add('path', $path) |
51 | ->add('method', $method) |
52 | ->add('name', $name) |
53 | ->add('private', $private) |
54 | ->add('middleware', $middleware) |
55 | ->add('resource_name', $resourceName) |
56 | ->add('external_route', $externalRoute); |
57 | } |
58 | |
59 | public function resource($resource, array $externalRoutes = [], $name = null, $controller = 'GatewayController') |
60 | { |
61 | if (!$name) { |
62 | $name = $resource; |
63 | } |
64 | |
65 | if (!empty($externalRoutes)) { |
66 | $this->add('external_routes', $externalRoutes); |
67 | } |
68 | |
69 | return $this->add('resource_name', $name) |
70 | ->add('resource_api', $resource) |
71 | ->add('controller', $controller); |
72 | } |
73 | |
74 | public function params(array $array) |
75 | { |
76 | return $this->add('parameters', $array); |
77 | } |
78 | |
79 | public function noPermission() |
80 | { |
81 | return $this->add('no_permission', true); |
82 | } |
83 | |
84 | public function noPermissionArray($routes) |
85 | { |
86 | |
87 | |
88 | $return = []; |
89 | $routes = collect($routes)->recursive(); |
90 | |
91 | $routes->each(function ($route) use (&$return) { |
92 | $service = $route->get('name'); |
93 | $route->get('resources')->each(function ($resources) use ($service, &$return) { |
94 | if ($resources->has('routes')) { |
95 | $resources->get('routes')->each(function ($route) use ($service, &$return) { |
96 | if ($route->has('no_permission') && $route->get('no_permission') === true) { |
97 | $return[] = $route->get('name'); |
98 | } |
99 | }); |
100 | } else { |
101 | $name = $resources->get('resource_name'); |
102 | |
103 | $methods = collect(['index', 'store', 'show', 'update', 'destroy']); |
104 | if ($resources->has('no_permission') && $resources->get('no_permission') === true) { |
105 | $methods->each(function ($method) use ($service, $name, &$return) { |
106 | $return[] = collect([$service, $name, $method])->filter()->join('.'); |
107 | }); |
108 | } |
109 | } |
110 | }); |
111 | }); |
112 | |
113 | return $return; |
114 | } |
115 | |
116 | public function format() |
117 | { |
118 | return json_decode(json_encode($this->options), true); |
119 | } |
120 | } |