1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Support\Providers; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use ElfSundae\Laravel\Support\Helper; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
|
10
|
|
|
class AppServiceProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Bootstrap any application services. |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
|
|
public function boot() |
18
|
|
|
{ |
19
|
|
|
$this->setLocaleForCarbon(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Set locale for Carbon. |
24
|
|
|
* |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
|
|
protected function setLocaleForCarbon() |
28
|
|
|
{ |
29
|
|
|
return Carbon::setLocale( |
30
|
|
|
$this->app['config']->get('support.carbon_locale', $this->app->getLocale()) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Register any application services. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function register() |
40
|
|
|
{ |
41
|
|
|
array_map([$this->app, 'register'], $this->getServiceProviders()); |
42
|
|
|
|
43
|
|
|
$this->modifyCurrentRequest(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get service providers to be registered. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
protected function getServiceProviders() |
52
|
|
|
{ |
53
|
|
|
$providers = []; |
54
|
|
|
|
55
|
|
|
if (is_app('admin') || $this->app->runningInConsole()) { |
56
|
|
|
array_push( |
57
|
|
|
$providers, |
58
|
|
|
\ElfSundae\Laravel\Datatables\DatatablesServiceProvider::class, |
59
|
|
|
\Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider::class |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $providers; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Modify the current request. |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
protected function modifyCurrentRequest() |
72
|
|
|
{ |
73
|
|
|
Helper::addAcceptableJsonType(function (Request $request) { |
74
|
|
|
return $this->shouldAddAcceptableJsonType($request); |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
if ($this->shouldFakeAppClient()) { |
78
|
|
|
$this->fakeAppClient(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Determines appending JSON type to the "Accept" header for the current request. |
84
|
|
|
* |
85
|
|
|
* @param \Illuminate\Http\Request $request |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
protected function shouldAddAcceptableJsonType(Request $request) |
89
|
|
|
{ |
90
|
|
|
return $this->isApiRequest(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Indicates the current request is an API request, e.g. the request is sent |
95
|
|
|
* from an API client. |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
protected function isApiRequest() |
100
|
|
|
{ |
101
|
|
|
return is_app('api'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Determines making a fake API client for the current request. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
protected function shouldFakeAppClient() |
110
|
|
|
{ |
111
|
|
|
return $this->app['config']->get('app.debug') && |
112
|
|
|
$this->app->isLocal() && |
113
|
|
|
$this->app['request']->ip() === '127.0.0.1' && |
114
|
|
|
$this->isApiRequest(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Fake current agent client as an app client. |
119
|
|
|
* |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
protected function fakeAppClient() |
123
|
|
|
{ |
124
|
|
|
$this->app->resolving('agent.client', function ($client) { |
125
|
|
|
if (! $client->is('AppClient')) { |
126
|
|
|
$client->setUserAgent($this->getUserAgentForFakingAppClient()); |
127
|
|
|
} |
128
|
|
|
}); |
129
|
|
|
|
130
|
|
|
$this->app->rebinding('request', function ($app, $request) { |
131
|
|
|
if ($request->hasHeader('X-API-TOKEN') || $request->has('_token')) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$request->headers->add( |
136
|
|
|
$this->getApiTokenHeadersForFakingAppClient() |
137
|
|
|
); |
138
|
|
|
}); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get User-Agent string for faking app client. |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
protected function getUserAgentForFakingAppClient() |
147
|
|
|
{ |
148
|
|
|
$clientData = urlsafe_base64_encode(json_encode( |
149
|
|
|
$this->app['config']->get('support.fake_app_client', []) |
150
|
|
|
)); |
151
|
|
|
|
152
|
|
|
$userAgent = $this->app['request']->header('User-Agent'); |
153
|
|
|
|
154
|
|
|
return "$userAgent client({$clientData})"; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get API token request headers for faking app client. |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
protected function getApiTokenHeadersForFakingAppClient() |
163
|
|
|
{ |
164
|
|
|
$data = app('api.token')->generateDataForKey( |
165
|
|
|
app('api.client')->defaultAppKey() |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$headers = []; |
169
|
|
|
|
170
|
|
|
foreach ($data as $key => $value) { |
171
|
|
|
$headers['X-API-'.strtoupper($key)] = $value; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $headers; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|