- demo 插件 plugins/demo.hello-world:演示短代码过滤器、comment.created 钩子、路由+命名空间视图、迁移+模型、后台设置页、filament.post_form 表单注入 - demo 主题 themes/demo:最小可运行主题(覆盖 partials + 绿色系样式) - 教程 docs/tutorial-plugin.md / docs/tutorial-theme.md(10 分钟上手);规范 docs/development.md(代码/插件/主题/队列/测试/提交) - README 增加开发者文档导航与上手示例 - 修复关键缺口:插件类原来靠 composer.json 硬编码加载,第三方 ZIP 安装的插件无法加载;现改为启动时扫描 plugins/*/src 运行期注册 PSR-4(register 阶段,保证 Filament 面板解析插件页面前就绪),composer.json 移除硬编码 - 测试:demo 插件 3 个用例 + demo 主题渲染;全量 89 通过
63 lines
2.3 KiB
PHP
63 lines
2.3 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace Plugins\Demo\HelloWorld;
|
||
|
||
use App\Blog\Support\PluginManager;
|
||
use App\Blog\Support\PluginServiceProvider;
|
||
use App\Models\Comment;
|
||
use App\Models\Post;
|
||
use App\Models\Setting;
|
||
use Filament\Forms\Components\TextInput;
|
||
use Filament\Schemas\Components\Section;
|
||
use Filament\Schemas\Schema;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Plugins\Demo\HelloWorld\Models\HelloWorldLog;
|
||
|
||
class ServiceProvider extends PluginServiceProvider
|
||
{
|
||
protected function boot(PluginManager $manager): void
|
||
{
|
||
// 1. 加载前台路由(可选)
|
||
$this->loadRoutes(__DIR__.'/../routes/web.php');
|
||
|
||
// 2. 注册视图命名空间(可选):views/ -> demo-hello::xxx
|
||
$this->loadViews(__DIR__.'/../views', 'demo-hello');
|
||
|
||
// 3. 过滤器示例:文章渲染时把 [hello xxx] 短代码替换为问候语
|
||
$manager->addFilter('post.rendered', function (string $html, Post $post) {
|
||
$greeting = (string) Setting::get('hello_world_greeting', '你好');
|
||
|
||
return preg_replace_callback(
|
||
'/\[hello\s+([^\]]+)\]/',
|
||
fn (array $m) => '<span class="hello-world">'.$greeting.','.e($m[1]).'!</span>',
|
||
$html
|
||
);
|
||
}, 20);
|
||
|
||
// 4. 动作示例:新评论创建时写日志(演示数据关联 + 钩子监听)
|
||
$manager->addAction('comment.created', function (Comment $comment) {
|
||
HelloWorldLog::create([
|
||
'comment_id' => $comment->id,
|
||
'note' => '捕获新评论:'.mb_substr($comment->content, 0, 30),
|
||
]);
|
||
Log::info('HelloWorld: 捕获新评论 #'.$comment->id);
|
||
});
|
||
|
||
// 5. 后台表单注入示例:给文章编辑页加一个演示字段(meta.demo_note)
|
||
$manager->addAction('filament.post_form', function (Schema $schema) {
|
||
$schema->components([
|
||
...$schema->getComponents(),
|
||
Section::make('Hello World 演示')
|
||
->collapsible()
|
||
->schema([
|
||
TextInput::make('meta.demo_note')
|
||
->label('演示字段(存到文章 meta)')
|
||
->helperText('这是插件注入的字段,停用插件后消失'),
|
||
]),
|
||
]);
|
||
});
|
||
}
|
||
}
|