Completed
Push — master ( f9866e...fa91dc )
by Song
02:51
created

src/Admin.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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