Completed
Push — master ( 15c9e2...d1079a )
by wen
03:11
created

AdminServiceProvider   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 19
Bugs 5 Features 6
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 147
rs 10
c 19
b 5
f 6
ccs 0
cts 88
cp 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getBasePath() 0 4 1
A publishAdmin() 0 8 1
A publishAssets() 0 6 1
A publishConfig() 0 6 1
A publishViews() 0 6 1
A publishTranslations() 0 6 1
A publishRoutes() 0 6 1
A boot() 0 22 2
A loadRoutes() 0 9 2
A register() 0 14 1
A registerMiddleware() 0 7 2
A registerProviders() 0 5 1
A registerExceptionHandler() 0 10 1
1
<?php
2
3
namespace Sco\Admin\Providers;
4
5
use Illuminate\Contracts\Debug\ExceptionHandler;
6
use Illuminate\Support\ServiceProvider;
7
use Sco\Admin\Exceptions\Handler;
8
9
/**
10
 *
11
 */
12
class AdminServiceProvider extends ServiceProvider
13
{
14
    protected $commands = [
15
        \Sco\Admin\Console\InstallCommand::class,
16
    ];
17
18
    protected $middlewares = [
19
        'admin.guest'       => \Sco\Admin\Http\Middleware\RedirectIfAuthenticated::class,
20
        'admin.menu'        => \Sco\Admin\Http\Middleware\AdminMenu::class,
21
        'admin.permissions' => \Sco\Admin\Http\Middleware\Permissions::class,
22
        'admin.phptojs'     => \Sco\Admin\Http\Middleware\PHPVarToJavaScript::class,
23
        'admin.can'         => \Sco\Admin\Http\Middleware\Authorize::class,
24
        'admin.role'        => \Sco\Admin\Http\Middleware\EntrustRole::class,
25
    ];
26
27
    public function getBasePath()
28
    {
29
        return dirname(dirname(__DIR__));
30
    }
31
32
    /**
33
     * Bootstrap the application services.
34
     *
35
     * @return void
36
     */
37
    public function boot()
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
    }
59
60
    protected function loadRoutes()
61
    {
62
        $routesFile = $this->getBasePath() . '/routes/admin.php';
63
        if (file_exists(base_path('routes/admin.php'))) {
64
            $routesFile = base_path('routes/admin.php');
65
        }
66
67
        $this->loadRoutesFrom($routesFile);
68
    }
69
70
    /**
71
     * Register the application services.
72
     *
73
     * @return void
74
     */
75
    public function register()
76
    {
77
        $this->registerMiddleware();
78
79
        $this->commands($this->commands);
80
81
        $this->mergeConfigFrom(
82
            $this->getBasePath() . '/config/admin.php',
83
            'admin'
84
        );
85
86
        $this->registerProviders();
87
        $this->registerExceptionHandler();
88
    }
89
90
    protected function registerMiddleware()
91
    {
92
        $router = $this->app['router'];
93
        foreach ($this->middlewares as $key => $middleware) {
94
            $router->aliasMiddleware($key, $middleware);
95
        }
96
    }
97
98
    protected function registerProviders()
99
    {
100
        $this->app->register(\Sco\ActionLog\LaravelServiceProvider::class);
101
        $this->app->register(\Laracasts\Utilities\JavaScript\JavaScriptServiceProvider::class);
102
    }
103
104
    protected function registerExceptionHandler()
105
    {
106
        $exceptHandler = app(ExceptionHandler::class);
107
        $this->app->singleton(
108
            ExceptionHandler::class,
109
            function () use ($exceptHandler) {
110
                return new Handler($exceptHandler);
111
            }
112
        );
113
    }
114
115
    protected function publishAdmin()
116
    {
117
        $this->publishAssets();
118
        $this->publishConfig();
119
        $this->publishViews();
120
        $this->publishTranslations();
121
        $this->publishRoutes();
122
    }
123
124
    protected function publishAssets()
125
    {
126
        $this->publishes([
127
            $this->getBasePath() . '/resources/assets' => base_path('resources/assets/vendor/admin'),
128
        ], 'assets');
129
    }
130
131
    protected function publishConfig()
132
    {
133
        $this->publishes([
134
            $this->getBasePath() . '/config/' => config_path(),
135
        ], 'config');
136
    }
137
138
    protected function publishViews()
139
    {
140
        $this->publishes([
141
            $this->getBasePath() . '/resources/views' => base_path('resources/views/vendor/admin'),
142
        ], 'views');
143
    }
144
145
    protected function publishTranslations()
146
    {
147
        $this->publishes([
148
            $this->getBasePath() . '/resources/lang' => base_path('resources/lang/vendor/admin'),
149
        ], 'lang');
150
    }
151
152
    protected function publishRoutes()
153
    {
154
        $this->publishes([
155
            $this->getBasePath() . '/routes/admin.php' => base_path('routes/admin.php'),
156
        ], 'routes');
157
    }
158
}
159