pub struct AddonAutoload { /* private fields */ }Expand description
自动加载器 — 对齐 spl_autoload_register
插件自动加载器(对齐 PHP spl_autoload_register 回调)
§设计
- 持有插件根目录(对齐 PHP
app()->getRootPath() . 'addons/') - 提供
resolve(class)方法:类名 → 文件路径(不实际加载文件) - 支持 PSR-0 风格的下划线转目录分隔符
Implementations§
Source§impl AddonAutoload
impl AddonAutoload
Sourcepub fn new(addons_path: impl Into<PathBuf>) -> AddonAutoload
pub fn new(addons_path: impl Into<PathBuf>) -> AddonAutoload
创建自动加载器
addons_path:插件根目录,对齐 PHPgetAddonsPath()返回的{rootPath}/addons/
Sourcepub fn addons_path(&self) -> &Path
pub fn addons_path(&self) -> &Path
获取插件根目录
Sourcepub fn resolve(&self, class: &str) -> Result<Option<PathBuf>, AddonLoaderError>
pub fn resolve(&self, class: &str) -> Result<Option<PathBuf>, AddonLoaderError>
解析类名到文件路径(对齐 PHP spl_autoload_register 回调)
§规则
- 类名必须以
addons\开头(对齐 PHPstrpos($class, $namespace) === 0) - 命名空间分隔符
\转换为目录分隔符/(对齐str_replace('\\', '/', ...)) - 类名中的下划线
_转换为目录分隔符(对齐str_replace('_', '/', $class)) - 拼接
.php后缀 - 检查文件是否存在
§参数
class:完整类名(如addons\operate\Plugin)
§返回
Ok(Some(path)):类映射到文件且文件存在Ok(None):类名不属于addons\命名空间,或文件不存在(让其他 autoloader 处理)Err(_):路径解析失败
Sourcepub fn resolve_controller(
&self,
addon: &str,
controller: &str,
) -> Result<Option<PathBuf>, AddonLoaderError>
pub fn resolve_controller( &self, addon: &str, controller: &str, ) -> Result<Option<PathBuf>, AddonLoaderError>
解析控制器类名(对齐 PHP get_addons_class($name, 'controller', $class))
§多级控制器点号分隔
对齐 PHP get_addons_class 中 . 处理:
if (strpos($class, '.') !== false) {
$array = explode('.', $class);
$class = array_pop($array);
$class = Str::studly($class);
$class = implode('\\', $array) . '\\' . $class;
}§示例
resolve_controller("operate", "Order")→addons/operate/controller/Order.phpresolve_controller("operate", "admin.Order")→addons/operate/controller/admin/Order.phpresolve_controller("operate", "admin.sub.Order")→addons/operate/controller/admin/sub/Order.php
Sourcepub fn resolve_plugin(
&self,
addon: &str,
) -> Result<Option<PathBuf>, AddonLoaderError>
pub fn resolve_plugin( &self, addon: &str, ) -> Result<Option<PathBuf>, AddonLoaderError>
解析插件入口类(对齐 PHP get_addons_class($name) 默认 type=‘hook’)
§示例
resolve_plugin("operate")→addons/operate/Plugin.php
Sourcepub fn resolve_strict(&self, class: &str) -> Result<PathBuf, AddonLoaderError>
pub fn resolve_strict(&self, class: &str) -> Result<PathBuf, AddonLoaderError>
强制解析类名(对齐 PHP get_addons_class 返回字符串而非 bool)
与 resolve 的区别:不检查文件是否存在,直接返回路径
Trait Implementations§
Source§impl Clone for AddonAutoload
impl Clone for AddonAutoload
Source§fn clone(&self) -> AddonAutoload
fn clone(&self) -> AddonAutoload
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for AddonAutoload
impl Debug for AddonAutoload
impl Eq for AddonAutoload
Source§impl PartialEq for AddonAutoload
impl PartialEq for AddonAutoload
impl StructuralPartialEq for AddonAutoload
Auto Trait Implementations§
impl Freeze for AddonAutoload
impl RefUnwindSafe for AddonAutoload
impl Send for AddonAutoload
impl Sync for AddonAutoload
impl Unpin for AddonAutoload
impl UnsafeUnpin for AddonAutoload
impl UnwindSafe for AddonAutoload
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.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,
impl<T> Scalar for T
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.