Completed
Push — master ( 1320bf...72f55a )
by Song
02:25
created

AdminServiceProvider::boot()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 16
nop 0
dl 0
loc 27
rs 8.8657
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\ServiceProvider;
7
8
class AdminServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $commands = [
14
        Console\AdminCommand::class,
15
        Console\MakeCommand::class,
16
        Console\MenuCommand::class,
17
        Console\InstallCommand::class,
18
        Console\PublishCommand::class,
19
        Console\UninstallCommand::class,
20
        Console\ImportCommand::class,
21
        Console\CreateUserCommand::class,
22
        Console\ResetPasswordCommand::class,
23
        Console\ExtendCommand::class,
24
    ];
25
26
    /**
27
     * The application's route middleware.
28
     *
29
     * @var array
30
     */
31
    protected $routeMiddleware = [
32
        'admin.auth'       => Middleware\Authenticate::class,
33
        'admin.pjax'       => Middleware\Pjax::class,
34
        'admin.log'        => Middleware\LogOperation::class,
35
        'admin.permission' => Middleware\Permission::class,
36
        'admin.bootstrap'  => Middleware\Bootstrap::class,
37
    ];
38
39
    /**
40
     * The application's route middleware groups.
41
     *
42
     * @var array
43
     */
44
    protected $middlewareGroups = [
45
        'admin' => [
46
            'admin.auth',
47
            'admin.pjax',
48
            'admin.log',
49
            'admin.bootstrap',
50
            'admin.permission',
51
        ],
52
    ];
53
54
    /**
55
     * Boot the service provider.
56
     *
57
     * @return void
58
     */
59
    public function boot()
60
    {
61
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'admin');
62
63
        if (config('admin.https') || config('admin.secure')) {
64
            \URL::forceScheme('https');
65
            $this->app['request']->server->set('HTTPS', true);
66
        }
67
68
        if (file_exists($routes = admin_path('routes.php'))) {
69
            $this->loadRoutesFrom($routes);
70
        }
71
72
        if ($this->app->runningInConsole()) {
73
            $this->publishes([__DIR__.'/../config' => config_path()], 'laravel-admin-config');
74
            $this->publishes([__DIR__.'/../resources/lang' => resource_path('lang')], 'laravel-admin-lang');
75
//            $this->publishes([__DIR__.'/../resources/views' => resource_path('views/admin')],           'laravel-admin-views');
76
            $this->publishes([__DIR__.'/../database/migrations' => database_path('migrations')], 'laravel-admin-migrations');
77
            $this->publishes([__DIR__.'/../resources/assets' => public_path('vendor/laravel-admin')], 'laravel-admin-assets');
78
        }
79
80
        //remove default feature of double encoding enable in laravel 5.6 or later.
81
        $bladeReflectionClass = new \ReflectionClass('\Illuminate\View\Compilers\BladeCompiler');
82
        if ($bladeReflectionClass->hasMethod('withoutDoubleEncoding')) {
83
            Blade::withoutDoubleEncoding();
84
        }
85
    }
86
87
    /**
88
     * Register the service provider.
89
     *
90
     * @return void
91
     */
92
    public function register()
93
    {
94
        $this->loadAdminAuthConfig();
95
96
        $this->registerRouteMiddleware();
97
98
        $this->commands($this->commands);
99
    }
100
101
    /**
102
     * Setup auth configuration.
103
     *
104
     * @return void
105
     */
106
    protected function loadAdminAuthConfig()
107
    {
108
        config(array_dot(config('admin.auth', []), 'auth.'));
109
    }
110
111
    /**
112
     * Register the route middleware.
113
     *
114
     * @return void
115
     */
116
    protected function registerRouteMiddleware()
117
    {
118
        // register route middleware.
119
        foreach ($this->routeMiddleware as $key => $middleware) {
120
            app('router')->aliasMiddleware($key, $middleware);
121
        }
122
123
        // register middleware group.
124
        foreach ($this->middlewareGroups as $key => $middleware) {
125
            app('router')->middlewareGroup($key, $middleware);
126
        }
127
    }
128
}
129