| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- <?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\ServiceProvider as LaravelServiceProvider;
- use Symfony\Component\Console\Output\NullOutput;
- abstract class ServiceProvider extends LaravelServiceProvider
- {
- const TYPE_THEME = 'theme';
- /**
- * @var ComposerProperty
- */
- public $composerProperty;
- /**
- * @var string
- */
- protected $name;
- /**
- * @var string
- */
- protected $packageName;
- /**
- * @var string
- */
- protected $type;
- /**
- * @var string
- */
- protected $path;
- /**
- * @var array
- */
- protected $js = [];
- /**
- * @var array
- */
- protected $css = [];
- /**
- * @var \Symfony\Component\Console\Output\OutputInterface
- */
- public $output;
- /**
- * @var array
- */
- protected $config;
- /**
- * @var array
- */
- protected $middleware = [];
- /**
- * @var array
- */
- protected $exceptRoutes = [];
- public function __construct($app)
- {
- parent::__construct($app);
- $this->output = new NullOutput();
- }
- /**
- * {@inheritdoc}
- */
- final public function boot()
- {
- $this->autoRegister();
- if ($this->disabled()) {
- return;
- }
- $this->init();
- }
- /**
- * 初始化操作.
- *
- * @return void
- */
- public function init()
- {
- if ($views = $this->getViewPath()) {
- $this->loadViewsFrom($views, $this->getName());
- }
- if ($lang = $this->getLangPath()) {
- $this->loadTranslationsFrom($lang, $this->getName());
- }
- if ($routes = $this->getRoutes()) {
- $this->registerRoutes($routes);
- }
- if ($this->middleware) {
- $this->addMiddleware();
- }
- if ($this->exceptRoutes) {
- $this->addExceptRoutes();
- }
- $this->aliasAssets();
- }
- /**
- * 自动注册扩展.
- */
- protected function autoRegister()
- {
- if (! $this->getName()) {
- return;
- }
- Admin::extension()->addExtension($this);
- }
- /**
- * 获取扩展名称.
- *
- * @return string|void
- */
- final public function getName()
- {
- return $this->name ?: ($this->name = str_replace('/', '.', $this->getPackageName()));
- }
- /**
- * 获取包名.
- *
- * @return string|void
- */
- final public function getPackageName()
- {
- if (! $this->packageName) {
- if (! $this->composerProperty) {
- return;
- }
- $this->packageName = $this->composerProperty->name;
- }
- return $this->packageName;
- }
- /**
- * 获取插件类型.
- *
- * @return string
- */
- public function getType()
- {
- return $this->type;
- }
- /**
- * 获取当前已安装版本.
- *
- * @return string
- */
- final public function getVersion()
- {
- return Admin::extension()->versionManager()->getCurrentVersion($this);
- }
- /**
- * 获取当前最新版本.
- *
- * @return string
- */
- final public function getLatestVersion()
- {
- return Admin::extension()->versionManager()->getFileVersions($this);
- }
- /**
- * 获取当前本地最新版本.
- *
- * @return string
- */
- final public function getLocalLatestVersion()
- {
- return last(
- array_keys(Admin::extension()->versionManager()->getFileVersions($this))
- );
- }
- /**
- * 获取扩展包路径.
- *
- * @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 mixed
- */
- final public function config($key = null, $default = null)
- {
- if ($this->config === null) {
- $this->initConfig();
- }
- if (is_array($key)) {
- $this->saveConfig($key);
- return;
- }
- if ($key === null) {
- return $this->config;
- }
- return Arr::get($this->config, $key, $default);
- }
- /**
- * 保存配置.
- *
- * @param array $config
- */
- public function saveConfig(array $config)
- {
- $this->config = array_merge($this->config, $config);
- Admin::setting()->save([$this->getConfigKey() => $this->serializeConfig($this->config)]);
- }
- /**
- * 初始化配置.
- */
- protected function initConfig()
- {
- $this->config = Admin::setting()->get($this->getConfigKey());
- $this->config = $this->config ? $this->unserializeConfig($this->config) : [];
- }
- /**
- * 更新扩展.
- */
- public function update()
- {
- }
- /**
- * 卸载扩展.
- */
- public function uninstall()
- {
- }
- /**
- * 发布静态资源.
- */
- public function publishable()
- {
- if ($assets = $this->getAssetPath()) {
- $this->publishes([
- $assets => $this->getPublishsPath(),
- ], $this->getName());
- }
- }
- /**
- * 获取资源发布路径.
- *
- * @return string
- */
- protected function getPublishsPath()
- {
- return public_path(
- Admin::asset()->getRealPath('@extension/'.str_replace('.', '/', $this->getName()))
- );
- }
- /**
- * 注册路由.
- *
- * @param $callback
- */
- public function registerRoutes($callback)
- {
- Admin::app()->routes(function ($router) use ($callback) {
- $router->group([
- 'prefix' => config('admin.route.prefix'),
- 'middleware' => config('admin.route.middleware'),
- ], $callback);
- });
- }
- /**
- * 注册中间件.
- */
- protected function addMiddleware()
- {
- $middleware = (array) config('admin.route.middleware');
- $before = $this->middleware['before'] ?? [];
- $middle = $this->middleware['middle'] ?? [];
- $after = $this->middleware['after'] ?? [];
- $this->mixMiddleware($middle);
- config([
- 'admin.route.middleware' => array_merge((array) $before, $middleware, (array) $after),
- ]);
- }
- protected function mixMiddleware(array $middle)
- {
- Admin::mixMiddlewareGroup($middle);
- }
- /**
- * 配置需要跳过权限认证和登录认证的路由.
- */
- protected function addExceptRoutes()
- {
- if (! empty($this->exceptRoutes['permission'])) {
- Admin::context()->addMany('permission.except', (array) $this->exceptRoutes['permission']);
- }
- if (! empty($this->exceptRoutes['auth'])) {
- Admin::context()->addMany('auth.except', (array) $this->exceptRoutes['auth']);
- }
- }
- /**
- * 获取静态资源路径.
- *
- * @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;
- }
- /**
- * @param ComposerProperty $composerProperty
- *
- * @return $this
- */
- public function withComposerProperty(ComposerProperty $composerProperty)
- {
- $this->composerProperty = $composerProperty;
- return $this;
- }
- /**
- * 获取或保存配置.
- *
- * @param string $key
- * @param string $value
- *
- * @return mixed
- */
- public static function setting($key = null, $value = null)
- {
- $extension = static::instance();
- if ($extension && $extension instanceof ServiceProvider) {
- return $extension->config($key, $value);
- }
- }
- /**
- * 翻译.
- *
- * @param string $key
- * @param array $replace
- * @param null $locale
- *
- * @return array|string|null
- */
- public static function trans($key, $replace = [], $locale = null)
- {
- return trans(static::instance()->getName().'::'.$key, $replace, $locale);
- }
- /**
- * 获取自身实例.
- *
- * @return $this
- */
- public static function instance()
- {
- return app(static::class);
- }
- /**
- * 注册别名.
- */
- protected function aliasAssets()
- {
- $asset = Admin::asset();
- // 注册静态资源路径别名
- $asset->alias($this->getName().'.path', '@extension/'.$this->getPackageName());
- if ($this->js || $this->css) {
- $asset->alias($this->getName(), [
- 'js' => $this->formatAssetFiles($this->js),
- 'css' => $this->formatAssetFiles($this->css),
- ]);
- }
- }
- /**
- * @param string|array $files
- *
- * @return mixed
- */
- protected function formatAssetFiles($files)
- {
- if (is_array($files)) {
- return array_map([$this, 'formatAssetFiles'], $files);
- }
- return '@'.$this->getName().'.path/'.trim($files, '/');
- }
- /**
- * 配置key.
- *
- * @return mixed
- */
- protected function getConfigKey()
- {
- return str_replace('.', ':', $this->getName());
- }
- /**
- * @param $config
- *
- * @return false|string
- */
- protected function serializeConfig($config)
- {
- return json_encode($config);
- }
- /**
- * @param $config
- *
- * @return array
- */
- protected function unserializeConfig($config)
- {
- return json_decode($config, true);
- }
- }
|