pub struct App { /* private fields */ }Expand description
App 容器(全局单例)
持有应用配置、各子系统单例和 DI 服务容器。通过 App::global() 获取全局实例,
通过 App::init() 初始化。
Implementations§
Source§impl App
impl App
Sourcepub fn new(config: AppConfig) -> App
pub fn new(config: AppConfig) -> App
构造 App 实例(不注册到全局单例)
用于测试或显式持有实例的场景。生产代码应使用 App::init() 注册全局单例。
Sourcepub fn init(config: AppConfig) -> &'static App
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);Sourcepub fn global() -> Option<&'static App>
pub fn global() -> Option<&'static App>
获取全局 App 容器实例
必须先调用 App::init() 初始化,否则返回 None。
§命名说明
此方法对应 PHP app() helper(获取全局容器实例)。
不使用 App::instance() 是为了避免与 App::instance<T>(绑定实例方法,
对齐 PHP app()->instance('key', $obj))冲突。
Sourcepub fn db_connection(&self, name: &str) -> Option<&DatabaseConnection>
pub fn db_connection(&self, name: &str) -> Option<&DatabaseConnection>
获取数据库连接配置
对齐 PHP Db::connect('mysql')。
当前返回 DatabaseConnection 配置。
后续将替换为 SZ-ORM Pool 实例。
Sourcepub fn db_connection_names(&self) -> Vec<&str>
pub fn db_connection_names(&self) -> Vec<&str>
获取所有数据库连接名称
Sourcepub fn default_db_connection(&self) -> Option<&DatabaseConnection>
pub fn default_db_connection(&self) -> Option<&DatabaseConnection>
获取默认数据库连接配置
Sourcepub fn singleton<T, F>(&self, factory: F)
pub fn singleton<T, F>(&self, factory: F)
注册单例服务
对齐 PHP app()->singleton('key', fn() => new Service())。
Sourcepub fn scoped<T, F>(&self, factory: F)
pub fn scoped<T, F>(&self, factory: F)
注册请求作用域服务
对齐 PHP app()->scoped('key', fn() => new Service())。
Sourcepub fn alias<T: 'static>(&self, name: impl Into<String>)
pub fn alias<T: 'static>(&self, name: impl Into<String>)
为服务类型注册字符串别名
对齐 PHP app()->alias('name', Service::class)。
Sourcepub fn make<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
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(向后兼容)。
Sourcepub fn make_with_scope<T: Send + Sync + 'static>(
&self,
scope_id: ScopeId,
) -> Option<Arc<T>>
pub fn make_with_scope<T: Send + Sync + 'static>( &self, scope_id: ScopeId, ) -> Option<Arc<T>>
解析服务实例(带作用域 ID)
对齐 PHP app()->make('key') + 请求作用域支持。
Sourcepub fn clear_scope(&self, scope_id: ScopeId)
pub fn clear_scope(&self, scope_id: ScopeId)
清理指定作用域的所有缓存实例
Sourcepub fn has_service<T: 'static>(&self) -> bool
pub fn has_service<T: 'static>(&self) -> bool
检查服务是否已注册
Trait Implementations§
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> 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
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>
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>
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>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
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
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.