Completed
Pull Request — master (#2212)
by
unknown
02:51
created

Admin::setSubtitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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