Completed
Push — master ( b3f467...3d5acc )
by Elf
05:46
created

AppServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 62
ccs 7
cts 7
cp 1
rs 10
wmc 8
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A registerServices() 0 4 1
A getProvidersForAdminSite() 0 6 1
A addAcceptableJsonType() 0 11 2
A register() 0 10 3
1
<?php
2
3
namespace App\Providers;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\ServiceProvider;
7
8
class AppServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Bootstrap any application services.
12
     */
13 1
    public function boot()
14
    {
15 1
        Carbon::setLocale('zh');
16 1
    }
17
18
    /**
19
     * Register any application services.
20
     */
21 1
    public function register()
22
    {
23 1
        if (is_domain('admin')) {
24
            $this->registerServices($this->getProvidersForAdminSite());
25
        }
26
27 1
        if (is_domain('api')) {
28
            $this->addAcceptableJsonType();
29
        }
30 1
    }
31
32
    /**
33
     * Register services.
34
     *
35
     * @param  string|array  $services
36
     */
37
    protected function registerServices($services)
38
    {
39
        array_map([$this->app, 'register'], (array) $services);
40
    }
41
42
    /**
43
     * Get the services provided for admin site.
44
     *
45
     * @return array
46
     */
47
    protected function getProvidersForAdminSite()
48
    {
49
        return [
50
            'Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider',
51
        ];
52
    }
53
54
    /**
55
     * Add "application/json" to the "Accept" header for the current request,
56
     * it will make `$request->expectsJson()` return true.
57
     */
58
    protected function addAcceptableJsonType()
59
    {
60
        $this->app->rebinding('request', function ($app, $request) {
61
            if (! str_contains(($accept = $request->header('Accept')), ['/json', '+json'])) {
62
                $accept = rtrim('application/json,'.$accept, ',');
63
64
                $request->headers->set('Accept', $accept);
65
                $request->server->set('HTTP_ACCEPT', $accept);
66
            }
67
        });
68
    }
69
}
70