123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- namespace Encore\Admin;
- use Illuminate\Support\Facades\Route;
- use Illuminate\Support\Facades\Validator;
- abstract class Extension
- {
- /**
- * @var string
- */
- protected $name;
- /**
- * @var array
- */
- public $css = [];
- /**
- * @var array
- */
- public $js = [];
- /**
- * @var string
- */
- public $assets = '';
- /**
- * @var string
- */
- public $views = '';
- /**
- * @var string
- */
- public $migrations = '';
- /**
- * @var array
- */
- public $menu = [];
- /**
- * @var array
- */
- public $permission = [];
- /**
- * Extension instance.
- *
- * @var Extension
- */
- protected static $instance;
- /**
- * The menu validation rules.
- *
- * @var array
- */
- protected $menuValidationRules = [
- 'title' => 'required',
- 'path' => 'required',
- 'icon' => 'required',
- ];
- /**
- * The permission validation rules.
- *
- * @var array
- */
- protected $permissionValidationRules = [
- 'name' => 'required',
- 'slug' => 'required',
- 'path' => 'required',
- ];
- /**
- * Returns the singleton instance.
- *
- * @return self
- */
- protected static function getInstance()
- {
- $class = get_called_class();
- if (!isset(self::$instance[$class]) || !self::$instance[$class] instanceof $class) {
- self::$instance[$class] = new static();
- }
- return static::$instance[$class];
- }
- /**
- * Bootstrap this extension.
- */
- public static function boot()
- {
- $extension = static::getInstance();
- Admin::extend($extension->name, get_called_class());
- if ($extension->disabled()) {
- return false;
- }
- if (!empty($css = $extension->css())) {
- Admin::css($css);
- }
- if (!empty($js = $extension->js())) {
- Admin::js($js);
- }
- return true;
- }
- /**
- * Get the path of assets files.
- *
- * @return string
- */
- public function assets()
- {
- return $this->assets;
- }
- /**
- * Get the paths of css files.
- *
- * @return array
- */
- public function css()
- {
- return $this->css;
- }
- /**
- * Get the paths of js files.
- *
- * @return array
- */
- public function js()
- {
- return $this->js;
- }
- /**
- * Get the path of view files.
- *
- * @return string
- */
- public function views()
- {
- return $this->views;
- }
- /**
- * Get the path of migration files.
- *
- * @return string
- */
- public function migrations()
- {
- return $this->migrations;
- }
- /**
- * @return array
- */
- public function menu()
- {
- return $this->menu;
- }
- /**
- * @return array
- */
- public function permission()
- {
- return $this->permission;
- }
- /**
- * Whether the extension is enabled.
- *
- * @return bool
- */
- public function enabled()
- {
- return static::config('enable') !== false;
- }
- /**
- * Whether the extension is disabled.
- *
- * @return bool
- */
- public function disabled()
- {
- return !$this->enabled();
- }
- /**
- * Get config set in config/admin.php.
- *
- * @param string $key
- * @param null $default
- *
- * @return \Illuminate\Config\Repository|mixed
- */
- public static function config($key = null, $default = null)
- {
- $name = array_search(get_called_class(), Admin::$extensions);
- if (is_null($key)) {
- $key = sprintf('admin.extensions.%s', strtolower($name));
- } else {
- $key = sprintf('admin.extensions.%s.%s', strtolower($name), $key);
- }
- return config($key, $default);
- }
- /**
- * Import menu item and permission to laravel-admin.
- */
- public static function import()
- {
- $extension = static::getInstance();
- if ($menu = $extension->menu()) {
- if ($extension->validateMenu($menu)) {
- extract($menu);
- static::createMenu($title, $path, $icon);
- }
- }
- if ($permission = $extension->permission()) {
- if ($extension->validatePermission($permission)) {
- extract($permission);
- static::createPermission($name, $slug, $path);
- }
- }
- }
- /**
- * Validate menu fields.
- *
- * @param array $menu
- *
- * @throws \Exception
- *
- * @return bool
- */
- public function validateMenu(array $menu)
- {
- /** @var \Illuminate\Validation\Validator $validator */
- $validator = Validator::make($menu, $this->menuValidationRules);
- if ($validator->passes()) {
- return true;
- }
- $message = "Invalid menu:\r\n".implode("\r\n", array_flatten($validator->errors()->messages()));
- throw new \Exception($message);
- }
- /**
- * Validate permission fields.
- *
- * @param array $permission
- *
- * @throws \Exception
- *
- * @return bool
- */
- public function validatePermission(array $permission)
- {
- /** @var \Illuminate\Validation\Validator $validator */
- $validator = Validator::make($permission, $this->permissionValidationRules);
- if ($validator->passes()) {
- return true;
- }
- $message = "Invalid permission:\r\n".implode("\r\n", array_flatten($validator->errors()->messages()));
- throw new \Exception($message);
- }
- /**
- * Create a item in laravel-admin left side menu.
- *
- * @param string $title
- * @param string $uri
- * @param string $icon
- * @param int $parentId
- */
- protected static function createMenu($title, $uri, $icon = 'fa-bars', $parentId = 0)
- {
- $menuModel = config('admin.database.menu_model');
- $lastOrder = $menuModel::max('order');
- $menuModel::create([
- 'parent_id' => $parentId,
- 'order' => $lastOrder + 1,
- 'title' => $title,
- 'icon' => $icon,
- 'uri' => $uri,
- ]);
- }
- /**
- * Create a permission for this extension.
- *
- * @param $name
- * @param $slug
- * @param $path
- */
- protected static function createPermission($name, $slug, $path)
- {
- $permissionModel = config('admin.database.permissions_model');
- $permissionModel::create([
- 'name' => $name,
- 'slug' => $slug,
- 'http_path' => '/'.trim($path, '/'),
- ]);
- }
- /**
- * Set routes for this extension.
- *
- * @param $callback
- */
- public static function routes($callback)
- {
- $attributes = array_merge(
- [
- 'prefix' => config('admin.route.prefix'),
- 'middleware' => config('admin.route.middleware'),
- ],
- static::config('route', [])
- );
- Route::group($attributes, $callback);
- }
- }
|