Completed
Push — master ( 74da27...ac625a )
by wen
12:26
created

AdminServiceProvider::publishConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
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\Column\ColumnManager;
12
use Sco\Admin\Config\ConfigManager;
13
use Sco\Admin\Exceptions\Handler;
14
use Sco\Admin\Facades\Config as ConfigFacade;
15
use Sco\Admin\Facades\Admin as AdminFacade;
16
17
/**
18
 *
19
 */
20
class AdminServiceProvider extends ServiceProvider
21
{
22
    protected $commands = [
23
        \Sco\Admin\Console\InstallCommand::class,
24
    ];
25
26
    protected $middlewares = [
27
        'admin.guest'          => \Sco\Admin\Http\Middleware\RedirectIfAuthenticated::class,
28
        'admin.menu'           => \Sco\Admin\Http\Middleware\AdminMenu::class,
29
        'admin.permissions'    => \Sco\Admin\Http\Middleware\Permissions::class,
30
        'admin.phptojs'        => \Sco\Admin\Http\Middleware\PHPVarToJavaScript::class,
31
        'admin.can'            => \Sco\Admin\Http\Middleware\Authorize::class,
32
        'admin.role'           => \Sco\Admin\Http\Middleware\EntrustRole::class,
33
        'admin.resolve.config' => \Sco\Admin\Http\Middleware\ResolveConfigInstance::class,
34
    ];
35
36
    protected $providers = [
37
        LaravelServiceProvider::class,
38
        JavaScriptServiceProvider::class,
39
        PublishServiceProvider::class,
40
    ];
41
42
    protected $aliases = [
43
        'Admin'       => AdminFacade::class,
44
        'AdminConfig' => ConfigFacade::class,
45
    ];
46
47
    public function getBasePath()
48
    {
49
        return dirname(dirname(__DIR__));
50
    }
51
52
    /**
53
     * Bootstrap the application services.
54
     *
55
     * @return void
56
     */
57
    public function boot()
58
    {
59
        // 路由文件
60
        $this->loadRoutes();
61
62
        // 后台模板目录
63
        $this->loadViewsFrom(
64
            $this->getBasePath() . '/resources/views',
65
            'admin'
66
        );
67
        // 后台语言包目录
68
        $this->loadTranslationsFrom(
69
            $this->getBasePath() . '/resources/lang',
70
            'admin'
71
        );
72
73
        if ($this->app->runningInConsole()) {
74
            $this->loadMigrationsFrom($this->getBasePath() . '/database/migrations');
75
        }
76
    }
77
78
    protected function loadRoutes()
79
    {
80
        $routesFile = $this->getBasePath() . '/routes/admin.php';
81
        if (file_exists(base_path('routes/admin.php'))) {
82
            $routesFile = base_path('routes/admin.php');
83
        }
84
85
        $this->loadRoutesFrom($routesFile);
86
    }
87
88
    /**
89
     * Register the application services.
90
     *
91
     * @return void
92
     */
93
    public function register()
94
    {
95
        $this->registerMiddleware();
96
        $this->registerAliases();
97
98
        $this->commands($this->commands);
99
100
        $this->mergeConfigFrom(
101
            $this->getBasePath() . '/config/admin.php',
102
            'admin'
103
        );
104
105
        $this->registerProviders();
106
        $this->registerExceptionHandler();
107
108
        $this->registerConfigFactory();
109
        //$this->registerColumnFactory();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% 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...
110
111
        $this->registerAdmin();
112
    }
113
114
    protected function registerMiddleware()
115
    {
116
        $router = $this->app['router'];
117
        foreach ($this->middlewares as $key => $middleware) {
118
            $router->aliasMiddleware($key, $middleware);
119
        }
120
121
        /*        $router->bind('model', function ($value) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
122
                    return $this->app->make('admin.config.factory')->makeFromUri($value);
123
                });*/
124
    }
125
126
    protected function registerAliases()
127
    {
128
        AliasLoader::getInstance($this->aliases);
129
    }
130
131
    protected function registerProviders()
132
    {
133
        foreach ($this->providers as $provider) {
134
            $this->app->register($provider);
135
        }
136
    }
137
138
    protected function registerExceptionHandler()
139
    {
140
        $exceptHandler = app(ExceptionHandler::class);
141
        $this->app->singleton(
142
            ExceptionHandler::class,
143
            function () use ($exceptHandler) {
144
                return new Handler($exceptHandler);
145
            }
146
        );
147
    }
148
149
    protected function registerConfigFactory()
150
    {
151
        $this->app->singleton('admin.config.factory', function ($app) {
152
            return new ConfigManager($app);
153
        });
154
    }
155
156
    protected function registerAdmin()
157
    {
158
        $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...
159
    }
160
161
}
162