123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- namespace Encore\Admin;
- use Closure;
- use Encore\Admin\Controllers\AuthController;
- use Encore\Admin\Layout\Content;
- use Encore\Admin\Traits\HasAssets;
- use Encore\Admin\Widgets\Navbar;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Config;
- use InvalidArgumentException;
- /**
- * Class Admin.
- */
- class Admin
- {
- use HasAssets;
- /**
- * The Laravel admin version.
- *
- * @var string
- */
- const VERSION = '1.6.10';
- /**
- * @var Navbar
- */
- protected $navbar;
- /**
- * @var string
- */
- public static $metaTitle;
- /**
- * @var array
- */
- public static $extensions = [];
- /**
- * @var []Closure
- */
- public static $booting;
- /**
- * @var []Closure
- */
- public static $booted;
- /**
- * Returns the long version of Laravel-admin.
- *
- * @return string The long application version
- */
- public static function getLongVersion()
- {
- return sprintf('Laravel-admin <comment>version</comment> <info>%s</info>', self::VERSION);
- }
- /**
- * @param $model
- * @param Closure $callable
- *
- * @return \Encore\Admin\Grid
- *
- * @deprecated since v1.6.1
- */
- public function grid($model, Closure $callable)
- {
- return new Grid($this->getModel($model), $callable);
- }
- /**
- * @param $model
- * @param Closure $callable
- *
- * @return \Encore\Admin\Form
- *
- * @deprecated since v1.6.1
- */
- public function form($model, Closure $callable)
- {
- return new Form($this->getModel($model), $callable);
- }
- /**
- * Build a tree.
- *
- * @param $model
- *
- * @return \Encore\Admin\Tree
- */
- public function tree($model, Closure $callable = null)
- {
- return new Tree($this->getModel($model), $callable);
- }
- /**
- * Build show page.
- *
- * @param $model
- * @param mixed $callable
- *
- * @return Show
- *
- * @deprecated since v1.6.1
- */
- public function show($model, $callable = null)
- {
- return new Show($this->getModel($model), $callable);
- }
- /**
- * @param Closure $callable
- *
- * @return \Encore\Admin\Layout\Content
- *
- * @deprecated since v1.6.1
- */
- public function content(Closure $callable = null)
- {
- return new Content($callable);
- }
- /**
- * @param $model
- *
- * @return mixed
- */
- public function getModel($model)
- {
- if ($model instanceof Model) {
- return $model;
- }
- if (is_string($model) && class_exists($model)) {
- return $this->getModel(new $model());
- }
- throw new InvalidArgumentException("$model is not a valid model");
- }
- /**
- * Left sider-bar menu.
- *
- * @return array
- */
- public function menu()
- {
- $menuModel = config('admin.database.menu_model');
- return (new $menuModel())->toTree();
- }
- /**
- * Set admin title.
- *
- * @return void
- */
- public static function setTitle($title)
- {
- self::$metaTitle = $title;
- }
- /**
- * Get admin title.
- *
- * @return Config
- */
- public function title()
- {
- return self::$metaTitle ? self::$metaTitle : config('admin.title');
- }
- /**
- * Get current login user.
- *
- * @return mixed
- */
- public function user()
- {
- return Auth::guard('admin')->user();
- }
- /**
- * Set navbar.
- *
- * @param Closure|null $builder
- *
- * @return Navbar
- */
- public function navbar(Closure $builder = null)
- {
- if (is_null($builder)) {
- return $this->getNavbar();
- }
- call_user_func($builder, $this->getNavbar());
- }
- /**
- * Get navbar object.
- *
- * @return \Encore\Admin\Widgets\Navbar
- */
- public function getNavbar()
- {
- if (is_null($this->navbar)) {
- $this->navbar = new Navbar();
- }
- return $this->navbar;
- }
- /**
- * Register the auth routes.
- *
- * @return void
- */
- public function registerAuthRoutes()
- {
- $attributes = [
- 'prefix' => config('admin.route.prefix'),
- 'middleware' => config('admin.route.middleware'),
- ];
- app('router')->group($attributes, function ($router) {
- /* @var \Illuminate\Routing\Router $router */
- $router->namespace('Encore\Admin\Controllers')->group(function ($router) {
- /* @var \Illuminate\Routing\Router $router */
- $router->resource('auth/users', 'UserController');
- $router->resource('auth/roles', 'RoleController');
- $router->resource('auth/permissions', 'PermissionController');
- $router->resource('auth/menu', 'MenuController', ['except' => ['create']]);
- $router->resource('auth/logs', 'LogController', ['only' => ['index', 'destroy', 'show']]);
- });
- $authController = config('admin.auth.controller', AuthController::class);
- /* @var \Illuminate\Routing\Router $router */
- $router->get('auth/login', $authController.'@getLogin');
- $router->post('auth/login', $authController.'@postLogin');
- $router->get('auth/logout', $authController.'@getLogout');
- $router->get('auth/setting', $authController.'@getSetting');
- $router->put('auth/setting', $authController.'@putSetting');
- });
- }
- /**
- * Extend a extension.
- *
- * @param string $name
- * @param string $class
- *
- * @return void
- */
- public static function extend($name, $class)
- {
- static::$extensions[$name] = $class;
- }
- /**
- * @param callable $callback
- */
- public static function booting(callable $callback)
- {
- static::$booting[] = $callback;
- }
- /**
- * @param callable $callback
- */
- public static function booted(callable $callback)
- {
- static::$booted[] = $callback;
- }
- /*
- * Disable Pjax for current Request
- *
- * @return void
- */
- public function disablePjax()
- {
- if (request()->pjax()) {
- request()->headers->set('X-PJAX', false);
- }
- }
- }
|