Completed
Push — master ( 180d5c...3a2462 )
by wen
03:42
created

AdminServiceProvider::boot()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 8
Bugs 1 Features 5
Metric Value
dl 0
loc 25
ccs 0
cts 18
cp 0
rs 8.8571
c 8
b 1
f 5
cc 2
eloc 13
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Sco\Admin\Providers;
4
5
use Illuminate\Contracts\Debug\ExceptionHandler;
6
use Illuminate\Foundation\AliasLoader;
7
use Illuminate\Support\ServiceProvider;
8
use Sco\Admin\Exceptions\Handler;
9
10
/**
11
 *
12
 */
13
class AdminServiceProvider extends ServiceProvider
14
{
15
    protected $commands = [
16
        \Sco\Admin\Commands\Install::class,
17
    ];
18
19
    protected $middlewares = [
20
        'auth.admin'  => \Sco\Admin\Http\Middleware\AdminAuthenticate::class,
21
        'guest.admin' => \Sco\Admin\Http\Middleware\RedirectIfAuthenticated::class,
22
        'admin.menu'  => \Sco\Admin\Http\Middleware\AdminMenu::class,
23
    ];
24
25
    public function getBasePath()
26
    {
27
        return dirname(dirname(__DIR__));
28
    }
29
30
    /**
31
     * Bootstrap the application services.
32
     *
33
     * @return void
34
     */
35
    public function boot()
36
    {
37
        $this->registerMiddleware();
38
39
        // 路由文件
40
        $this->loadRoutes();
41
42
        // 后台模板目录
43
        $this->loadViewsFrom(
44
            $this->getBasePath() . '/resources/views',
45
            'admin'
46
        );
47
        // 后台语言包目录
48
        $this->loadTranslationsFrom(
49
            $this->getBasePath() . '/resources/lang',
50
            'admin'
51
        );
52
53
        if ($this->app->runningInConsole()) {
54
            $this->loadMigrationsFrom($this->getBasePath() . '/database/migrations');
55
            $this->publishAdmin();
56
        }
57
58
        $this->registerExceptionHandler();
59
    }
60
61
    protected function registerExceptionHandler()
62
    {
63
        $exceptHandler = app(ExceptionHandler::class);
64
        $this->app->singleton(
65
            ExceptionHandler::class,
66
            function () use ($exceptHandler) {
67
                return new Handler($exceptHandler);
68
            }
69
        );
70
    }
71
72
    protected function registerMiddleware()
73
    {
74
        $router = $this->app['router'];
75
        foreach ($this->middlewares as $key => $middleware) {
76
            $router->aliasMiddleware($key, $middleware);
77
        }
78
    }
79
80
    protected function loadRoutes()
81
    {
82
        $routesFile = $this->getBasePath() . '/routes/admin.php';
83
        if (file_exists(base_path('routes/admin.php'))) {
84
            $routesFile = base_path('routes/admin.php');
85
        }
86
87
        $this->loadRoutesFrom($routesFile);
88
    }
89
90
    /**
91
     * Register the application services.
92
     *
93
     * @return void
94
     */
95
    public function register()
96
    {
97
        //$this->commands($this->commands);
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
99
        $this->mergeConfigFrom(
100
            $this->getBasePath() . '/config/admin.php',
101
            'admin'
102
        );
103
104
        $this->registerProviders();
105
    }
106
107
    protected function registerProviders()
108
    {
109
        $this->app->register(\Zizaco\Entrust\EntrustServiceProvider::class);
110
        $this->app->register(\Sco\ActionLog\LaravelServiceProvider::class);
111
112
        AliasLoader::getInstance([
113
            'Entrust'   => \Zizaco\Entrust\EntrustFacade::class,
114
        ])->register();
115
    }
116
117
    protected function publishAdmin()
118
    {
119
        $this->publishAssets();
120
        $this->publishConfig();
121
        $this->publishViews();
122
        $this->publishTranslations();
123
        $this->publishRoutes();
124
    }
125
126
    protected function publishAssets()
127
    {
128
        $this->publishes([
129
            $this->getBasePath() . '/resources/assets' => base_path('resources/assets/vendor/admin'),
130
        ], 'assets');
131
    }
132
133
    protected function publishConfig()
134
    {
135
        $this->publishes([
136
            $this->getBasePath() . '/config/'   => config_path(),
137
        ], 'config');
138
    }
139
140
    protected function publishViews()
141
    {
142
        $this->publishes([
143
            $this->getBasePath() . '/resources/views' => base_path('resources/views/vendor/admin'),
144
        ], 'views');
145
    }
146
147
    protected function publishTranslations()
148
    {
149
        $this->publishes([
150
            $this->getBasePath() . '/resources/lang' => base_path('resources/lang/vendor/admin'),
151
        ], 'lang');
152
    }
153
154
    protected function publishRoutes()
155
    {
156
        $this->publishes([
157
            $this->getBasePath() . '/routes/admin.php' => base_path('routes/admin.php'),
158
        ], 'routes');
159
    }
160
}
161