diff --git a/beike/Shop/Providers/PluginServiceProvider.php b/beike/Shop/Providers/PluginServiceProvider.php index 1dc8e435..7bb41a10 100644 --- a/beike/Shop/Providers/PluginServiceProvider.php +++ b/beike/Shop/Providers/PluginServiceProvider.php @@ -17,6 +17,8 @@ use Illuminate\Support\ServiceProvider; class PluginServiceProvider extends ServiceProvider { + private string $pluginBasePath = ''; + /** * Register the application services. * @@ -36,30 +38,74 @@ class PluginServiceProvider extends ServiceProvider public function boot() { $manager = app('plugin'); - $plugins = $manager->getPlugins(); $bootstraps = $manager->getEnabledBootstraps(); - $pluginBasePath = base_path('plugins'); + $this->pluginBasePath = base_path('plugins'); foreach ($bootstraps as $bootstrap) { - $filePath = $bootstrap['file']; $pluginCode = $bootstrap['code']; - if (file_exists($filePath)) { - $className = "Plugin\\{$pluginCode}\\Bootstrap"; - if (method_exists($className, 'boot')) { - (new $className)->boot(); - } - } - - $routePath = "{$pluginBasePath}/{$pluginCode}/routes.php"; - if (file_exists($routePath)) { - Route::prefix('plugin') - ->middleware('web') - ->group(function () use ($routePath) { - $this->loadRoutesFrom($routePath); - }); - } - - $this->loadViewsFrom("{$pluginBasePath}/{$pluginCode}/Views", $pluginCode); + $this->bootPlugin($bootstrap); + $this->loadRoutes($pluginCode); + $this->loadTranslations($pluginCode); + $this->loadViews($pluginCode); } } + + + /** + * 调用插件 Bootstrap::boot() + * + * @param $bootstrap + */ + private function bootPlugin($bootstrap) + { + $filePath = $bootstrap['file']; + $pluginCode = $bootstrap['code']; + if (file_exists($filePath)) { + $className = "Plugin\\{$pluginCode}\\Bootstrap"; + if (method_exists($className, 'boot')) { + (new $className)->boot(); + } + } + } + + + /** + * 加载插件路由 + * + * @param $pluginCode + */ + private function loadRoutes($pluginCode) + { + $pluginBasePath = $this->pluginBasePath; + $routePath = "{$pluginBasePath}/{$pluginCode}/routes.php"; + if (file_exists($routePath)) { + Route::prefix('plugin') + ->middleware('web') + ->group(function () use ($routePath) { + $this->loadRoutesFrom($routePath); + }); + } + } + + + /** + * 加载多语言 + */ + private function loadTranslations($pluginCode) + { + $pluginBasePath = $this->pluginBasePath; + $this->loadTranslationsFrom("{$pluginBasePath}/{$pluginCode}/Lang", $pluginCode); + } + + + /** + * 加载模板目录 + * + * @param $pluginCode + */ + private function loadViews($pluginCode) + { + $pluginBasePath = $this->pluginBasePath; + $this->loadViewsFrom("{$pluginBasePath}/{$pluginCode}/Views", $pluginCode); + } } diff --git a/plugins/HeaderMenu/Bootstrap.php b/plugins/HeaderMenu/Bootstrap.php index d066e2a2..19c5db29 100644 --- a/plugins/HeaderMenu/Bootstrap.php +++ b/plugins/HeaderMenu/Bootstrap.php @@ -17,14 +17,14 @@ class Bootstrap { add_filter('header.categories', function ($data) { $data[] = [ - 'name' => '插件链接', + 'name' => trans('HeaderMenu::header.plugin_link'), 'url' => shop_route('home.index'), 'children' => [ [ - "name" => "最新商品", + "name" => trans('HeaderMenu::header.latest_products'), "url" => plugin_route('latest_products'), ], [ - "name" => "百度", + "name" => trans('HeaderMenu::header.baidu'), "url" => "https://www.baidu.com", ] ], diff --git a/plugins/HeaderMenu/Lang/en/header.php b/plugins/HeaderMenu/Lang/en/header.php new file mode 100644 index 00000000..8a6db184 --- /dev/null +++ b/plugins/HeaderMenu/Lang/en/header.php @@ -0,0 +1,16 @@ + + * @created 2022-07-28 16:19:06 + * @modified 2022-07-28 16:19:06 + */ + +return [ + 'plugin_link' => 'Plugin Link', + 'latest_products' => 'Latest Products', + 'baidu' => 'Baidu', +]; diff --git a/plugins/HeaderMenu/Lang/zh-CN/header.php b/plugins/HeaderMenu/Lang/zh-CN/header.php new file mode 100644 index 00000000..a34a96d5 --- /dev/null +++ b/plugins/HeaderMenu/Lang/zh-CN/header.php @@ -0,0 +1,16 @@ + + * @created 2022-07-28 16:19:06 + * @modified 2022-07-28 16:19:06 + */ + +return [ + 'plugin_link' => '插件链接', + 'latest_products' => '最新商品', + 'baidu' => '百度', +];