Completed
Push — master ( 1c7dfc...0a1e1d )
by Song
02:23
created

Admin::disablePjax()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin;
4
5
use Closure;
6
use Encore\Admin\Auth\Database\Menu;
7
use Encore\Admin\Layout\Content;
8
use Encore\Admin\Widgets\Navbar;
9
use Illuminate\Database\Eloquent\Model as EloquentModel;
10
use Illuminate\Support\Facades\Auth;
11
use Illuminate\Support\Facades\Config;
12
use Illuminate\Support\Facades\Request;
13
use Illuminate\Support\Facades\Route;
14
use InvalidArgumentException;
15
16
/**
17
 * Class Admin.
18
 */
19
class Admin
20
{
21
    /**
22
     * The Laravel admin version.
23
     *
24
     * @var string
25
     */
26
    const VERSION = '1.5.19';
27
28
    /**
29
     * @var Navbar
30
     */
31
    protected $navbar;
32
33
    /**
34
     * @var array
35
     */
36
    public static $script = [];
37
38
    /**
39
     * @var array
40
     */
41
    public static $css = [];
42
43
    /**
44
     * @var array
45
     */
46
    public static $js = [];
47
48
    /**
49
     * @var array
50
     */
51
    public static $extensions = [];
52
53
    /**
54
     * @var Closure
55
     */
56
    public static $booted;
57
58
    /**
59
     * Returns the long version of Laravel-admin.
60
     *
61
     * @return string The long application version
62
     */
63
    public static function getLongVersion()
64
    {
65
        return sprintf('Laravel-admin <comment>version</comment> <info>%s</info>', self::VERSION);
66
    }
67
68
    /**
69
     * @param $model
70
     * @param Closure $callable
71
     *
72
     * @return \Encore\Admin\Grid
73
     */
74
    public function grid($model, Closure $callable)
75
    {
76
        return new Grid($this->getModel($model), $callable);
77
    }
78
79
    /**
80
     * @param $model
81
     * @param Closure $callable
82
     *
83
     * @return \Encore\Admin\Form
84
     */
85
    public function form($model, Closure $callable)
86
    {
87
        return new Form($this->getModel($model), $callable);
88
    }
89
90
    /**
91
     * Build a tree.
92
     *
93
     * @param $model
94
     *
95
     * @return \Encore\Admin\Tree
96
     */
97
    public function tree($model, Closure $callable = null)
98
    {
99
        return new Tree($this->getModel($model), $callable);
100
    }
101
102
    /**
103
     * Build show page.
104
     *
105
     * @param $model
106
     * @param mixed $callable
107
     *
108
     * @return Show
109
     */
110
    public function show($model, $callable = null)
111
    {
112
        return new Show($this->getModel($model), $callable);
113
    }
114
115
    /**
116
     * @param Closure $callable
117
     *
118
     * @return \Encore\Admin\Layout\Content
119
     */
120
    public function content(Closure $callable = null)
121
    {
122
        return new Content($callable);
123
    }
124
125
    /**
126
     * @param $model
127
     *
128
     * @return mixed
129
     */
130
    public function getModel($model)
131
    {
132
        if ($model instanceof EloquentModel) {
133
            return $model;
134
        }
135
136
        if (is_string($model) && class_exists($model)) {
137
            return $this->getModel(new $model());
138
        }
139
140
        throw new InvalidArgumentException("$model is not a valid model");
141
    }
142
143
    /**
144
     * Add css or get all css.
145
     *
146
     * @param null $css
147
     *
148
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void
149
     */
150 View Code Duplication
    public static function css($css = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
151
    {
152
        if (!is_null($css)) {
153
            self::$css = array_merge(self::$css, (array) $css);
154
155
            return;
156
        }
157
158
        static::$css = array_merge(static::$css, (array) $css);
159
160
        return view('admin::partials.css', ['css' => array_unique(static::$css)]);
161
    }
162
163
    /**
164
     * Add js or get all js.
165
     *
166
     * @param null $js
167
     *
168
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void
169
     */
170 View Code Duplication
    public static function js($js = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
171
    {
172
        if (!is_null($js)) {
173
            self::$js = array_merge(self::$js, (array) $js);
174
175
            return;
176
        }
177
178
        static::$js = array_merge(static::$js, (array) $js);
179
180
        return view('admin::partials.js', ['js' => array_unique(static::$js)]);
181
    }
182
183
    /**
184
     * @param string $script
185
     *
186
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void
187
     */
188
    public static function script($script = '')
189
    {
190
        if (!empty($script)) {
191
            self::$script = array_merge(self::$script, (array) $script);
192
193
            return;
194
        }
195
196
        return view('admin::partials.script', ['script' => array_unique(self::$script)]);
197
    }
198
199
    /**
200
     * Left sider-bar menu.
201
     *
202
     * @return array
203
     */
204
    public function menu()
205
    {
206
        return (new Menu())->toTree();
207
    }
208
209
    /**
210
     * Get admin title.
211
     *
212
     * @return Config
213
     */
214
    public function title()
215
    {
216
        return config('admin.title');
217
    }
218
219
    /**
220
     * Get current login user.
221
     *
222
     * @return mixed
223
     */
224
    public function user()
225
    {
226
        return Auth::guard('admin')->user();
227
    }
228
229
    /**
230
     * Set navbar.
231
     *
232
     * @param Closure|null $builder
233
     *
234
     * @return Navbar
235
     */
236
    public function navbar(Closure $builder = null)
237
    {
238
        if (is_null($builder)) {
239
            return $this->getNavbar();
240
        }
241
242
        call_user_func($builder, $this->getNavbar());
243
    }
244
245
    /**
246
     * Get navbar object.
247
     *
248
     * @return \Encore\Admin\Widgets\Navbar
249
     */
250
    public function getNavbar()
251
    {
252
        if (is_null($this->navbar)) {
253
            $this->navbar = new Navbar();
254
        }
255
256
        return $this->navbar;
257
    }
258
259
    /**
260
     * Register the auth routes.
261
     *
262
     * @return void
263
     */
264
    public function registerAuthRoutes()
265
    {
266
        $attributes = [
267
            'prefix'     => config('admin.route.prefix'),
268
            'namespace'  => 'Encore\Admin\Controllers',
269
            'middleware' => config('admin.route.middleware'),
270
        ];
271
272
        Route::group($attributes, function ($router) {
273
274
            /* @var \Illuminate\Routing\Router $router */
275
            $router->group([], function ($router) {
276
277
                /* @var \Illuminate\Routing\Router $router */
278
                $router->resource('auth/users', 'UserController');
279
                $router->resource('auth/roles', 'RoleController');
280
                $router->resource('auth/permissions', 'PermissionController');
281
                $router->resource('auth/menu', 'MenuController', ['except' => ['create']]);
282
                $router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']]);
283
            });
284
285
            $router->get('auth/login', 'AuthController@getLogin');
286
            $router->post('auth/login', 'AuthController@postLogin');
287
            $router->get('auth/logout', 'AuthController@getLogout');
288
            $router->get('auth/setting', 'AuthController@getSetting');
289
            $router->put('auth/setting', 'AuthController@putSetting');
290
        });
291
    }
292
293
    /**
294
     * Extend a extension.
295
     *
296
     * @param string $name
297
     * @param string $class
298
     *
299
     * @return void
300
     */
301
    public static function extend($name, $class)
302
    {
303
        static::$extensions[$name] = $class;
304
    }
305
306
    /**
307
     * @param callable $callback
308
     */
309
    public static function booted(callable $callback)
310
    {
311
        static::$booted = $callback;
0 ignored issues
show
Documentation Bug introduced by
It seems like $callback of type callable is incompatible with the declared type object<Closure> of property $booted.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
312
    }
313
314
    /*
315
     * Disable Pjax for current Request
316
     *
317
     * @return void
318
     */
319
    public function disablePjax()
320
    {
321
        $request = Request::instance();
322
323
        if ($request->pjax()) {
324
            $request->headers->set('X-PJAX', false);
325
        }
326
    }
327
}
328