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

AppServiceProvider::getProvidersForAdminSite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
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