1 | <?php |
||||||
2 | |||||||
3 | namespace ElfSundae\Laravel\Api; |
||||||
4 | |||||||
5 | use Closure; |
||||||
6 | use Illuminate\Support\Str; |
||||||
7 | use Illuminate\Http\Request; |
||||||
8 | |||||||
9 | class Helper |
||||||
10 | { |
||||||
11 | /** |
||||||
12 | * Stores the app key of the current api client. |
||||||
13 | */ |
||||||
14 | const CURRENT_APP_KEY = 'current_app_key'; |
||||||
15 | |||||||
16 | /** |
||||||
17 | * Set the current app key for the request. |
||||||
18 | * |
||||||
19 | * @param string $key |
||||||
20 | * @param \Illuminate\Http\Request|null $request |
||||||
21 | * @return \Illuminate\Http\Request |
||||||
22 | */ |
||||||
23 | public static function setCurrentAppKeyForRequest($key, Request $request = null) |
||||||
24 | { |
||||||
25 | $request = $request ?: app('request'); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
26 | |||||||
27 | $request->attributes->set(static::CURRENT_APP_KEY, $key); |
||||||
28 | |||||||
29 | return $request; |
||||||
30 | } |
||||||
31 | |||||||
32 | /** |
||||||
33 | * Get the app key of the current api client. |
||||||
34 | * |
||||||
35 | * @param \Illuminate\Http\Request|null $request |
||||||
36 | * @return string|null |
||||||
37 | */ |
||||||
38 | public static function getCurrentAppKey(Request $request = null) |
||||||
39 | { |
||||||
40 | $request = $request ?: app('request'); |
||||||
0 ignored issues
–
show
The function
app 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
![]() |
|||||||
41 | |||||||
42 | return $request->attributes->get(static::CURRENT_APP_KEY); |
||||||
43 | } |
||||||
44 | |||||||
45 | /** |
||||||
46 | * Add JSON type to the "Accept" header for the request. |
||||||
47 | * |
||||||
48 | * @see https://laravel-china.org/topics/3430 |
||||||
49 | * |
||||||
50 | * @param \Closure $determination |
||||||
51 | * @param \Closure $callback |
||||||
52 | * @return mixed |
||||||
53 | */ |
||||||
54 | public static function addAcceptableJsonTypeForRequest(Closure $determination = null, Closure $callback = null) |
||||||
55 | { |
||||||
56 | app()->rebinding('request', function ($app, Request $request) use ($determination, $callback) { |
||||||
0 ignored issues
–
show
The function
app 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
![]() |
|||||||
57 | if (is_null($determination) || $determination($request)) { |
||||||
58 | $accept = $request->headers->get('Accept'); |
||||||
59 | |||||||
60 | if (! Str::contains($accept, ['/json', '+json'])) { |
||||||
61 | $accept = rtrim('application/json,'.$accept, ','); |
||||||
62 | |||||||
63 | $request->headers->set('Accept', $accept); |
||||||
64 | $request->server->set('HTTP_ACCEPT', $accept); |
||||||
65 | $_SERVER['HTTP_ACCEPT'] = $accept; |
||||||
66 | |||||||
67 | if ($callback) { |
||||||
68 | $callback($request); |
||||||
69 | } |
||||||
70 | } |
||||||
71 | } |
||||||
72 | }); |
||||||
73 | } |
||||||
74 | } |
||||||
75 |