Completed
Push — master ( 5884a0...e773ea )
by Song
05:59 queued 03:41
created

Admin::setFavicon()   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\Controllers\AuthController;
7
use Encore\Admin\Layout\Content;
8
use Encore\Admin\Traits\HasAssets;
9
use Encore\Admin\Widgets\Navbar;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Support\Arr;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\Config;
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
     *
106
     * @return \Encore\Admin\Tree
107
     */
108
    public function tree($model, Closure $callable = null)
109
    {
110
        return new Tree($this->getModel($model), $callable);
111
    }
112
113
    /**
114
     * Build show page.
115
     *
116
     * @param $model
117
     * @param mixed $callable
118
     *
119
     * @return Show
120
     *
121
     * @deprecated since v1.6.1
122
     */
123
    public function show($model, $callable = null)
124
    {
125
        return new Show($this->getModel($model), $callable);
126
    }
127
128
    /**
129
     * @param Closure $callable
130
     *
131
     * @return \Encore\Admin\Layout\Content
132
     *
133
     * @deprecated since v1.6.1
134
     */
135
    public function content(Closure $callable = null)
136
    {
137
        return new Content($callable);
138
    }
139
140
    /**
141
     * @param $model
142
     *
143
     * @return mixed
144
     */
145
    public function getModel($model)
146
    {
147
        if ($model instanceof Model) {
148
            return $model;
149
        }
150
151
        if (is_string($model) && class_exists($model)) {
152
            return $this->getModel(new $model());
153
        }
154
155
        throw new InvalidArgumentException("$model is not a valid model");
156
    }
157
158
    /**
159
     * Left sider-bar menu.
160
     *
161
     * @return array
162
     */
163
    public function menu()
164
    {
165
        if (!empty($this->menu)) {
166
            return $this->menu;
167
        }
168
169
        $menuModel = config('admin.database.menu_model');
170
171
        return $this->menu = (new $menuModel())->toTree();
172
    }
173
174
    /**
175
     * @param array $menu
176
     *
177
     * @return array
178
     */
179
    public function menuLinks($menu = [])
180
    {
181
        if (empty($menu)) {
182
            $menu = $this->menu();
183
        }
184
185
        $links = [];
186
187
        foreach ($menu as $item) {
188
            if (!empty($item['children'])) {
189
                $links = array_merge($links, $this->menuLinks($item['children']));
190
            } else {
191
                $links[] = Arr::only($item, ['title', 'uri', 'icon']);
192
            }
193
        }
194
195
        return $links;
196
    }
197
198
    /**
199
     * Set admin title.
200
     *
201
     * @return void
202
     */
203
    public static function setTitle($title)
204
    {
205
        self::$metaTitle = $title;
206
    }
207
208
    /**
209
     * Get admin title.
210
     *
211
     * @return Config
212
     */
213
    public function title()
214
    {
215
        return self::$metaTitle ? self::$metaTitle : config('admin.title');
216
    }
217
218
    /**
219
     * Set favicon.
220
     *
221
     * @return void
222
     */
223
    public static function setFavicon($favicon)
224
    {
225
        self::$favicon = $favicon;
226
    }
227
228
    /**
229
     * Get favicon.
230
     *
231
     * @return Config
232
     */
233
    public function favicon()
234
    {
235
        return self::$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\Routing\Router $router */
293
            $router->namespace('Encore\Admin\Controllers')->group(function ($router) {
0 ignored issues
show
Bug introduced by
The method namespace() does not exist on Illuminate\Routing\Router. Did you maybe mean prependGroupNamespace()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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