2e86c939
xu
“首次提交”
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
/**
* MultiFieldSession is the base class for session storage implementations with multi-field data storage support.
*
* With multi-field data storage, session data can be split between several fields in the storage record.
* Using such a storage allows saving particular session data into separated field, which then can be used
* to manipulate sessions in the way plain PHP does not allow.
*
* For example the ID of the authenticated user can be saved as separated column in the MySQL 'session' table,
* which allows to query all active sessions for a particular user or terminate them at will.
*
* Customizing of the session writing is performed via [[writeCallback]], reading via [[readCallback]].
*
* While extending this class you should use [[composeFields()]] method - while writing the session data into the storage and
* [[extractData()]] - while reading session data from the storage.
*
* @property bool $useCustomStorage Whether to use custom storage. This property is read-only.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0.6
*/
abstract class MultiFieldSession extends Session
{
/**
* @var callable a callback that will be called during session data reading.
* The signature of the callback should be as follows:
*
* ```
* function ($fields)
* ```
*
* where `$fields` is the storage field set for read session and `$session` is this session instance.
* If callback returns an array, it will be merged into the session data.
*
* For example:
*
* ```php
* function ($fields) {
* return [
* 'expireDate' => Yii::$app->formatter->asDate($fields['expire']),
* ];
* }
* ```
*/
public $readCallback;
/**
* @var callable a callback that will be called during session data writing.
* The signature of the callback should be as follows:
*
* ```
* function ($session)
* ```
*
* where `$session` is this session instance, this variable can be used to retrieve session data.
* Callback should return the actual fields set, which should be saved into the session storage.
*
* For example:
*
* ```php
* function ($session) {
* return [
* 'user_id' => Yii::$app->user->id,
* 'ip' => $_SERVER['REMOTE_ADDR'],
* 'is_trusted' => $session->get('is_trusted', false),
* ];
* }
* ```
*/
public $writeCallback;
/**
* Returns a value indicating whether to use custom session storage.
* This method overrides the parent implementation and always returns true.
* @return bool whether to use custom storage.
*/
public function getUseCustomStorage()
{
return true;
}
/**
* Composes storage field set for session writing.
* @param string $id session id
* @param string $data session data
* @return array storage fields
*/
protected function composeFields($id, $data)
{
$fields = [
'data' => $data,
];
if ($this->writeCallback !== null) {
$fields = array_merge(
$fields,
call_user_func($this->writeCallback, $this)
);
if (!is_string($fields['data'])) {
$_SESSION = $fields['data'];
$fields['data'] = session_encode();
}
}
// ensure 'id' and 'expire' are never affected by [[writeCallback]]
$fields = array_merge($fields, [
'id' => $id,
'expire' => time() + $this->getTimeout(),
]);
return $fields;
}
/**
* Extracts session data from storage field set.
* @param array $fields storage fields.
* @return string session data.
*/
protected function extractData($fields)
{
if ($this->readCallback !== null) {
if (!isset($fields['data'])) {
$fields['data'] = '';
}
$extraData = call_user_func($this->readCallback, $fields);
if (!empty($extraData)) {
session_decode($fields['data']);
$_SESSION = array_merge((array)$_SESSION, (array)$extraData);
return session_encode();
}
return $fields['data'];
} else {
return isset($fields['data']) ? $fields['data'] : '';
}
}
}
|