Admin.php 6.2 KB

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