Completed
Pull Request — master (#2212)
by
unknown
02:57
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\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 Subtitle
27
     */
28
    protected $subtitle = '';
29
30
    /**
31
     * @var array
32
     */
33
    public static $script = [];
34
35
    /**
36
     * @var array
37
     */
38
    public static $css = [];
39
40
    /**
41
     * @var array
42
     */
43
    public static $js = [];
44
45
    /**
46
     * @var array
47
     */
48
    public static $extensions = [];
49
50
    /**
51
     * @param $model
52
     * @param Closure $callable
53
     *
54
     * @return \Encore\Admin\Grid
55
     */
56
    public function grid($model, Closure $callable)
57
    {
58
        return new Grid($this->getModel($model), $callable);
59
    }
60
61
    /**
62
     * @param $model
63
     * @param Closure $callable
64
     *
65
     * @return \Encore\Admin\Form
66
     */
67
    public function form($model, Closure $callable)
68
    {
69
        return new Form($this->getModel($model), $callable);
70
    }
71
72
    /**
73
     * Build a tree.
74
     *
75
     * @param $model
76
     *
77
     * @return \Encore\Admin\Tree
78
     */
79
    public function tree($model, Closure $callable = null)
80
    {
81
        return new Tree($this->getModel($model), $callable);
82
    }
83
84
    /**
85
     * @param Closure $callable
86
     *
87
     * @return \Encore\Admin\Layout\Content
88
     */
89
    public function content(Closure $callable = null)
90
    {
91
        return new Content($callable);
92
    }
93
94
    /**
95
     * @param $model
96
     *
97
     * @return mixed
98
     */
99
    public function getModel($model)
100
    {
101
        if ($model instanceof EloquentModel) {
102
            return $model;
103
        }
104
105
        if (is_string($model) && class_exists($model)) {
106
            return $this->getModel(new $model());
107
        }
108
109
        throw new InvalidArgumentException("$model is not a valid model");
110
    }
111
112
    /**
113
     * Add css or get all css.
114
     *
115
     * @param null $css
116
     *
117
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void
118
     */
119 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...
120
    {
121
        if (!is_null($css)) {
122
            self::$css = array_merge(self::$css, (array) $css);
123
124
            return;
125
        }
126
127
        $css = array_get(Form::collectFieldAssets(), 'css', []);
128
129
        static::$css = array_merge(static::$css, $css);
130
131
        return view('admin::partials.css', ['css' => array_unique(static::$css)]);
132
    }
133
134
    /**
135
     * Add js or get all js.
136
     *
137
     * @param null $js
138
     *
139
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void
140
     */
141 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...
142
    {
143
        if (!is_null($js)) {
144
            self::$js = array_merge(self::$js, (array) $js);
145
146
            return;
147
        }
148
149
        $js = array_get(Form::collectFieldAssets(), 'js', []);
150
151
        static::$js = array_merge(static::$js, $js);
152
153
        return view('admin::partials.js', ['js' => array_unique(static::$js)]);
154
    }
155
156
    /**
157
     * @param string $script
158
     *
159
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void
160
     */
161
    public static function script($script = '')
162
    {
163
        if (!empty($script)) {
164
            self::$script = array_merge(self::$script, (array) $script);
165
166
            return;
167
        }
168
169
        return view('admin::partials.script', ['script' => array_unique(self::$script)]);
170
    }
171
172
    /**
173
     * Left sider-bar menu.
174
     *
175
     * @return array
176
     */
177
    public function menu()
178
    {
179
        return (new Menu())->toTree();
180
    }
181
182
    /**
183
     * Get admin title.
184
     *
185
     * @return Config
186
     */
187
    public function title()
188
    {
189
        return config('admin.title');
190
    }
191
192
    /**
193
    * @param string $subtitle
194
    * @param string $sep
195
    *
196
    * @retuen void
197
    */
198
    public function setSubtitle($subtitle, $sep = ' - ')
199
    {
200
        $this->subtitle = $sep . $subtitle;
0 ignored issues
show
Documentation Bug introduced by
It seems like $sep . $subtitle of type string is incompatible with the declared type object<Encore\Admin\Subtitle> of property $subtitle.

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...
201
    }
202
203
    /**
204
    * @retuen string
205
    */
206
    public function getSubtitle()
207
    {
208
        return $this->subtitle;
209
    }
210
211
    /**
212
     * Get current login user.
213
     *
214
     * @return mixed
215
     */
216
    public function user()
217
    {
218
        return Auth::guard('admin')->user();
219
    }
220
221
    /**
222
     * Set navbar.
223
     *
224
     * @param Closure|null $builder
225
     *
226
     * @return Navbar
227
     */
228
    public function navbar(Closure $builder = null)
229
    {
230
        if (is_null($builder)) {
231
            return $this->getNavbar();
232
        }
233
234
        call_user_func($builder, $this->getNavbar());
235
    }
236
237
    /**
238
     * Get navbar object.
239
     *
240
     * @return \Encore\Admin\Widgets\Navbar
241
     */
242
    public function getNavbar()
243
    {
244
        if (is_null($this->navbar)) {
245
            $this->navbar = new Navbar();
246
        }
247
248
        return $this->navbar;
249
    }
250
251
    /**
252
     * Register the auth routes.
253
     *
254
     * @return void
255
     */
256
    public function registerAuthRoutes()
257
    {
258
        $attributes = [
259
            'prefix'     => config('admin.route.prefix'),
260
            'namespace'  => 'Encore\Admin\Controllers',
261
            'middleware' => config('admin.route.middleware'),
262
        ];
263
264
        Route::group($attributes, function ($router) {
265
266
            /* @var \Illuminate\Routing\Router $router */
267
            $router->group([], function ($router) {
268
269
                /* @var \Illuminate\Routing\Router $router */
270
                $router->resource('auth/users', 'UserController');
271
                $router->resource('auth/roles', 'RoleController');
272
                $router->resource('auth/permissions', 'PermissionController');
273
                $router->resource('auth/menu', 'MenuController', ['except' => ['create']]);
274
                $router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy']]);
275
            });
276
277
            $router->get('auth/login', 'AuthController@getLogin');
278
            $router->post('auth/login', 'AuthController@postLogin');
279
            $router->get('auth/logout', 'AuthController@getLogout');
280
            $router->get('auth/setting', 'AuthController@getSetting');
281
            $router->put('auth/setting', 'AuthController@putSetting');
282
        });
283
    }
284
285
    /**
286
     * Extend a extension.
287
     *
288
     * @param string $name
289
     * @param string $class
290
     *
291
     * @return void
292
     */
293
    public static function extend($name, $class)
294
    {
295
        static::$extensions[$name] = $class;
296
    }
297
}
298