Completed
Push — master ( d3d242...9eb338 )
by Elf
05:07
created

AppServiceProvider::addAcceptableJsonType()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 0
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
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
     * @return void
14
     */
15 1
    public function boot()
16
    {
17 1
        Carbon::setLocale('zh');
18 1
    }
19
20
    /**
21
     * Register any application services.
22
     *
23
     * @return void
24
     */
25 1
    public function register()
26
    {
27 1
        if ($this->app->isLocal()) {
28
            $this->registerServices($this->getProvidersForLocalEnvironment());
29
        }
30
31 1
        if ($this->app->runningInConsole()) {
32 1
            $this->registerServices($this->getProvidersForConsole());
33 1
        }
34
35 1
        if (is_domain('admin')) {
36
            $this->registerServices($this->getProvidersForAdminSite());
37
        }
38
39 1
        if (is_domain('api')) {
40
            $this->addAcceptableJsonType();
41
        }
42 1
    }
43
44
    /**
45
     * Register services.
46
     *
47
     * @param  string|array  $services
48
     * @return void
49
     */
50 1
    protected function registerServices($services)
51
    {
52 1
        foreach ((array) $services as $value) {
53 1
            $this->app->register($value);
54 1
        }
55 1
    }
56
57
    /**
58
     * Get the services provided for local environment.
59
     *
60
     * @return array
61
     */
62
    protected function getProvidersForLocalEnvironment()
63
    {
64
        return [
65
            'Barryvdh\Debugbar\ServiceProvider',
66
            'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider',
67
        ];
68
    }
69
70
    /**
71
     * Get the services provided for console.
72
     *
73
     * @return array
74
     */
75 1
    protected function getProvidersForConsole()
76
    {
77
        return [
78 1
            'BackupManager\Laravel\Laravel5ServiceProvider',
79 1
        ];
80
    }
81
82
    /**
83
     * Get the services provided for admin site.
84
     *
85
     * @return array
86
     */
87
    protected function getProvidersForAdminSite()
88
    {
89
        return [
90
            'Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider',
91
        ];
92
    }
93
94
    /**
95
     * Add "application/json" to the "Accept" header for the current request,
96
     * it will make `$request->expectsJson()` return true.
97
     */
98
    protected function addAcceptableJsonType()
99
    {
100
        $this->app->rebinding('request', function ($app, $request) {
101
            if (! str_contains(($accept = $request->header('Accept')), ['/json', '+json'])) {
102
                $accept = rtrim('application/json,'.$accept, ',');
103
104
                $request->headers->set('Accept', $accept);
105
                $request->server->set('HTTP_ACCEPT', $accept);
106
            }
107
        });
108
    }
109
}
110