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

AppServiceProvider   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
dl 0
loc 110
ccs 20
cts 20
cp 1
rs 10
c 4
b 1
f 0
wmc 14
lcom 1
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
A extendResponses() 0 6 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 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