xiaohuilam / laravel

Laravel 深入详解 —— 源代码解析,新手进阶指南
433 stars 80 forks source link

08. SessionServiceProvider 详解 #8

Open xiaohuilam opened 5 years ago

xiaohuilam commented 5 years ago

前言还没想好。

服务提供者部分

SessionServiceProvider 没有 boot 方法,其 register 方法为 https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/SessionServiceProvider.php#L15-L22

作用

下面,我们以 SESSION_DRIVER=file 继续分析 当 SESSION_DRIVERfile 时,会调用到 createFileDriver https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php#L47-L50 createFileDriver 调用了 createNativeDriver https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php#L57-L64

这里 $this->app['config']['session.files'] 得到的是 https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/config/session.php#L60

然后通过此处实例化 Illuminate\Session\FileSessionHandler https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php#L61

然后调用 buildSession 获得 \Illuminate\Session\Store 对象 https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php#L163-L170

session 功能 / 操作部分

读取 session

当触发 Session::start 时, https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L69-L78

调用的 loadSessionhttps://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L85-L88

调用的 readFromHandlerhttps://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L95-L106

这里的 read 方法,就是 FileSessionHandler::read https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/FileSessionHandler.php#L67-L76

执行到这里时, session 中的数据就被存放到了 Illuminate\Session\Store::$attributes 了。

而执行 Session::get 时, https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L205-L208

正好就能从 Store::$attributes 中取出 session 的数据了。

写入 session

当我们使用 Illuminate\Support\Facades\Session 时,操作是调用到的 session,也就是 Illuminate\Session\SessionManager。但是在 Illuminate\Support\Manager__call 魔术方法,实现了将 session.driver 载入到 session 中被执行的能力。 https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Support/Manager.php#L144-L147

有兴趣的同学请阅读 15. Laravel 神奇的 Manager 类

假如我们执行 Session::put('key', 'value') 会穿透到

https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L265-L274

当我们手动 Session::save 时,或者程序终止运行触发 register_shutdown_function 时,会触发

https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L124-L133

也就是 FileSessionHandler::write https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/FileSessionHandler.php#L81-L87

至此,写入 session 的流程走完。