github.com/zly-app/zapp@v1.3.3/plugin/plugin.go (about) 1 package plugin 2 3 import ( 4 "go.uber.org/zap" 5 6 "github.com/zly-app/zapp/core" 7 "github.com/zly-app/zapp/logger" 8 ) 9 10 // 插件建造者 11 type pluginCreator func(app core.IApp) core.IPlugin 12 13 func (h pluginCreator) Create(app core.IApp) core.IPlugin { 14 return h(app) 15 } 16 17 // 建造者列表 18 var creators = make(map[core.PluginType]core.IPluginCreator) 19 20 // 注册插件建造者 21 func RegisterCreator(pluginType core.PluginType, creator core.IPluginCreator) { 22 if _, ok := creators[pluginType]; ok { 23 logger.Log.Fatal("重复注册建造者", zap.String("pluginType", string(pluginType))) 24 } 25 creators[pluginType] = creator 26 } 27 28 // 注册插件建造者函数 29 func RegisterCreatorFunc(pluginType core.PluginType, creatorFunc func(app core.IApp) core.IPlugin) { 30 RegisterCreator(pluginType, pluginCreator(creatorFunc)) 31 } 32 33 // 构建插件 34 func MakePlugin(app core.IApp, pluginType core.PluginType) core.IPlugin { 35 if creator, ok := creators[pluginType]; ok { 36 return creator.Create(app) 37 } 38 app.Fatal("使用了未注册建造者的插件", zap.String("pluginType", string(pluginType))) 39 return nil 40 }