Completed
Push — master ( 1b3dfc...3e655f )
by Elf
05:19 queued 02:52
created

AppServiceProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 40%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 77
ccs 14
cts 35
cp 0.4
rs 10
c 2
b 1
f 0
wmc 12
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
B register() 0 18 5
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['config']['app.debug']) {
28
            $this->registerServicesForDebugging();
29
        }
30
31 1
        if ($this->app->runningInConsole()) {
32 1
            $this->registerServicesForConsole();
33 1
        }
34
35 1
        if (is_domain('admin')) {
36
            $this->registerServicesForAdmin();
37
        }
38
39 1
        if (is_domain('api')) {
40
            $this->hackForApiRequest();
41
        }
42 1
    }
43
44
    /**
45
     * Register services for debugging.
46
     */
47
    protected function registerServicesForDebugging()
48
    {
49
        $this->app->register(\Barryvdh\Debugbar\ServiceProvider::class);
50
        $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
51
    }
52
53
    /**
54
     * Register services for console.
55
     */
56 1
    protected function registerServicesForConsole()
57
    {
58 1
        $this->app->register(\BackupManager\Laravel\Laravel5ServiceProvider::class);
59 1
    }
60
61
    /**
62
     * Register services for admin.
63
     */
64
    protected function registerServicesForAdmin()
65
    {
66
        $this->app->register(\Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider::class);
67
    }
68
69
    /**
70
     * Hack the current Request instance for adding "Accept: application/json" header,
71
     * to make `$request->expectsJson()` working for API requests.
72
     */
73
    protected function hackForApiRequest()
74
    {
75
        $this->app->rebinding('request', function ($app, $request) {
76
            if (! str_contains(($accept = $request->headers->get('Accept')), ['/json', '+json'])) {
77
                $accept = 'application/json'.(empty($accept) ? '' : ',').$accept;
78
79
                $request->headers->set('Accept', $accept);
80
                $request->server->set('HTTP_ACCEPT', $accept);
81
            }
82
        });
83
    }
84
}
85