From c45b478de23ea2c31b7cfa76606f7115dcf3d49c Mon Sep 17 00:00:00 2001 From: printempw Date: Thu, 28 Jun 2018 12:07:20 +0800 Subject: [PATCH] Add .env variables for customizing the path to load plugins from --- .env.example | 3 +++ app/Http/Controllers/PluginController.php | 6 +++--- app/Services/Plugin.php | 4 +++- app/Services/PluginManager.php | 25 +++++++++++++++-------- config/plugins.php | 25 +++++++++++++++++++++++ resources/lang/en/errors.yml | 1 + resources/lang/zh_CN/errors.yml | 3 ++- 7 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 config/plugins.php diff --git a/.env.example b/.env.example index 5bc30a62..91572c07 100644 --- a/.env.example +++ b/.env.example @@ -73,3 +73,6 @@ FS_DRIVER = local REDIS_HOST = 127.0.0.1 REDIS_PASSWORD = null REDIS_PORT = 6379 + +PLUGINS_DIR = null +PLUGINS_URL = null diff --git a/app/Http/Controllers/PluginController.php b/app/Http/Controllers/PluginController.php index a6b4c2ae..422c73eb 100644 --- a/app/Http/Controllers/PluginController.php +++ b/app/Http/Controllers/PluginController.php @@ -75,13 +75,13 @@ class PluginController extends Controller return Datatables::of($installed) ->setRowId('plugin-{{ $name }}') ->editColumn('title', function ($plugin) { - return trans($plugin->title); + return trans($plugin->title ?: 'EMPTY'); }) ->editColumn('description', function ($plugin) { - return trans($plugin->description); + return trans($plugin->description ?: 'EMPTY'); }) ->editColumn('author', function ($plugin) { - return ['author' => trans($plugin->author), 'url' => $plugin->url]; + return ['author' => trans($plugin->author ?: 'EMPTY'), 'url' => $plugin->url]; }) ->addColumn('status', function ($plugin) { return trans('admin.plugins.status.'.($plugin->isEnabled() ? 'enabled' : 'disabled')); diff --git a/app/Services/Plugin.php b/app/Services/Plugin.php index e4fd8062..da5e4d2e 100644 --- a/app/Services/Plugin.php +++ b/app/Services/Plugin.php @@ -104,7 +104,9 @@ class Plugin implements Arrayable, ArrayAccess public function assets($relativeUri) { - return url("plugins/{$this->getDirname()}/$relativeUri"); + $baseUrl = config('plugins.url') ?: url('plugins'); + + return "$baseUrl/{$this->getDirname()}/$relativeUri"; } /** diff --git a/app/Services/PluginManager.php b/app/Services/PluginManager.php index 97665264..47846652 100644 --- a/app/Services/PluginManager.php +++ b/app/Services/PluginManager.php @@ -61,31 +61,38 @@ class PluginManager $installed = []; - $resource = opendir(base_path('plugins')); + try { + $resource = opendir($this->getPluginsDir()); + } catch (\Exception $e) { + throw new PrettyPageException(trans('errors.plugins.directory', ['msg' => $e->getMessage()]), 500); + } + // traverse plugins dir while($filename = @readdir($resource)) { - if ($filename == "." || $filename == "..") + if ($filename == '.' || $filename == '..') continue; - $path = base_path('plugins')."/".$filename; + $path = $this->getPluginsDir().DIRECTORY_SEPARATOR.$filename; if (is_dir($path)) { - if (file_exists($path."/package.json")) { + $packageJsonPath = $path.DIRECTORY_SEPARATOR.'package.json'; + + if (file_exists($packageJsonPath)) { // load packages installed - $installed[$filename] = json_decode($this->filesystem->get($path."/package.json"), true); + $installed[$filename] = json_decode($this->filesystem->get($packageJsonPath), true); } } } closedir($resource); - foreach ($installed as $path => $package) { + foreach ($installed as $dirname => $package) { // Instantiates an Plugin object using the package path and package.json file. - $plugin = new Plugin($this->getPluginsDir().'/'.$path, $package); + $plugin = new Plugin($this->getPluginsDir().DIRECTORY_SEPARATOR.$dirname, $package); // Per default all plugins are installed if they are registered in composer. - $plugin->setDirname($path); + $plugin->setDirname($dirname); $plugin->setInstalled(true); $plugin->setNameSpace(Arr::get($package, 'namespace')); $plugin->setVersion(Arr::get($package, 'version')); @@ -255,7 +262,7 @@ class PluginManager */ protected function getPluginsDir() { - return $this->app->basePath().'/plugins'; + return config('plugins.directory') ?: base_path('plugins'); } } diff --git a/config/plugins.php b/config/plugins.php new file mode 100644 index 00000000..bd499d11 --- /dev/null +++ b/config/plugins.php @@ -0,0 +1,25 @@ + menv('PLUGINS_DIR'), + + /* + |-------------------------------------------------------------------------- + | Plugins Assets URL + |-------------------------------------------------------------------------- + | + | The URL to access plugin's assets (CSS, JavaScript etc.). + | Defaults to `http://site_url/plugins`. + | + */ + 'url' => menv('PLUGINS_URL'), +]; diff --git a/resources/lang/en/errors.yml b/resources/lang/en/errors.yml index f50c54b2..8254d49a 100644 --- a/resources/lang/en/errors.yml +++ b/resources/lang/en/errors.yml @@ -14,3 +14,4 @@ exception: plugins: duplicate: The plugin [:dir1] has a duplicated plugin name definition which is same to plugin [:dir2]. Please check your plugins directory, remove one of them or use another name definition. + directory: We can't approach the path for loading plugins specified by the PLUGINS_DIR in .env file. Please check your configuration. Error :msg diff --git a/resources/lang/zh_CN/errors.yml b/resources/lang/zh_CN/errors.yml index 60163f0b..f4df7a57 100644 --- a/resources/lang/zh_CN/errors.yml +++ b/resources/lang/zh_CN/errors.yml @@ -14,4 +14,5 @@ exception: 如果您是站长,请开启 .env 中的 APP_DEBUG 以查看详细信息。 plugins: - duplicate: 【插件定义重复】:dir1 目录下的插件与 :dir2 目录下的插件使用了相同的 name 定义并造成了冲突。请检查您的 plugins 目录,移除其中一个插件或者使用不同的 name 属性。 + duplicate: 【插件定义重复】:dir1 目录下的插件与 :dir2 目录下的插件使用了相同的 name 定义并造成了冲突。请检查您的插件目录,移除其中一个插件或者使用不同的 name 属性。 + directory: 配置文件 .env 中指定的插件加载目录(PLUGINS_DIR)不存在或无法打开,请检查您的配置。错误信息::msg