Skip to main content

App

Struct App 

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

App 容器(全局单例)

持有应用配置、各子系统单例和 DI 服务容器。通过 App::global() 获取全局实例, 通过 App::init() 初始化。

Implementations§

Source§

impl App

Source

pub fn new(config: AppConfig) -> App

构造 App 实例(不注册到全局单例)

用于测试或显式持有实例的场景。生产代码应使用 App::init() 注册全局单例。

Source

pub fn init(config: AppConfig) -> &'static App

初始化全局 App 容器

只能调用一次,重复调用返回已有实例。

use sz_rust_core::container::App;
use sz_rust_core::config::AppConfig;

let config = AppConfig::load_from_dir("config").unwrap();
let app = App::init(config);
Source

pub fn global() -> Option<&'static App>

获取全局 App 容器实例

必须先调用 App::init() 初始化,否则返回 None

§命名说明

此方法对应 PHP app() helper(获取全局容器实例)。 不使用 App::instance() 是为了避免与 App::instance<T>(绑定实例方法, 对齐 PHP app()->instance('key', $obj))冲突。

Source

pub fn config(&self) -> &AppConfig

获取应用配置

Source

pub fn db_connection(&self, name: &str) -> Option<&DatabaseConnection>

获取数据库连接配置

对齐 PHP Db::connect('mysql')

当前返回 DatabaseConnection 配置。 后续将替换为 SZ-ORM Pool 实例。

Source

pub fn db_connection_names(&self) -> Vec<&str>

获取所有数据库连接名称

Source

pub fn default_db_connection(&self) -> Option<&DatabaseConnection>

获取默认数据库连接配置

Source

pub fn set_cache(&self, cache: impl Into<String>)

设置 Cache 单例(将替换为真正的 Cache facade)

Source

pub fn cache(&self) -> Option<String>

获取 Cache 单例

Source

pub fn set_log(&self, log: impl Into<String>)

设置 Log 单例(将替换为真正的日志系统)

Source

pub fn log(&self) -> Option<String>

获取 Log 单例

Source

pub fn container(&self) -> &Container

获取 DI 服务容器引用

Source

pub fn bind<T, F>(&self, factory: F)
where T: Send + Sync + 'static, F: Fn() -> T + Send + Sync + 'static,

注册瞬态服务

对齐 PHP app()->bind('key', fn() => new Service())

Source

pub fn singleton<T, F>(&self, factory: F)
where T: Send + Sync + 'static, F: Fn() -> T + Send + Sync + 'static,

注册单例服务

对齐 PHP app()->singleton('key', fn() => new Service())

Source

pub fn scoped<T, F>(&self, factory: F)
where T: Send + Sync + 'static, F: Fn() -> T + Send + Sync + 'static,

注册请求作用域服务

对齐 PHP app()->scoped('key', fn() => new Service())

Source

pub fn instance<T>(&self, instance: T)
where T: Send + Sync + 'static,

直接绑定已创建的实例

对齐 PHP app()->instance('key', $obj)

Source

pub fn alias<T: 'static>(&self, name: impl Into<String>)

为服务类型注册字符串别名

对齐 PHP app()->alias('name', Service::class)

Source

pub fn make<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>

解析服务实例(自动感知请求作用域)

对齐 PHP app()->make('key')

P1-ARCH-DI-02:若当前线程处于 RequestScopeLayer 管理的请求中(即 current_scope_id() 返回 Some),则自动路由到 make_with_scope,使 Scoped 绑定在请求内缓存、请求结束清理。 请求外调用则退化为无作用域的 make(向后兼容)。

Source

pub fn make_with_scope<T: Send + Sync + 'static>( &self, scope_id: ScopeId, ) -> Option<Arc<T>>

解析服务实例(带作用域 ID)

对齐 PHP app()->make('key') + 请求作用域支持。

Source

pub fn clear_scope(&self, scope_id: ScopeId)

清理指定作用域的所有缓存实例

Source

pub fn has_service<T: 'static>(&self) -> bool

检查服务是否已注册

Trait Implementations§

Source§

impl Debug for App

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for App

§

impl !RefUnwindSafe for App

§

impl !UnwindSafe for App

§

impl Send for App

§

impl Sync for App

§

impl Unpin for App

§

impl UnsafeUnpin for App

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more