Completed
Push — master ( abe7e1...daed7d )
by Elf
04:48
created

AppServiceProvider::extendResponses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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