M7: 全量测试(25 用例)+ README + 部署文档(nginx/systemd/cron)+ theme:publish 命令

This commit is contained in:
ak
2026-08-11 18:48:16 +08:00
parent 933736cfdd
commit fb9175402a
12 changed files with 802 additions and 65 deletions
+1 -22
View File
@@ -18,7 +18,7 @@ class ThemeServiceProvider extends ServiceProvider
public function boot(): void
{
// 激活主题的 views 目录前置到全局视图路径,实现主题覆盖 + 默认兜底
$this->prependActiveThemeViews();
app(ThemeManager::class)->prependViews();
// 侧边栏数据共享:所有前台视图自动获得 $categories/$recentPosts/$hotTags/$links 等
View::composer(
@@ -26,25 +26,4 @@ class ThemeServiceProvider extends ServiceProvider
SidebarComposer::class
);
}
private function prependActiveThemeViews(): void
{
$manager = app(ThemeManager::class);
// 迁移尚未执行(如 migrate:fresh 首轮)时跳过 DB 查询,使用默认主题
$active = \Illuminate\Support\Facades\Schema::hasTable('settings')
? $manager->active()
: config('themes.default');
$views = $manager->path($active).'/views';
if (! is_dir($views)) {
return;
}
$paths = config('view.paths');
array_unshift($paths, $views);
config(['view.paths' => $paths]);
app('view')->getFinder()->setPaths($paths);
}
}
+24
View File
@@ -69,6 +69,30 @@ class ThemeManager
}
Setting::set('active_theme', $theme);
$this->prependViews();
}
/**
* 把激活主题的 views 目录前置到全局视图路径。
* Provider boot 与运行时切换主题时调用。
*/
public function prependViews(): void
{
// 迁移尚未执行(migrate:fresh 首轮)时跳过 DB 查询,使用默认主题
$active = \Illuminate\Support\Facades\Schema::hasTable('settings')
? $this->active()
: config('themes.default');
$views = $this->path($active).'/views';
if (! is_dir($views)) {
return;
}
$paths = config('view.paths');
array_unshift($paths, $views);
config(['view.paths' => $paths]);
app('view')->getFinder()->setPaths($paths);
}
/**
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Console\Commands;
use App\Blog\Support\ThemeManager;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
class ThemePublish extends Command
{
protected $signature = 'theme:publish {name? : 主题名(默认全部)}';
protected $description = '把主题 assets 拷贝到 public/themes 供生产环境直接托管';
public function handle(ThemeManager $themes): int
{
$names = $this->argument('name')
? [$this->argument('name')]
: array_keys($themes->all());
foreach ($names as $name) {
if (! $themes->exists($name)) {
$this->warn("跳过不存在的主题:{$name}");
continue;
}
$source = $themes->path($name).'/assets';
$target = public_path('themes/'.$name);
if (! is_dir($source)) {
$this->warn("主题 {$name} 没有 assets 目录");
continue;
}
File::deleteDirectory($target);
File::copyDirectory($source, $target);
$this->info("已发布:{$name} -> public/themes/{$name}");
}
return self::SUCCESS;
}
}