Completed
Push — master ( a7b5fe...4405b1 )
by wen
03:16
created

AdminServiceProvider::boot()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 6
Bugs 1 Features 5
Metric Value
dl 0
loc 24
rs 8.9713
c 6
b 1
f 5
ccs 0
cts 13
cp 0
cc 2
eloc 12
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Sco\Admin\Providers;
4
5
use Carbon\Carbon;
6
use Illuminate\Contracts\Debug\ExceptionHandler;
7
use Illuminate\Foundation\AliasLoader;
8
use Illuminate\Support\ServiceProvider;
9
use KodiComponents\Navigation\Contracts\BadgeInterface;
10
use KodiComponents\Navigation\Contracts\NavigationInterface;
11
use KodiComponents\Navigation\Contracts\PageInterface;
12
use KodiComponents\Navigation\Navigation;
13
use KodiComponents\Navigation\NavigationServiceProvider;
14
use Laracasts\Utilities\JavaScript\JavaScriptServiceProvider;
15
use Sco\ActionLog\LaravelServiceProvider;
16
use Sco\Admin\Admin;
17
use Sco\Admin\Config\ConfigFactory;
18
use Sco\Admin\Contracts\ConfigFactoryInterface;
19
use Sco\Admin\Contracts\RepositoryInterface;
20
use Sco\Admin\Elements\ElementFactory;
21
use Sco\Admin\Exceptions\Handler;
22
use Sco\Admin\Facades\AdminFacade;
23
use Sco\Admin\Facades\AdminElementFacade;
24
use Sco\Admin\Facades\AdminNavigationFacade;
25
use Sco\Admin\Navigation\Badge;
26
use Sco\Admin\Navigation\Page;
27
use Sco\Admin\Repositories\Repository;
28
29
/**
30
 *
31
 */
32
class AdminServiceProvider extends ServiceProvider
33
{
34
    protected $commands = [
35
        \Sco\Admin\Console\InstallCommand::class,
36
    ];
37
38
    protected $middlewares = [
39
        'admin.guest'     => \Sco\Admin\Http\Middleware\RedirectIfAuthenticated::class,
40
        'admin.phptojs'   => \Sco\Admin\Http\Middleware\PHPVarToJavaScript::class,
41
        'admin.can.route' => \Sco\Admin\Http\Middleware\RouteAuthorize::class,
42
        'admin.can.model' => \Sco\Admin\Http\Middleware\ModelAuthorize::class,
43
    ];
44
45
    protected $providers = [
46
        LaravelServiceProvider::class,
47
        JavaScriptServiceProvider::class,
48
        PublishServiceProvider::class,
49
        //NavigationServiceProvider::class,
50
    ];
51
52
    protected $aliases = [
53
        'Admin'           => AdminFacade::class,
54
        'AdminElement'    => AdminElementFacade::class,
55
        'AdminNavigation' => AdminNavigationFacade::class,
56
    ];
57
58
    public function getBasePath()
59
    {
60
        return dirname(dirname(__DIR__));
61
    }
62
63
    /**
64
     * Bootstrap the application services.
65
     *
66
     * @return void
67
     */
68
    public function boot()
69
    {
70
        Carbon::setLocale($this->app['config']->get('app.locale'));
71
72
        // 路由文件
73
        $this->loadRoutes();
74
75
        // 后台模板目录
76
        $this->loadViewsFrom(
77
            $this->getBasePath() . '/resources/views',
78
            'admin'
79
        );
80
        // 后台语言包目录
81
        $this->loadTranslationsFrom(
82
            $this->getBasePath() . '/resources/lang',
83
            'admin'
84
        );
85
86
        if ($this->app->runningInConsole()) {
87
            $this->loadMigrationsFrom($this->getBasePath() . '/database/migrations');
88
        }
89
90
        $this->app->call([$this, 'registerNavigation']);
91
    }
92
93
    protected function loadRoutes()
94
    {
95
        $routesFile = $this->getBasePath() . '/routes/admin.php';
96
        $this->loadRoutesFrom($routesFile);
97
    }
98
99
    /**
100
     * Register the application services.
101
     *
102
     * @return void
103
     */
104
    public function register()
105
    {
106
        $this->mergeConfigFrom(
107
            $this->getBasePath() . '/config/admin.php',
108
            'admin'
109
        );
110
111
        $this->registerExceptionHandler();
112
        $this->registerAdmin();
113
        $this->registerAliases();
114
        $this->registerMiddleware();
115
        $this->bindRouteModel();
116
        $this->registerProviders();
117
        $this->registerCoreContainerAliases();
118
119
120
        $this->app->bind(RepositoryInterface::class, Repository::class);
121
        $this->app->singleton(ConfigFactoryInterface::class, function () {
122
            return new ConfigFactory($this->app);
0 ignored issues
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...
123
        });
124
        $this->app->singleton('admin.element.factory', function () {
125
            return new ElementFactory($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...
126
        });
127
128
129
        // overwrite Navigation Page Bind
130
        $this->app->singleton('admin.navigation', function () {
131
            return new Navigation();
132
        });
133
        $this->app->bind(PageInterface::class, Page::class);
134
        $this->app->bind(BadgeInterface::class, Badge::class);
135
136
        $this->commands($this->commands);
137
138
    }
139
140
    protected function registerMiddleware()
141
    {
142
        foreach ($this->middlewares as $key => $middleware) {
143
            $this->app['router']->aliasMiddleware($key, $middleware);
144
        }
145
    }
146
147
    protected function registerAliases()
148
    {
149
        AliasLoader::getInstance($this->aliases);
150
    }
151
152
    protected function registerProviders()
153
    {
154
        foreach ($this->providers as $provider) {
155
            $this->app->register($provider);
156
        }
157
    }
158
159
    protected function registerExceptionHandler()
160
    {
161
        $exceptHandler = app(ExceptionHandler::class);
162
        $this->app->singleton(
163
            ExceptionHandler::class,
164
            function () use ($exceptHandler) {
165
                return new Handler($exceptHandler);
166
            }
167
        );
168
    }
169
170
    protected function registerAdmin()
171
    {
172
        $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...
173
    }
174
175
    protected function bindRouteModel()
176
    {
177
        $this->app['router']->bind('model', function ($value) {
178
            return $this->app[ConfigFactoryInterface::class]->make($value)->getModel();
179
        });
180
    }
181
182
    protected function registerCoreContainerAliases()
183
    {
184
        $aliases = [
185
            'admin.navigation' => [Navigation::class, NavigationInterface::class],
186
        ];
187
188
        foreach ($aliases as $key => $aliases) {
189
            foreach ($aliases as $alias) {
190
                $this->app->alias($key, $alias);
191
            }
192
        }
193
    }
194
195
    /**
196
     * @param NavigationInterface $navigation
197
     */
198
    public function registerNavigation(NavigationInterface $navigation)
199
    {
200
        require $this->getBasePath() . '/src/navigation.php';
201
    }
202
}
203