CMDC.Plugin.Registry (cmdc v0.5.3)

Copy Markdown View Source

Plugin 注册表。

管理已注册的 Plugin 集合,按 priority/0 自动排序(数值小的先执行), 同一模块只保留一份(去重)。

使用示例

registry = CMDC.Plugin.Registry.new()
{:ok, registry} = CMDC.Plugin.Registry.register(registry, {MyPlugin, []})
{:ok, registry} = CMDC.Plugin.Registry.register(registry, {SecurityPlugin, max_paths: 10})

plugins = CMDC.Plugin.Registry.entries(registry)

Summary

Functions

返回已注册 plugin 的数量。

返回已排序的 [{module, plugin_state}] 列表,供 Pipeline.run/3 使用。

从 plugin 规格列表构建 Registry,完成初始化、去重、排序。

返回已注册的 plugin 模块列表。

创建空的 Registry。

注册一个 Plugin。

检查模块是否已注册。

注销一个 Plugin(按模块名移除)。

Types

entry()

@type entry() :: {module(), CMDC.Plugin.plugin_state()}

plugin_spec()

@type plugin_spec() :: module() | {module(), keyword()}

t()

@type t() :: %CMDC.Plugin.Registry{entries: [entry()]}

Functions

count(registry)

@spec count(t()) :: non_neg_integer()

返回已注册 plugin 的数量。

entries(registry)

@spec entries(t()) :: [entry()]

返回已排序的 [{module, plugin_state}] 列表,供 Pipeline.run/3 使用。

from_specs(specs)

@spec from_specs([plugin_spec()]) :: t()

从 plugin 规格列表构建 Registry,完成初始化、去重、排序。

specs 支持两种格式:

  • {ModuleName, keyword_opts} — 带自定义选项
  • ModuleName — 使用空 opts 初始化

初始化失败的 plugin 会被跳过并记录警告日志。

modules(registry)

@spec modules(t()) :: [module()]

返回已注册的 plugin 模块列表。

new()

@spec new() :: t()

创建空的 Registry。

register(registry, arg)

@spec register(
  t(),
  {module(), keyword()}
) :: {:ok, t()} | {:error, term()}

注册一个 Plugin。

调用 module.init(opts) 初始化状态,若同一模块已注册则覆盖旧状态, 注册后自动重新排序。

registered?(registry, module)

@spec registered?(t(), module()) :: boolean()

检查模块是否已注册。

unregister(registry, module)

@spec unregister(t(), module()) :: t()

注销一个 Plugin(按模块名移除)。