|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Strategies\Metadata; |
|
4
|
|
|
|
|
5
|
|
|
use Laravel\Passport\Passport; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use ReflectionMethod; |
|
8
|
|
|
use Illuminate\Routing\Route; |
|
9
|
|
|
use Mpociot\Reflection\DocBlock; |
|
10
|
|
|
use Mpociot\Reflection\DocBlock\Tag; |
|
11
|
|
|
use Mpociot\ApiDoc\Strategies\Strategy; |
|
12
|
|
|
use Mpociot\ApiDoc\Tools\RouteDocBlocker; |
|
13
|
|
|
|
|
14
|
|
|
class GetFromMiddleware extends Strategy |
|
15
|
|
|
{ |
|
16
|
|
|
public function __invoke(Route $route, ReflectionClass $controller, ReflectionMethod $method, array $routeRules, array $context = []) |
|
17
|
|
|
{ |
|
18
|
|
|
return [ |
|
19
|
|
|
'scopes' => $this->getScopesInfo($route), |
|
20
|
|
|
'headers' => $this->getHeaders($route) |
|
21
|
|
|
]; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
protected function getScopesInfo($route) |
|
26
|
|
|
{ |
|
27
|
|
|
$middlewares = $route->gatherMiddleware(); |
|
28
|
|
|
$anyScopeMiddleware = collect($middlewares)->first(function($item) { |
|
29
|
|
|
return preg_match('/^scope:/', $item); |
|
30
|
|
|
}); |
|
31
|
|
View Code Duplication |
if ($anyScopeMiddleware) { |
|
|
|
|
|
|
32
|
|
|
$scopes = explode(',', preg_replace('/^scope:/', '', $anyScopeMiddleware)); |
|
33
|
|
|
$scopesInfo = Passport::scopes()->filter(function($item) use($scopes) { |
|
34
|
|
|
return in_array($item->id, $scopes); |
|
35
|
|
|
})->values(); |
|
36
|
|
|
return [ |
|
37
|
|
|
'type' => 'any', |
|
38
|
|
|
'scopes' => $scopesInfo |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
$allScopeMiddleware = collect($middlewares)->first(function($item) { |
|
42
|
|
|
return preg_match('/^scopes:/', $item); |
|
43
|
|
|
}); |
|
44
|
|
View Code Duplication |
if ($allScopeMiddleware) { |
|
|
|
|
|
|
45
|
|
|
$scopes = explode(',', preg_replace('/^scopes:/', '', $allScopeMiddleware)); |
|
46
|
|
|
$scopesInfo = Passport::scopes()->filter(function($item) use($scopes) { |
|
47
|
|
|
return in_array($item->id, $scopes); |
|
48
|
|
|
})->values(); |
|
49
|
|
|
return [ |
|
50
|
|
|
'type' => 'all', |
|
51
|
|
|
'scopes' => $scopesInfo |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function getHeaders($route) |
|
58
|
|
|
{ |
|
59
|
|
|
$middlewares = $route->gatherMiddleware(); |
|
60
|
|
|
$authMiddleware = collect($middlewares)->first(function($item) { |
|
61
|
|
|
return preg_match('/^auth:/', $item); |
|
62
|
|
|
}); |
|
63
|
|
|
$headers = []; |
|
64
|
|
|
if ($authMiddleware) { |
|
65
|
|
|
$guard = preg_replace('/^auth:/', '', $authMiddleware); |
|
66
|
|
|
$driver = config('auth.guards.'.$guard.'.driver'); |
|
67
|
|
|
if ($driver === 'token') { |
|
68
|
|
|
$headers['Authorization'] = 'Token {token}'; |
|
69
|
|
|
} else if ($driver === 'passport' || $driver === 'jwt') { |
|
70
|
|
|
$headers['Authorization'] = 'Bearer {access_token}'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $headers; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.