1 | <?php |
||||||
2 | |||||||
3 | namespace Yansongda\LaravelApi; |
||||||
4 | |||||||
5 | use Illuminate\Support\Facades\Auth; |
||||||
6 | use Illuminate\Support\ServiceProvider; |
||||||
7 | use Yansongda\LaravelApi\Guards\TokenGuard; |
||||||
8 | |||||||
9 | class ApiServiceProvider extends ServiceProvider |
||||||
10 | { |
||||||
11 | /** |
||||||
12 | * Indicates if loading of the provider is deferred. |
||||||
13 | * |
||||||
14 | * @var bool |
||||||
15 | */ |
||||||
16 | protected $defer = false; |
||||||
17 | |||||||
18 | /** |
||||||
19 | * Bootstrap. |
||||||
20 | * |
||||||
21 | * @author yansongda <[email protected]> |
||||||
22 | * |
||||||
23 | * @return void |
||||||
24 | */ |
||||||
25 | public function boot() |
||||||
26 | { |
||||||
27 | $this->loadResources(); |
||||||
28 | |||||||
29 | $this->publishResources(); |
||||||
30 | } |
||||||
31 | |||||||
32 | /** |
||||||
33 | * Register the service. |
||||||
34 | * |
||||||
35 | * @author yansongda <[email protected]> |
||||||
36 | * |
||||||
37 | * @return void |
||||||
38 | */ |
||||||
39 | public function register() |
||||||
40 | { |
||||||
41 | $this->registerGuard(); |
||||||
42 | |||||||
43 | $this->detectUserProvider(); |
||||||
44 | } |
||||||
45 | |||||||
46 | /** |
||||||
47 | * Load resources. |
||||||
48 | * |
||||||
49 | * @author yansongda <[email protected]> |
||||||
50 | * |
||||||
51 | * @return void |
||||||
52 | */ |
||||||
53 | protected function loadResources() |
||||||
54 | { |
||||||
55 | $this->loadMigrationsFrom(dirname(__DIR__) . '/database/migrations'); |
||||||
56 | |||||||
57 | if (Api::$enableRoute) { |
||||||
58 | $this->loadRoutesFrom(dirname(__DIR__) . '/routes/api.php'); |
||||||
59 | } |
||||||
60 | } |
||||||
61 | |||||||
62 | /** |
||||||
63 | * Publish resources. |
||||||
64 | * |
||||||
65 | * @author yansongda <[email protected]> |
||||||
66 | * |
||||||
67 | * @return void |
||||||
68 | */ |
||||||
69 | protected function publishResources() |
||||||
70 | { |
||||||
71 | $this->publishes([ |
||||||
72 | dirname(__DIR__) . '/database/migrations' => database_path('migrations') |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
73 | ], 'laravel-api-migrations'); |
||||||
74 | } |
||||||
75 | |||||||
76 | /** |
||||||
77 | * Register guard. |
||||||
78 | * |
||||||
79 | * @author yansongda <[email protected]> |
||||||
80 | * |
||||||
81 | * @return void |
||||||
82 | */ |
||||||
83 | protected function registerGuard() |
||||||
84 | { |
||||||
85 | Auth::extend('api', function ($app, $name, array $config) { |
||||||
0 ignored issues
–
show
The parameter
$config is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
The parameter
$name is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||||
86 | $guard = new TokenGuard($app['request']); |
||||||
87 | |||||||
88 | $user = $guard->user(); |
||||||
89 | |||||||
90 | $app['request']['user'] = $user->{$user->getKeyName()}; |
||||||
0 ignored issues
–
show
The method
getKeyName() does not exist on Illuminate\Contracts\Auth\Authenticatable .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||||||
91 | $app['request']['app'] = $guard->app()->app_id; |
||||||
92 | |||||||
93 | return $guard; |
||||||
94 | }); |
||||||
95 | } |
||||||
96 | |||||||
97 | /** |
||||||
98 | * Detect user provider. |
||||||
99 | * |
||||||
100 | * @author yansongda <[email protected]> |
||||||
101 | * |
||||||
102 | * @return void |
||||||
103 | */ |
||||||
104 | protected function detectUserProvider() |
||||||
105 | { |
||||||
106 | $provider = config('auth.guards.api.provider'); |
||||||
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
107 | |||||||
108 | Api::$user = config('auth.providers.' . $provider . '.model'); |
||||||
109 | } |
||||||
110 | } |
||||||
111 |