|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Api; |
|
4
|
|
|
|
|
5
|
|
|
use ElfSundae\Laravel\Api\Client; |
|
6
|
|
|
use Illuminate\Support\ServiceProvider; |
|
7
|
|
|
use Illuminate\Contracts\Encryption\Encrypter; |
|
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 = true; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Register the service provider. |
|
20
|
|
|
* |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
|
|
public function register() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/api.php', 'api'); |
|
26
|
|
|
|
|
27
|
|
|
$this->registerClient(); |
|
28
|
|
|
|
|
29
|
|
|
$this->registerToken(); |
|
30
|
|
|
|
|
31
|
|
|
if ($this->app->runningInConsole()) { |
|
32
|
|
|
$this->registerForConsole(); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Register the Client singleton. |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function registerClient() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->app->singleton('api.client', function ($app) { |
|
44
|
|
|
$config = $app->make('config'); |
|
45
|
|
|
|
|
46
|
|
|
$client = new Client( |
|
47
|
|
|
$app->make(Encrypter::class), |
|
48
|
|
|
$config->get('api.clients', []) |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$client->setDefaultAppKey($config->get('api.default_client')); |
|
52
|
|
|
|
|
53
|
|
|
return $client; |
|
54
|
|
|
}); |
|
55
|
|
|
|
|
56
|
|
|
$this->app->alias('api.client', Client::class); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Register the Token singleton. |
|
61
|
|
|
* |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function registerToken() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->app->singleton('api.token', function ($app) { |
|
67
|
|
|
return new Token($app->make(Client::class)); |
|
68
|
|
|
}); |
|
69
|
|
|
|
|
70
|
|
|
$this->app->alias('api.token', Token::class); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Register for console. |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function registerForConsole() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->publishes([ |
|
81
|
|
|
__DIR__.'/../config/api.php' => base_path('config/api.php'), |
|
82
|
|
|
], 'api'); |
|
83
|
|
|
|
|
84
|
|
|
$this->commands([ |
|
85
|
|
|
Console\GenerateClientCommand::class, |
|
86
|
|
|
Console\GenerateTokenCommand::class, |
|
87
|
|
|
]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get the services provided by the provider. |
|
92
|
|
|
* |
|
93
|
|
|
* @return string[] |
|
94
|
|
|
*/ |
|
95
|
|
|
public function provides() |
|
96
|
|
|
{ |
|
97
|
|
|
return ['api.client', Client::class, 'api.token', Token::class]; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|