Completed
Push — master ( 024746...0b49f1 )
by Song
04:00
created

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