Completed
Push — master ( 78b3ab...6795be )
by wen
15:30
created

AdminServiceProvider   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 20
Bugs 5 Features 7
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 131
rs 10
c 20
b 5
f 7
ccs 0
cts 88
cp 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getBasePath() 0 4 1
A boot() 0 20 2
A loadRoutes() 0 9 2
A register() 0 17 1
A registerMiddleware() 0 6 2
A registerAliases() 0 4 1
A registerProviders() 0 6 2
A registerExceptionHandler() 0 10 1
A registerAdmin() 0 4 1
A bindRouteModel() 0 6 1
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 Laracasts\Utilities\JavaScript\JavaScriptServiceProvider;
9
use Sco\ActionLog\LaravelServiceProvider;
10
use Sco\Admin\Admin;
11
use Sco\Admin\Exceptions\Handler;
12
use Sco\Admin\Facades\AdminFacade;
13
14
/**
15
 *
16
 */
17
class AdminServiceProvider extends ServiceProvider
18
{
19
    protected $commands = [
20
        \Sco\Admin\Console\InstallCommand::class,
21
    ];
22
23
    protected $middlewares = [
24
        'admin.guest'     => \Sco\Admin\Http\Middleware\RedirectIfAuthenticated::class,
25
        'admin.phptojs'   => \Sco\Admin\Http\Middleware\PHPVarToJavaScript::class,
26
        'admin.can.route' => \Sco\Admin\Http\Middleware\RouteAuthorize::class,
27
        'admin.can.model' => \Sco\Admin\Http\Middleware\ModelAuthorize::class,
28
        'admin.role'      => \Sco\Admin\Http\Middleware\EntrustRole::class,
29
    ];
30
31
    protected $providers = [
32
        LaravelServiceProvider::class,
33
        JavaScriptServiceProvider::class,
34
        PublishServiceProvider::class,
35
    ];
36
37
    protected $aliases = [
38
        'Admin' => AdminFacade::class,
39
        //'AdminConfig' => ConfigFacade::class,
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
40
    ];
41
42
    public function getBasePath()
43
    {
44
        return dirname(dirname(__DIR__));
45
    }
46
47
    /**
48
     * Bootstrap the application services.
49
     *
50
     * @return void
51
     */
52
    public function boot()
53
    {
54
        // 路由文件
55
        $this->loadRoutes();
56
57
        // 后台模板目录
58
        $this->loadViewsFrom(
59
            $this->getBasePath() . '/resources/views',
60
            'admin'
61
        );
62
        // 后台语言包目录
63
        $this->loadTranslationsFrom(
64
            $this->getBasePath() . '/resources/lang',
65
            'admin'
66
        );
67
68
        if ($this->app->runningInConsole()) {
69
            $this->loadMigrationsFrom($this->getBasePath() . '/database/migrations');
70
        }
71
    }
72
73
    protected function loadRoutes()
74
    {
75
        $routesFile = $this->getBasePath() . '/routes/admin.php';
76
        if (file_exists(base_path('routes/admin.php'))) {
77
            $routesFile = base_path('routes/admin.php');
78
        }
79
80
        $this->loadRoutesFrom($routesFile);
81
    }
82
83
    /**
84
     * Register the application services.
85
     *
86
     * @return void
87
     */
88
    public function register()
89
    {
90
        $this->mergeConfigFrom(
91
            $this->getBasePath() . '/config/admin.php',
92
            'admin'
93
        );
94
95
        $this->registerExceptionHandler();
96
        $this->registerAdmin();
97
        $this->registerAliases();
98
        $this->registerMiddleware();
99
        $this->bindRouteModel();
100
101
        $this->commands($this->commands);
102
103
        $this->registerProviders();
104
    }
105
106
    protected function registerMiddleware()
107
    {
108
        foreach ($this->middlewares as $key => $middleware) {
109
            $this->app['router']->aliasMiddleware($key, $middleware);
110
        }
111
    }
112
113
    protected function registerAliases()
114
    {
115
        AliasLoader::getInstance($this->aliases);
116
    }
117
118
    protected function registerProviders()
119
    {
120
        foreach ($this->providers as $provider) {
121
            $this->app->register($provider);
122
        }
123
    }
124
125
    protected function registerExceptionHandler()
126
    {
127
        $exceptHandler = app(ExceptionHandler::class);
128
        $this->app->singleton(
129
            ExceptionHandler::class,
130
            function () use ($exceptHandler) {
131
                return new Handler($exceptHandler);
132
            }
133
        );
134
    }
135
136
    protected function registerAdmin()
137
    {
138
        $this->app->instance('admin.instance', new Admin($this->app));
1 ignored issue
show
Compatibility introduced by
$this->app of type object<Illuminate\Contra...Foundation\Application> is not a sub-type of object<Illuminate\Foundation\Application>. It seems like you assume a concrete implementation of the interface Illuminate\Contracts\Foundation\Application to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
139
    }
140
141
    protected function bindRouteModel()
142
    {
143
        $this->app['router']->bind('model', function ($value) {
144
            return $this->app['admin.instance']->getConfig($value);
145
        });
146
    }
147
}
148