Completed
Push — master ( 5fc65a...affc4f )
by Song
02:25
created

AdminServiceProvider::ensureHttps()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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