新增插件读取静态文件helper: plugin_asset()

This commit is contained in:
Edward Yang 2023-03-03 15:58:22 +08:00
parent c7338ba877
commit 3971ad5977
5 changed files with 118 additions and 0 deletions

View File

@ -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
*/

View File

@ -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);
}

69
beike/Plugin/Asset.php Normal file
View File

@ -0,0 +1,69 @@
<?php
/**
* Asset.php
*
* @copyright 2023 beikeshop.com - All Rights Reserved
* @link https://beikeshop.com
* @author Edward Yang <yangjin@guangda.work>
* @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 [];
}
}

View File

@ -0,0 +1,33 @@
<?php
/**
* PluginController.php
*
* @copyright 2023 beikeshop.com - All Rights Reserved
* @link https://beikeshop.com
* @author Edward Yang <yangjin@guangda.work>
* @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 '';
}
}

View File

@ -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');