Completed
Push — master ( 5ca9ad...782d0f )
by Song
03:18
created

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