From 3971ad5977cfc6706396140d67d52d49e07dc775 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Fri, 3 Mar 2023 15:58:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8F=92=E4=BB=B6=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E9=9D=99=E6=80=81=E6=96=87=E4=BB=B6helper:=20plugin?= =?UTF-8?q?=5Fasset()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beike/Helpers.php | 11 +++ beike/Hook/Hook.php | 2 + beike/Plugin/Asset.php | 69 +++++++++++++++++++ .../Http/Controllers/PluginController.php | 33 +++++++++ beike/Shop/Routes/shop.php | 3 + 5 files changed, 118 insertions(+) create mode 100644 beike/Plugin/Asset.php create mode 100644 beike/Shop/Http/Controllers/PluginController.php diff --git a/beike/Helpers.php b/beike/Helpers.php index f619eeb3..22863a91 100644 --- a/beike/Helpers.php +++ b/beike/Helpers.php @@ -57,6 +57,17 @@ function plugin_setting($key, $default = null) return setting("plugin.{$key}", $default); } +/** + * 获取插件静态文件 + * + * @param $code , 插件编码 + * @param $filePath , 相对于插件目录 static 的文件路径 + */ +function plugin_asset($code, $filePath): string +{ + return shop_route('plugin.asset', ['code' => $code, 'path' => $filePath]); +} + /** * 获取后台管理前缀名称, 默认为 admin */ diff --git a/beike/Hook/Hook.php b/beike/Hook/Hook.php index 362bed03..e4394720 100644 --- a/beike/Hook/Hook.php +++ b/beike/Hook/Hook.php @@ -60,6 +60,7 @@ class Hook if (config('app.debug') && has_debugbar()) { Debugbar::log("HOOK === @hook: {$hook}"); } + return $this->get($hook, $params, $callback, $htmlContent); } @@ -76,6 +77,7 @@ class Hook if (config('app.debug') && has_debugbar()) { Debugbar::log("HOOK === @hookwrapper: {$hook}"); } + return $this->get($hook, $params, $callback, $htmlContent); } diff --git a/beike/Plugin/Asset.php b/beike/Plugin/Asset.php new file mode 100644 index 00000000..0279f3d3 --- /dev/null +++ b/beike/Plugin/Asset.php @@ -0,0 +1,69 @@ + + * @created 2023-03-03 14:50:31 + * @modified 2023-03-03 14:50:31 + */ + +namespace Beike\Plugin; + +use Illuminate\Support\Facades\File; +use Illuminate\Support\Str; + +class Asset +{ + private $pluginPath; + + const CONTENT_TYPES = [ + 'js' => 'application/javascript', + 'css' => 'text/css', + 'jpg' => 'image/jpeg', + 'apng' => 'image/apng', + 'avif' => 'image/avif', + 'gif' => 'image/gif', + 'jpeg' => 'image/jpeg', + 'png' => 'image/png', + 'svg' => 'image/svg+xml', + 'webp' => 'image/webp', + 'webm' => 'video/webm', + 'ogg' => 'video/ogg', + ]; + + public function __construct($pluginCode) + { + if (empty($pluginCode)) { + throw new \Exception('Empty plugin code!'); + } + $folderName = Str::studly($pluginCode); + $this->pluginPath = base_path('plugins/' . $folderName); + } + + public static function getInstance($pluginCode) + { + return new self($pluginCode); + } + + + /** + * Get content and type + * + * @param $file + * @return array|string + */ + public function getContent($file) + { + $filePath = $this->pluginPath . '/Static/' . $file; + if (is_file($filePath)) { + $extension = File::extension($filePath); + return [ + 'type' => self::CONTENT_TYPES[$extension] ?? '', + 'content' => file_get_contents($filePath), + ]; + } + return []; + } +} diff --git a/beike/Shop/Http/Controllers/PluginController.php b/beike/Shop/Http/Controllers/PluginController.php new file mode 100644 index 00000000..d884800e --- /dev/null +++ b/beike/Shop/Http/Controllers/PluginController.php @@ -0,0 +1,33 @@ + + * @created 2023-03-03 15:06:40 + * @modified 2023-03-03 15:06:40 + */ + +namespace Beike\Shop\Http\Controllers; + +use Beike\Plugin\Asset; +use Illuminate\Support\Facades\Response; + +class PluginController extends Controller +{ + public function asset($code, $path) + { + $contents = Asset::getInstance($code)->getContent($path); + + $content = $contents['content'] ?? ''; + $type = $contents['type'] ?? ''; + + if ($content && $type) { + $response = Response::make($content); + $response->header('Content-Type', $type); + return $response; + } + return ''; + } +} diff --git a/beike/Shop/Routes/shop.php b/beike/Shop/Routes/shop.php index a05552b3..7e5199b1 100644 --- a/beike/Shop/Routes/shop.php +++ b/beike/Shop/Routes/shop.php @@ -21,6 +21,7 @@ use Beike\Shop\Http\Controllers\HomeController; use Beike\Shop\Http\Controllers\LanguageController; use Beike\Shop\Http\Controllers\PageCategoryController; use Beike\Shop\Http\Controllers\PageController; +use Beike\Shop\Http\Controllers\PluginController; use Beike\Shop\Http\Controllers\ProductController; use Beike\Shop\Http\Controllers\ZoneController; use Illuminate\Support\Facades\Route; @@ -65,6 +66,8 @@ Route::prefix('/') Route::get('register', [RegisterController::class, 'index'])->name('register.index'); Route::post('register', [RegisterController::class, 'store'])->name('register.store'); + Route::get('plugin/{code}/{path}', [PluginController::class, 'asset'])->where('path', '(.*)')->name('plugin.asset'); + Route::middleware('checkout_auth:' . Customer::AUTH_GUARD) ->group(function () { Route::get('carts', [CartController::class, 'index'])->name('carts.index');