Open xiaohuilam opened 5 years ago
前言还没想好。
SessionServiceProvider 没有 boot 方法,其 register 方法为 https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/SessionServiceProvider.php#L15-L22
SessionServiceProvider
boot
register
作用
Illuminate\Session\SessionManager
session
单例注册 Illuminate\Session\Store 为 session.store
Illuminate\Session\Store
session.store
具体生成 Illuminate\Session\Store 对象的过程,由你 .env 配置 SESSION_DRIVER 而调用 Illuminate\Session\SessionManager 不同方法: createArrayDriver createCookieDriver createFileDriver createNativeDriver 其实 createFileDriver 就是调用的此方法 createDatabaseDriver createApcDriver createMemcachedDriver createRedisDriver
具体生成 Illuminate\Session\Store 对象的过程,由你 .env 配置 SESSION_DRIVER 而调用 Illuminate\Session\SessionManager 不同方法:
.env
SESSION_DRIVER
createArrayDriver
createCookieDriver
createFileDriver
createNativeDriver
createDatabaseDriver
createApcDriver
createMemcachedDriver
createRedisDriver
Illuminate\Session\Middleware\StartSession
下面,我们以 SESSION_DRIVER=file 继续分析 当 SESSION_DRIVER 为 file 时,会调用到 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
SESSION_DRIVER=file
file
这里 $this->app['config']['session.files'] 得到的是 https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/config/session.php#L60
$this->app['config']['session.files']
然后通过此处实例化 Illuminate\Session\FileSessionHandler https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php#L61
Illuminate\Session\FileSessionHandler
然后调用 buildSession 获得 \Illuminate\Session\Store 对象 https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php#L163-L170
buildSession
\Illuminate\Session\Store
当触发 Session::start 时, https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L69-L78
Session::start
调用的 loadSession 为 https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L85-L88
loadSession
调用的 readFromHandler 为 https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L95-L106
readFromHandler
这里的 read 方法,就是 FileSessionHandler::read https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/FileSessionHandler.php#L67-L76
read
FileSessionHandler::read
执行到这里时, session 中的数据就被存放到了 Illuminate\Session\Store::$attributes 了。
Illuminate\Session\Store::$attributes
而执行 Session::get 时, https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L205-L208
Session::get
正好就能从 Store::$attributes 中取出 session 的数据了。
Store::$attributes
当我们使用 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
Illuminate\Support\Facades\Session
Illuminate\Support\Manager
__call
session.driver
有兴趣的同学请阅读 15. Laravel 神奇的 Manager 类
假如我们执行 Session::put('key', 'value') 会穿透到
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 时,会触发
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
FileSessionHandler::write
至此,写入 session 的流程走完。
服务提供者部分
SessionServiceProvider
没有boot
方法,其register
方法为 https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/SessionServiceProvider.php#L15-L22作用
Illuminate\Session\SessionManager
为session
单例注册
Illuminate\Session\Store
为session.store
Illuminate\Session\Middleware\StartSession
中间件下面,我们以
SESSION_DRIVER=file
继续分析 当SESSION_DRIVER
为file
时,会调用到createFileDriver
https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php#L47-L50createFileDriver
调用了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-L170session 功能 / 操作部分
读取 session
当触发
Session::start
时, https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L69-L78调用的
loadSession
为 https://github.com/xiaohuilam/laravel/blob/ca57c288f7825c42550333b613440cb4e824b3ad/vendor/laravel/framework/src/Illuminate/Session/Store.php#L85-L88调用的
readFromHandler
为 https://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假如我们执行
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 的流程走完。