pub struct AddonRegistry { /* private fields */ }Expand description
插件注册中心 — 对齐隐式插件状态管理 插件注册中心
对齐 PHP think\addons\Service 中隐式的插件状态管理。
§线程安全
使用 RwLock<HashMap<String, AddonManifest>> 实现,支持多读单写。
§用法
ⓘ
use sz_rust_addons_loader::registry::AddonRegistry;
use sz_rust_addons_loader::manifest::AddonManifest;
let registry = AddonRegistry::new();
let manifest = AddonManifest::new("operate");
registry.register(manifest).unwrap();
assert!(registry.exists("operate"));
assert_eq!(registry.count(), 1);Implementations§
Source§impl AddonRegistry
impl AddonRegistry
Sourcepub fn new() -> AddonRegistry
pub fn new() -> AddonRegistry
创建空注册中心
Sourcepub fn register(&self, manifest: AddonManifest) -> Result<(), AddonLoaderError>
pub fn register(&self, manifest: AddonManifest) -> Result<(), AddonLoaderError>
Sourcepub fn upsert(&self, manifest: AddonManifest)
pub fn upsert(&self, manifest: AddonManifest)
强制注册或更新插件(对齐 PHP array_merge 覆盖行为)
Sourcepub fn unregister(
&self,
name: &str,
) -> Result<Option<AddonManifest>, AddonLoaderError>
pub fn unregister( &self, name: &str, ) -> Result<Option<AddonManifest>, AddonLoaderError>
Sourcepub fn get(&self, name: &str) -> Result<AddonManifest, AddonLoaderError>
pub fn get(&self, name: &str) -> Result<AddonManifest, AddonLoaderError>
Sourcepub fn try_get(&self, name: &str) -> Option<AddonManifest>
pub fn try_get(&self, name: &str) -> Option<AddonManifest>
尝试查询插件清单(不返回错误)
Sourcepub fn is_enabled(&self, name: &str) -> Result<bool, AddonLoaderError>
pub fn is_enabled(&self, name: &str) -> Result<bool, AddonLoaderError>
判断插件是否启用(对齐 PHP Route::execute 中 !$info['status'] 检查)
§返回
Ok(true):插件存在且 status != 0Ok(false):插件存在但 status == 0Err(AddonNotFound):插件不存在
Sourcepub fn set_enabled(
&self,
name: &str,
enabled: bool,
) -> Result<(), AddonLoaderError>
pub fn set_enabled( &self, name: &str, enabled: bool, ) -> Result<(), AddonLoaderError>
Sourcepub fn all(&self) -> Vec<AddonManifest>
pub fn all(&self) -> Vec<AddonManifest>
获取所有已注册的插件清单(按名字序)
Sourcepub fn enabled_addons(&self) -> Vec<AddonManifest>
pub fn enabled_addons(&self) -> Vec<AddonManifest>
获取所有启用的插件清单(按名字序)
Sourcepub fn disabled_addons(&self) -> Vec<AddonManifest>
pub fn disabled_addons(&self) -> Vec<AddonManifest>
获取所有禁用的插件清单(按名字序)
Sourcepub fn load_from_directory(
&self,
addons_path: &Path,
) -> Result<Vec<AddonLoaderError>, AddonLoaderError>
pub fn load_from_directory( &self, addons_path: &Path, ) -> Result<Vec<AddonLoaderError>, AddonLoaderError>
从插件目录批量加载并注册(对齐 PHP Service::loadService 扫描逻辑)
§扫描规则
对齐 PHP scandir($addons_path):
- 扫描
addons_path下的所有子目录 - 跳过
...及文件项 - 子目录必须包含
Plugin.php(对齐 PHPis_file($addonDir . ucfirst($name) . '.php'),Rust 侧统一使用Plugin.php) - 解析
Plugin.php中的$info数组 - 注册到注册中心
§错误处理
- 单个插件解析失败不会中断整体扫描,但会记录到返回的错误列表
- 目录读取失败返回
ScanDir错误
Sourcepub fn addon_path(&self, name: &str) -> Result<PathBuf, AddonLoaderError>
pub fn addon_path(&self, name: &str) -> Result<PathBuf, AddonLoaderError>
获取插件文件路径(对齐 PHP getAddonsPath() . $name . DIRECTORY_SEPARATOR)
Trait Implementations§
Source§impl Debug for AddonRegistry
impl Debug for AddonRegistry
Source§impl Default for AddonRegistry
impl Default for AddonRegistry
Source§fn default() -> AddonRegistry
fn default() -> AddonRegistry
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !Freeze for AddonRegistry
impl !RefUnwindSafe for AddonRegistry
impl Send for AddonRegistry
impl Sync for AddonRegistry
impl Unpin for AddonRegistry
impl UnsafeUnpin for AddonRegistry
impl UnwindSafe for AddonRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.