Completed
Push — master ( 1537c0...f6919b )
by Song
02:34
created

AdminServiceProvider::macroRouter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 16
Ratio 80 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 16
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin;
4
5
use Encore\Admin\Layout\Content;
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\Blade;
9
use Illuminate\Support\ServiceProvider;
10
11
class AdminServiceProvider extends ServiceProvider
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $commands = [
17
        Console\AdminCommand::class,
18
        Console\MakeCommand::class,
19
        Console\ControllerCommand::class,
20
        Console\MenuCommand::class,
21
        Console\InstallCommand::class,
22
        Console\PublishCommand::class,
23
        Console\UninstallCommand::class,
24
        Console\ImportCommand::class,
25
        Console\CreateUserCommand::class,
26
        Console\ResetPasswordCommand::class,
27
        Console\ExtendCommand::class,
28
        Console\ExportSeedCommand::class,
29
        Console\MinifyCommand::class,
30
        Console\FormCommand::class,
31
        Console\PermissionCommand::class,
32
        Console\ActionCommand::class,
33
        Console\GenerateMenuCommand::class,
34
        Console\ConfigCommand::class,
35
    ];
36
37
    /**
38
     * The application's route middleware.
39
     *
40
     * @var array
41
     */
42
    protected $routeMiddleware = [
43
        'admin.auth'       => Middleware\Authenticate::class,
44
        'admin.pjax'       => Middleware\Pjax::class,
45
        'admin.log'        => Middleware\LogOperation::class,
46
        'admin.permission' => Middleware\Permission::class,
47
        'admin.bootstrap'  => Middleware\Bootstrap::class,
48
        'admin.session'    => Middleware\Session::class,
49
    ];
50
51
    /**
52
     * The application's route middleware groups.
53
     *
54
     * @var array
55
     */
56
    protected $middlewareGroups = [
57
        'admin' => [
58
            'admin.auth',
59
            'admin.pjax',
60
            'admin.log',
61
            'admin.bootstrap',
62
            'admin.permission',
63
            //            'admin.session',
64
        ],
65
    ];
66
67
    /**
68
     * Boot the service provider.
69
     *
70
     * @return void
71
     */
72
    public function boot()
73
    {
74
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'admin');
75
76
        $this->ensureHttps();
77
78
        if (file_exists($routes = admin_path('routes.php'))) {
79
            $this->loadRoutesFrom($routes);
80
        }
81
82
        $this->registerPublishing();
83
84
        $this->compatibleBlade();
85
86
        Blade::directive('box', function ($title) {
87
            return "<?php \$box = new \Encore\Admin\Widgets\Box({$title}, '";
88
        });
89
90
        Blade::directive('endbox', function ($expression) {
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
            return "'); echo \$box->render(); ?>";
92
        });
93
    }
94
95
    /**
96
     * Force to set https scheme if https enabled.
97
     *
98
     * @return void
99
     */
100
    protected function ensureHttps()
101
    {
102
        if (config('admin.https') || config('admin.secure')) {
103
            url()->forceScheme('https');
104
            $this->app['request']->server->set('HTTPS', true);
105
        }
106
    }
107
108
    /**
109
     * Register the package's publishable resources.
110
     *
111
     * @return void
112
     */
113
    protected function registerPublishing()
114
    {
115
        if ($this->app->runningInConsole()) {
116
            $this->publishes([__DIR__.'/../config' => config_path()], 'laravel-admin-config');
117
            $this->publishes([__DIR__.'/../resources/lang' => resource_path('lang')], 'laravel-admin-lang');
118
            $this->publishes([__DIR__.'/../database/migrations' => database_path('migrations')], 'laravel-admin-migrations');
119
            $this->publishes([__DIR__.'/../resources/assets' => public_path('vendor/laravel-admin')], 'laravel-admin-assets');
120
        }
121
    }
122
123
    /**
124
     * Remove default feature of double encoding enable in laravel 5.6 or later.
125
     *
126
     * @return void
127
     */
128
    protected function compatibleBlade()
129
    {
130
        $reflectionClass = new \ReflectionClass('\Illuminate\View\Compilers\BladeCompiler');
131
132
        if ($reflectionClass->hasMethod('withoutDoubleEncoding')) {
133
            Blade::withoutDoubleEncoding();
134
        }
135
    }
136
137
    /**
138
     * Extends laravel router.
139
     */
140
    protected function macroRouter()
141
    {
142 View Code Duplication
        Router::macro('content', function ($uri, $content, $options = []) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
            return $this->match(['GET', 'HEAD'], $uri, function (Content $layout) use ($content, $options) {
0 ignored issues
show
Bug introduced by
The method match() does not seem to exist on object<Encore\Admin\AdminServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
                return $layout
145
                    ->title(Arr::get($options, 'title', ' '))
146
                    ->description(Arr::get($options, 'desc', ' '))
147
                    ->body($content);
148
            });
149
        });
150
151 View Code Duplication
        Router::macro('component', function ($uri, $component, $data = [], $options = []) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
            return $this->match(['GET', 'HEAD'], $uri, function (Content $layout) use ($component, $data, $options) {
0 ignored issues
show
Bug introduced by
The method match() does not seem to exist on object<Encore\Admin\AdminServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
                return $layout
154
                    ->title(Arr::get($options, 'title', ' '))
155
                    ->description(Arr::get($options, 'desc', ' '))
156
                    ->component($component, $data);
157
            });
158
        });
159
    }
160
161
    /**
162
     * Register the service provider.
163
     *
164
     * @return void
165
     */
166
    public function register()
167
    {
168
        $this->loadAdminAuthConfig();
169
170
        $this->registerRouteMiddleware();
171
172
        $this->commands($this->commands);
173
174
        $this->macroRouter();
175
    }
176
177
    /**
178
     * Setup auth configuration.
179
     *
180
     * @return void
181
     */
182
    protected function loadAdminAuthConfig()
183
    {
184
        config(Arr::dot(config('admin.auth', []), 'auth.'));
185
    }
186
187
    /**
188
     * Register the route middleware.
189
     *
190
     * @return void
191
     */
192
    protected function registerRouteMiddleware()
193
    {
194
        // register route middleware.
195
        foreach ($this->routeMiddleware as $key => $middleware) {
196
            app('router')->aliasMiddleware($key, $middleware);
197
        }
198
199
        // register middleware group.
200
        foreach ($this->middlewareGroups as $key => $middleware) {
201
            app('router')->middlewareGroup($key, $middleware);
202
        }
203
    }
204
}
205