Completed
Push — master ( fad484...3eeff7 )
by Song
02:31
created

Admin::navbar()   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 1
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 $booting;
57
58
    /**
59
     * @var Closure
60
     */
61
    public static $booted;
62
63
    /**
64
     * Returns the long version of Laravel-admin.
65
     *
66
     * @return string The long application version
67
     */
68
    public static function getLongVersion()
69
    {
70
        return sprintf('Laravel-admin <comment>version</comment> <info>%s</info>', self::VERSION);
71
    }
72
73
    /**
74
     * @param $model
75
     * @param Closure $callable
76
     *
77
     * @return \Encore\Admin\Grid
78
     */
79
    public function grid($model, Closure $callable)
80
    {
81
        return new Grid($this->getModel($model), $callable);
82
    }
83
84
    /**
85
     * @param $model
86
     * @param Closure $callable
87
     *
88
     * @return \Encore\Admin\Form
89
     */
90
    public function form($model, Closure $callable)
91
    {
92
        return new Form($this->getModel($model), $callable);
93
    }
94
95
    /**
96
     * Build a tree.
97
     *
98
     * @param $model
99
     *
100
     * @return \Encore\Admin\Tree
101
     */
102
    public function tree($model, Closure $callable = null)
103
    {
104
        return new Tree($this->getModel($model), $callable);
105
    }
106
107
    /**
108
     * Build show page.
109
     *
110
     * @param $model
111
     * @param mixed $callable
112
     *
113
     * @return Show
114
     */
115
    public function show($model, $callable = null)
116
    {
117
        return new Show($this->getModel($model), $callable);
118
    }
119
120
    /**
121
     * @param Closure $callable
122
     *
123
     * @return \Encore\Admin\Layout\Content
124
     */
125
    public function content(Closure $callable = null)
126
    {
127
        return new Content($callable);
128
    }
129
130
    /**
131
     * @param $model
132
     *
133
     * @return mixed
134
     */
135
    public function getModel($model)
136
    {
137
        if ($model instanceof EloquentModel) {
138
            return $model;
139
        }
140
141
        if (is_string($model) && class_exists($model)) {
142
            return $this->getModel(new $model());
143
        }
144
145
        throw new InvalidArgumentException("$model is not a valid model");
146
    }
147
148
    /**
149
     * Add css or get all css.
150
     *
151
     * @param null $css
152
     *
153
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void
154
     */
155 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...
156
    {
157
        if (!is_null($css)) {
158
            self::$css = array_merge(self::$css, (array) $css);
159
160
            return;
161
        }
162
163
        static::$css = array_merge(static::$css, (array) $css);
164
165
        return view('admin::partials.css', ['css' => array_unique(static::$css)]);
166
    }
167
168
    /**
169
     * Add js or get all js.
170
     *
171
     * @param null $js
172
     *
173
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void
174
     */
175 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...
176
    {
177
        if (!is_null($js)) {
178
            self::$js = array_merge(self::$js, (array) $js);
179
180
            return;
181
        }
182
183
        static::$js = array_merge(static::$js, (array) $js);
184
185
        return view('admin::partials.js', ['js' => array_unique(static::$js)]);
186
    }
187
188
    /**
189
     * @param string $script
190
     *
191
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void
192
     */
193
    public static function script($script = '')
194
    {
195
        if (!empty($script)) {
196
            self::$script = array_merge(self::$script, (array) $script);
197
198
            return;
199
        }
200
201
        return view('admin::partials.script', ['script' => array_unique(self::$script)]);
202
    }
203
204
    /**
205
     * Left sider-bar menu.
206
     *
207
     * @return array
208
     */
209
    public function menu()
210
    {
211
        return (new Menu())->toTree();
212
    }
213
214
    /**
215
     * Get admin title.
216
     *
217
     * @return Config
218
     */
219
    public function title()
220
    {
221
        return config('admin.title');
222
    }
223
224
    /**
225
     * Get current login user.
226
     *
227
     * @return mixed
228
     */
229
    public function user()
230
    {
231
        return Auth::guard('admin')->user();
232
    }
233
234
    /**
235
     * Set navbar.
236
     *
237
     * @param Closure|null $builder
238
     *
239
     * @return Navbar
240
     */
241
    public function navbar(Closure $builder = null)
242
    {
243
        if (is_null($builder)) {
244
            return $this->getNavbar();
245
        }
246
247
        call_user_func($builder, $this->getNavbar());
248
    }
249
250
    /**
251
     * Get navbar object.
252
     *
253
     * @return \Encore\Admin\Widgets\Navbar
254
     */
255
    public function getNavbar()
256
    {
257
        if (is_null($this->navbar)) {
258
            $this->navbar = new Navbar();
259
        }
260
261
        return $this->navbar;
262
    }
263
264
    /**
265
     * Register the auth routes.
266
     *
267
     * @return void
268
     */
269
    public function registerAuthRoutes()
270
    {
271
        $attributes = [
272
            'prefix'     => config('admin.route.prefix'),
273
            'namespace'  => 'Encore\Admin\Controllers',
274
            'middleware' => config('admin.route.middleware'),
275
        ];
276
277
        Route::group($attributes, function ($router) {
278
279
            /* @var \Illuminate\Routing\Router $router */
280
            $router->group([], function ($router) {
281
282
                /* @var \Illuminate\Routing\Router $router */
283
                $router->resource('auth/users', 'UserController');
284
                $router->resource('auth/roles', 'RoleController');
285
                $router->resource('auth/permissions', 'PermissionController');
286
                $router->resource('auth/menu', 'MenuController', ['except' => ['create']]);
287
                $router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']]);
288
            });
289
290
            $router->get('auth/login', 'AuthController@getLogin');
291
            $router->post('auth/login', 'AuthController@postLogin');
292
            $router->get('auth/logout', 'AuthController@getLogout');
293
            $router->get('auth/setting', 'AuthController@getSetting');
294
            $router->put('auth/setting', 'AuthController@putSetting');
295
        });
296
    }
297
298
    /**
299
     * Extend a extension.
300
     *
301
     * @param string $name
302
     * @param string $class
303
     *
304
     * @return void
305
     */
306
    public static function extend($name, $class)
307
    {
308
        static::$extensions[$name] = $class;
309
    }
310
311
    /**
312
     * @param callable $callback
313
     */
314
    public static function booting(callable $callback)
315
    {
316
        static::$booting = $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 $booting.

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...
317
    }
318
319
    /**
320
     * @param callable $callback
321
     */
322
    public static function booted(callable $callback)
323
    {
324
        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...
325
    }
326
327
    /*
328
     * Disable Pjax for current Request
329
     *
330
     * @return void
331
     */
332
    public function disablePjax()
333
    {
334
        $request = Request::instance();
335
336
        if ($request->pjax()) {
337
            $request->headers->set('X-PJAX', false);
338
        }
339
    }
340
}
341