Completed
Push — master ( 9c0195...4ed182 )
by Elf
04:17
created

AppServiceProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
dl 0
loc 98
ccs 16
cts 16
cp 1
rs 10
c 4
b 1
f 0
wmc 13
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
B register() 0 18 5
A registerServices() 0 6 2
A getProvidersForLocalEnvironment() 0 7 1
A getProvidersForConsole() 0 6 1
A getProvidersForAdminSite() 0 6 1
A addAcceptableJsonType() 0 11 2
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 ($this->app->isLocal()) {
24
            $this->registerServices($this->getProvidersForLocalEnvironment());
25
        }
26
27 1
        if ($this->app->runningInConsole()) {
28 1
            $this->registerServices($this->getProvidersForConsole());
29
        }
30
31 1
        if (is_domain('admin')) {
32
            $this->registerServices($this->getProvidersForAdminSite());
33
        }
34
35 1
        if (is_domain('api')) {
36
            $this->addAcceptableJsonType();
37
        }
38 1
    }
39
40
    /**
41
     * Register services.
42
     *
43
     * @param  string|array  $services
44
     * @return void
45
     */
46 1
    protected function registerServices($services)
47
    {
48 1
        foreach ((array) $services as $value) {
49 1
            $this->app->register($value);
50
        }
51 1
    }
52
53
    /**
54
     * Get the services provided for local environment.
55
     *
56
     * @return array
57
     */
58
    protected function getProvidersForLocalEnvironment()
59
    {
60
        return [
61
            'Barryvdh\Debugbar\ServiceProvider',
62
            'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider',
63
        ];
64
    }
65
66
    /**
67
     * Get the services provided for console.
68
     *
69
     * @return array
70
     */
71 1
    protected function getProvidersForConsole()
72
    {
73
        return [
74 1
            'BackupManager\Laravel\Laravel5ServiceProvider',
75
        ];
76
    }
77
78
    /**
79
     * Get the services provided for admin site.
80
     *
81
     * @return array
82
     */
83
    protected function getProvidersForAdminSite()
84
    {
85
        return [
86
            'Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider',
87
        ];
88
    }
89
90
    /**
91
     * Add "application/json" to the "Accept" header for the current request,
92
     * it will make `$request->expectsJson()` return true.
93
     */
94
    protected function addAcceptableJsonType()
95
    {
96
        $this->app->rebinding('request', function ($app, $request) {
97
            if (! str_contains(($accept = $request->header('Accept')), ['/json', '+json'])) {
98
                $accept = rtrim('application/json,'.$accept, ',');
99
100
                $request->headers->set('Accept', $accept);
101
                $request->server->set('HTTP_ACCEPT', $accept);
102
            }
103
        });
104
    }
105
}
106