123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- <?php
- namespace Dcat\Admin\Extend;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Exception\RuntimeException;
- use Dcat\Admin\Support\ComposerProperty;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
- use Symfony\Component\Console\Output\NullOutput;
- abstract class ServiceProvider extends LaravelServiceProvider
- {
- /**
- * @var ComposerProperty
- */
- protected $composerProperty;
- /**
- * @var string
- */
- protected $name;
- /**
- * @var string
- */
- protected $path;
- /**
- * @var array
- */
- protected $js = [];
- /**
- * @var array
- */
- protected $css = [];
- /**
- * @var array
- */
- protected $menu = [];
- /**
- * @var array
- */
- protected $permission = [];
- /**
- * @var array
- */
- protected $menuValidationRules = [
- 'title' => 'required',
- 'path' => 'required',
- 'icon' => 'required',
- ];
- /**
- * @var array
- */
- protected $permissionValidationRules = [
- 'name' => 'required',
- 'slug' => 'required',
- 'path' => 'required',
- ];
- /**
- * @var \Symfony\Component\Console\Output\OutputInterface
- */
- public $output;
- public function __construct($app)
- {
- parent::__construct($app);
- $this->output = new NullOutput();
- }
- /**
- * {@inheritdoc}
- */
- public function boot()
- {
- if ($views = $this->getViewPath()) {
- $this->loadViewsFrom($views, $this->getName());
- }
- if ($lang = $this->getLangPath()) {
- $this->loadTranslationsFrom($lang, $this->getName());
- }
- if ($routes = $this->getRoutes()) {
- return $this->registerRoutes($routes);
- }
- $this->registerAssets();
- }
- /**
- * 获取扩展名称.
- *
- * @return string
- */
- final public function getName()
- {
- return $this->name ?: ($this->name = str_replace('/', '.', $this->composerProperty->name));
- }
- /**
- * 获取扩展包路径.
- *
- * @param string $path
- *
- * @return string
- *
- * @throws \ReflectionException
- */
- public function path(?string $path = null)
- {
- if (! $this->path) {
- $this->path = realpath(dirname((new \ReflectionClass(static::class))->getFileName()).'/..');
- if (! is_dir($this->path)) {
- throw new RuntimeException("The {$this->path} is not a directory.");
- }
- }
- $path = ltrim($path, '/');
- return $path ? $this->path.'/'.$path : $this->path;
- }
- /**
- * 判断扩展是否启用.
- *
- * @return bool
- */
- final public function enabled()
- {
- return Admin::extension()->enabled($this->getName());
- }
- /**
- * 判断扩展是否禁用.
- *
- * @return bool
- */
- final public function disabled()
- {
- return ! $this->enabled();
- }
- /**
- * 获取配置.
- *
- * @param string $key
- * @param null $default
- *
- * @return \Illuminate\Config\Repository|mixed
- */
- final public function config($key = null, $default = null)
- {
- return Admin::setting()->get($this->name);
- }
- /**
- * 导入扩展.
- */
- public function import()
- {
- $this->importMenus();
- $this->importPermissions();
- }
- /**
- * 卸载扩展.
- */
- public function uninstall()
- {
- }
- /**
- * 注册路由.
- *
- * @param $callback
- */
- public function registerRoutes($callback)
- {
- Admin::app()->routes(function ($router) use ($callback) {
- $attributes = array_merge(
- [
- 'prefix' => config('admin.route.prefix'),
- 'middleware' => config('admin.route.middleware'),
- ]
- );
- $router->group($attributes, $callback);
- });
- }
- /**
- * 获取静态资源路径.
- *
- * @return string
- */
- final public function getAssetPath()
- {
- return $this->path('resources/assets');
- }
- /**
- * 获取视图路径.
- *
- * @return string
- */
- final public function getViewPath()
- {
- return $this->path('resources/views');
- }
- /**
- * 获取语言包路径.
- *
- * @return string
- */
- final public function getLangPath()
- {
- return $this->path('resources/lang');
- }
- /**
- * 获取路由地址.
- *
- * @return string
- *
- * @throws \ReflectionException
- */
- final public function getRoutes()
- {
- $path = $this->path('src/Http/routes.php');
- return is_file($path) ? $path : null;
- }
- /**
- * 获取菜单.
- *
- * @return array
- */
- protected function menu()
- {
- return $this->menu;
- }
- /**
- * @return array
- */
- protected function permission()
- {
- return $this->permission;
- }
- /**
- * @param ComposerProperty $composerProperty
- *
- * @return $this
- */
- public function withComposerProperty(ComposerProperty $composerProperty)
- {
- $this->composerProperty = $composerProperty;
- return $this;
- }
- /**
- * 导入菜单.
- *
- * @throws \Exception
- */
- protected function importMenus()
- {
- if (! ($menu = $this->menu()) || ! $this->validateMenu($menu)) {
- return;
- }
- extract($menu);
- if ($this->checkMenu($path)) {
- $this->output->writeln("<warn>Menu [$path] already exists!</warn>");
- } else {
- $this->createMenu($title, $path, $icon);
- $this->output->writeln('<info>Import extension menu succeeded!</info>');
- }
- }
- /**
- * 导入权限.
- *
- * @throws \Exception
- */
- protected function importPermissions()
- {
- if (! $this->config('admin.permission.enable')) {
- return;
- }
- if (! ($permission = $this->permission()) || ! $this->validatePermission($permission)) {
- return;
- }
- extract($permission);
- if ($this->checkPermission($slug)) {
- $this->output->writeln("<warn>Permission [$slug] already exists!</warn>");
- } else {
- $this->createPermission($name, $slug, $path);
- $this->output->writeln('<info>Import extension permission succeeded!</info>');
- }
- }
- /**
- * 注册别名.
- */
- protected function registerAssets()
- {
- if ($this->js || $this->css) {
- Admin::asset()->alias($this->getName(), $this->js, $this->css);
- }
- }
- /**
- * 验证菜单.
- *
- * @param array $menu
- *
- * @throws \Exception
- *
- * @return bool
- */
- protected 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", Arr::flatten($validator->errors()->messages()));
- $this->output->writeln("<error>{$message}</error>");
- }
- /**
- * @param $path
- *
- * @return bool
- */
- protected function checkMenu($path)
- {
- $menuModel = config('admin.database.menu_model');
- return $result = $menuModel::where('uri', $path)->exists();
- }
- /**
- * 验证权限.
- *
- * @param array $permission
- *
- * @throws \Exception
- *
- * @return bool
- */
- protected 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", Arr::flatten($validator->errors()->messages()));
- $this->output->writeln("<error>{$message}</error>");
- }
- /**
- * 创建菜单.
- *
- * @param string $title
- * @param string $uri
- * @param string $icon
- * @param int $parentId
- */
- protected 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,
- ]);
- }
- /**
- * @param $slug
- *
- * @return bool
- */
- protected function checkPermission($slug)
- {
- $permissionModel = config('admin.database.permissions_model');
- return $permissionModel::where('slug', $slug)->exists();
- }
- /**
- * 创建权限.
- *
- * @param $name
- * @param $slug
- * @param $path
- */
- protected function createPermission($name, $slug, $path)
- {
- $permissionModel = config('admin.database.permissions_model');
- $permissionModel::create([
- 'name' => $name,
- 'slug' => $slug,
- 'http_path' => trim($path, '/'),
- ]);
- }
- }
|