Completed
Push — master ( b743eb...6e309a )
by Song
02:57
created

Admin::booting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
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\Controllers\AuthController;
8
use Encore\Admin\Layout\Content;
9
use Encore\Admin\Traits\HasAssets;
10
use Encore\Admin\Widgets\Navbar;
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Support\Arr;
13
use Illuminate\Support\Facades\Auth;
14
use InvalidArgumentException;
15
16
/**
17
 * Class Admin.
18
 */
19
class Admin
20
{
21
    use HasAssets;
22
23
    /**
24
     * The Laravel admin version.
25
     *
26
     * @var string
27
     */
28
    const VERSION = '1.6.15';
29
30
    /**
31
     * @var Navbar
32
     */
33
    protected $navbar;
34
35
    /**
36
     * @var array
37
     */
38
    protected $menu = [];
39
40
    /**
41
     * @var string
42
     */
43
    public static $metaTitle;
44
45
    /**
46
     * @var string
47
     */
48
    public static $favicon;
49
50
    /**
51
     * @var array
52
     */
53
    public static $extensions = [];
54
55
    /**
56
     * @var []Closure
57
     */
58
    protected static $bootingCallbacks = [];
59
60
    /**
61
     * @var []Closure
62
     */
63
    protected static $bootedCallbacks = [];
64
65
    /**
66
     * Returns the long version of Laravel-admin.
67
     *
68
     * @return string The long application version
69
     */
70
    public static function getLongVersion()
71
    {
72
        return sprintf('Laravel-admin <comment>version</comment> <info>%s</info>', self::VERSION);
73
    }
74
75
    /**
76
     * @param $model
77
     * @param Closure $callable
78
     *
79
     * @return \Encore\Admin\Grid
80
     *
81
     * @deprecated since v1.6.1
82
     */
83
    public function grid($model, Closure $callable)
84
    {
85
        return new Grid($this->getModel($model), $callable);
86
    }
87
88
    /**
89
     * @param $model
90
     * @param Closure $callable
91
     *
92
     * @return \Encore\Admin\Form
93
     *
94
     *  @deprecated since v1.6.1
95
     */
96
    public function form($model, Closure $callable)
97
    {
98
        return new Form($this->getModel($model), $callable);
99
    }
100
101
    /**
102
     * Build a tree.
103
     *
104
     * @param $model
105
     * @param Closure|null $callable
106
     *
107
     * @return \Encore\Admin\Tree
108
     */
109
    public function tree($model, Closure $callable = null)
110
    {
111
        return new Tree($this->getModel($model), $callable);
112
    }
113
114
    /**
115
     * Build show page.
116
     *
117
     * @param $model
118
     * @param mixed $callable
119
     *
120
     * @return Show
121
     *
122
     * @deprecated since v1.6.1
123
     */
124
    public function show($model, $callable = null)
125
    {
126
        return new Show($this->getModel($model), $callable);
127
    }
128
129
    /**
130
     * @param Closure $callable
131
     *
132
     * @return \Encore\Admin\Layout\Content
133
     *
134
     * @deprecated since v1.6.1
135
     */
136
    public function content(Closure $callable = null)
137
    {
138
        return new Content($callable);
139
    }
140
141
    /**
142
     * @param $model
143
     *
144
     * @return mixed
145
     */
146
    public function getModel($model)
147
    {
148
        if ($model instanceof Model) {
149
            return $model;
150
        }
151
152
        if (is_string($model) && class_exists($model)) {
153
            return $this->getModel(new $model());
154
        }
155
156
        throw new InvalidArgumentException("$model is not a valid model");
157
    }
158
159
    /**
160
     * Left sider-bar menu.
161
     *
162
     * @return array
163
     */
164
    public function menu()
165
    {
166
        if (!empty($this->menu)) {
167
            return $this->menu;
168
        }
169
170
        $menuClass = config('admin.database.menu_model');
171
172
        /** @var Menu $menuModel */
173
        $menuModel = new $menuClass;
174
175
        return $this->menu = $menuModel->toTree();
176
    }
177
178
    /**
179
     * @param array $menu
180
     *
181
     * @return array
182
     */
183
    public function menuLinks($menu = [])
184
    {
185
        if (empty($menu)) {
186
            $menu = $this->menu();
187
        }
188
189
        $links = [];
190
191
        foreach ($menu as $item) {
192
            if (!empty($item['children'])) {
193
                $links = array_merge($links, $this->menuLinks($item['children']));
194
            } else {
195
                $links[] = Arr::only($item, ['title', 'uri', 'icon']);
196
            }
197
        }
198
199
        return $links;
200
    }
201
202
    /**
203
     * Set admin title.
204
     *
205
     * @param string $title
206
     *
207
     * @return void
208
     */
209
    public static function setTitle($title)
210
    {
211
        self::$metaTitle = $title;
212
    }
213
214
    /**
215
     * Get admin title.
216
     *
217
     * @return string
218
     */
219
    public function title()
220
    {
221
        return self::$metaTitle ? self::$metaTitle : config('admin.title');
222
    }
223
224
    /**
225
     * @param null|string $favicon
226
     *
227
     * @return string|void
228
     */
229
    public function favicon($favicon = null)
230
    {
231
        if (is_null($favicon)) {
232
            return static::$favicon;
233
        }
234
235
        static::$favicon = $favicon;
236
    }
237
238
    /**
239
     * Get current login user.
240
     *
241
     * @return mixed
242
     */
243
    public function user()
244
    {
245
        return Auth::guard('admin')->user();
246
    }
247
248
    /**
249
     * Set navbar.
250
     *
251
     * @param Closure|null $builder
252
     *
253
     * @return Navbar
254
     */
255
    public function navbar(Closure $builder = null)
256
    {
257
        if (is_null($builder)) {
258
            return $this->getNavbar();
259
        }
260
261
        call_user_func($builder, $this->getNavbar());
262
    }
263
264
    /**
265
     * Get navbar object.
266
     *
267
     * @return \Encore\Admin\Widgets\Navbar
268
     */
269
    public function getNavbar()
270
    {
271
        if (is_null($this->navbar)) {
272
            $this->navbar = new Navbar();
273
        }
274
275
        return $this->navbar;
276
    }
277
278
    /**
279
     * Register the auth routes.
280
     *
281
     * @return void
282
     */
283
    public function registerAuthRoutes()
284
    {
285
        $attributes = [
286
            'prefix'     => config('admin.route.prefix'),
287
            'middleware' => config('admin.route.middleware'),
288
        ];
289
290
        app('router')->group($attributes, function ($router) {
291
292
            /* @var \Illuminate\Support\Facades\Route $router */
293
            $router->namespace('Encore\Admin\Controllers')->group(function ($router) {
294
295
                /* @var \Illuminate\Routing\Router $router */
296
                $router->resource('auth/users', 'UserController')->names('admin.auth.users');
297
                $router->resource('auth/roles', 'RoleController')->names('admin.auth.roles');
298
                $router->resource('auth/permissions', 'PermissionController')->names('admin.auth.permissions');
299
                $router->resource('auth/menu', 'MenuController', ['except' => ['create']])->names('admin.auth.menu');
300
                $router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']])->names('admin.auth.logs');
301
            });
302
303
            $authController = config('admin.auth.controller', AuthController::class);
304
305
            /* @var \Illuminate\Routing\Router $router */
306
            $router->get('auth/login', $authController.'@getLogin')->name('admin.login');
307
            $router->post('auth/login', $authController.'@postLogin');
308
            $router->get('auth/logout', $authController.'@getLogout')->name('admin.logout');
309
            $router->get('auth/setting', $authController.'@getSetting')->name('admin.setting');
310
            $router->put('auth/setting', $authController.'@putSetting');
311
        });
312
    }
313
314
    /**
315
     * Extend a extension.
316
     *
317
     * @param string $name
318
     * @param string $class
319
     *
320
     * @return void
321
     */
322
    public static function extend($name, $class)
323
    {
324
        static::$extensions[$name] = $class;
325
    }
326
327
    /**
328
     * @param callable $callback
329
     */
330
    public static function booting(callable $callback)
331
    {
332
        static::$bootingCallbacks[] = $callback;
333
    }
334
335
    /**
336
     * @param callable $callback
337
     */
338
    public static function booted(callable $callback)
339
    {
340
        static::$bootedCallbacks[] = $callback;
341
    }
342
343
    /**
344
     * Bootstrap the admin application.
345
     */
346
    public function bootstrap()
347
    {
348
        $this->fireBootingCallbacks();
349
350
        Form::registerBuiltinFields();
351
352
        Grid::registerColumnDisplayer();
353
354
        Grid\Filter::registerFilters();
355
356
        require config('admin.bootstrap', admin_path('bootstrap.php'));
357
358
        $assets = Form::collectFieldAssets();
359
360
        self::css($assets['css']);
361
        self::js($assets['js']);
362
363
        $this->fireBootedCallbacks();
364
    }
365
366
    /**
367
     * Call the booting callbacks for the admin application.
368
     */
369
    protected function fireBootingCallbacks()
370
    {
371
        foreach (static::$bootingCallbacks as $callable) {
372
            call_user_func($callable);
373
        }
374
    }
375
376
    /**
377
     * Call the booted callbacks for the admin application.
378
     */
379
    protected function fireBootedCallbacks()
380
    {
381
        foreach (static::$bootedCallbacks as $callable) {
382
            call_user_func($callable);
383
        }
384
    }
385
386
    /*
387
     * Disable Pjax for current Request
388
     *
389
     * @return void
390
     */
391
    public function disablePjax()
392
    {
393
        if (request()->pjax()) {
394
            request()->headers->set('X-PJAX', false);
395
        }
396
    }
397
}
398