Skip to main content

AddonAutoload

Struct AddonAutoload 

Source
pub struct AddonAutoload { /* private fields */ }
Expand description

插件自动加载器(对齐 PHP spl_autoload_register 回调)

§设计

  • 持有插件根目录(对齐 PHP app()->getRootPath() . 'addons/'
  • 提供 resolve(class) 方法:类名 → 文件路径(不实际加载文件)
  • 支持 PSR-0 风格的下划线转目录分隔符

Implementations§

Source§

impl AddonAutoload

Source

pub fn new(addons_path: impl Into<PathBuf>) -> Self

创建自动加载器

  • addons_path:插件根目录,对齐 PHP getAddonsPath() 返回的 {rootPath}/addons/
Source

pub fn addons_path(&self) -> &Path

获取插件根目录

Source

pub fn resolve(&self, class: &str) -> AddonLoaderResult<Option<PathBuf>>

解析类名到文件路径(对齐 PHP spl_autoload_register 回调)

§规则
  1. 类名必须以 addons\ 开头(对齐 PHP strpos($class, $namespace) === 0
  2. 命名空间分隔符 \ 转换为目录分隔符 /(对齐 str_replace('\\', '/', ...)
  3. 类名中的下划线 _ 转换为目录分隔符(对齐 str_replace('_', '/', $class)
  4. 拼接 .php 后缀
  5. 检查文件是否存在
§参数
  • class:完整类名(如 addons\operate\Plugin
§返回
  • Ok(Some(path)):类映射到文件且文件存在
  • Ok(None):类名不属于 addons\ 命名空间,或文件不存在(让其他 autoloader 处理)
  • Err(_):路径解析失败
Source

pub fn resolve_controller( &self, addon: &str, controller: &str, ) -> AddonLoaderResult<Option<PathBuf>>

解析控制器类名(对齐 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.php
  • resolve_controller("operate", "admin.Order")addons/operate/controller/admin/Order.php
  • resolve_controller("operate", "admin.sub.Order")addons/operate/controller/admin/sub/Order.php
Source

pub fn resolve_plugin(&self, addon: &str) -> AddonLoaderResult<Option<PathBuf>>

解析插件入口类(对齐 PHP get_addons_class($name) 默认 type=‘hook’)

§示例
  • resolve_plugin("operate")addons/operate/Plugin.php
Source

pub fn resolve_strict(&self, class: &str) -> AddonLoaderResult<PathBuf>

强制解析类名(对齐 PHP get_addons_class 返回字符串而非 bool)

resolve 的区别:不检查文件是否存在,直接返回路径

Trait Implementations§

Source§

impl Clone for AddonAutoload

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for AddonAutoload

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for AddonAutoload

Source§

impl PartialEq for AddonAutoload

Source§

fn eq(&self, other: &AddonAutoload) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for AddonAutoload

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.