1 | <?php |
||
30 | abstract class MultiFieldSession extends Session |
||
31 | { |
||
32 | /** |
||
33 | * @var callable a callback that will be called during session data reading. |
||
34 | * The signature of the callback should be as follows: |
||
35 | * |
||
36 | * ``` |
||
37 | * function ($fields) |
||
38 | * ``` |
||
39 | * |
||
40 | * where `$fields` is the storage field set for read session and `$session` is this session instance. |
||
41 | * If callback returns an array, it will be merged into the session data. |
||
42 | * |
||
43 | * For example: |
||
44 | * |
||
45 | * ```php |
||
46 | * function ($fields) { |
||
47 | * return [ |
||
48 | * 'expireDate' => Yii::$app->formatter->asDate($fields['expire']), |
||
49 | * ]; |
||
50 | * } |
||
51 | * ``` |
||
52 | */ |
||
53 | public $readCallback; |
||
54 | /** |
||
55 | * @var callable a callback that will be called during session data writing. |
||
56 | * The signature of the callback should be as follows: |
||
57 | * |
||
58 | * ``` |
||
59 | * function ($session) |
||
60 | * ``` |
||
61 | * |
||
62 | * where `$session` is this session instance, this variable can be used to retrieve session data. |
||
63 | * Callback should return the actual fields set, which should be saved into the session storage. |
||
64 | * |
||
65 | * For example: |
||
66 | * |
||
67 | * ```php |
||
68 | * function ($session) { |
||
69 | * return [ |
||
70 | * 'user_id' => Yii::$app->user->id, |
||
71 | * 'ip' => $_SERVER['REMOTE_ADDR'], |
||
72 | * 'is_trusted' => $session->get('is_trusted', false), |
||
73 | * ]; |
||
74 | * } |
||
75 | * ``` |
||
76 | */ |
||
77 | public $writeCallback; |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Returns a value indicating whether to use custom session storage. |
||
82 | * This method overrides the parent implementation and always returns true. |
||
83 | * @return bool whether to use custom storage. |
||
84 | */ |
||
85 | public function getUseCustomStorage() |
||
89 | |||
90 | /** |
||
91 | * Composes storage field set for session writing. |
||
92 | * @param string $id session id |
||
93 | * @param string $data session data |
||
94 | * @return array storage fields |
||
95 | */ |
||
96 | 3 | protected function composeFields($id, $data) |
|
97 | { |
||
98 | $fields = [ |
||
99 | 3 | 'data' => $data, |
|
100 | ]; |
||
101 | 3 | if ($this->writeCallback !== null) { |
|
102 | 1 | $fields = array_merge( |
|
103 | 1 | $fields, |
|
104 | 1 | call_user_func($this->writeCallback, $this) |
|
105 | ); |
||
106 | 1 | if (!is_string($fields['data'])) { |
|
107 | $_SESSION = $fields['data']; |
||
108 | $fields['data'] = session_encode(); |
||
109 | } |
||
110 | } |
||
111 | // ensure 'id' and 'expire' are never affected by [[writeCallback]] |
||
112 | 3 | $fields = array_merge($fields, [ |
|
113 | 3 | 'id' => $id, |
|
114 | 3 | 'expire' => time() + $this->getTimeout(), |
|
115 | ]); |
||
116 | 3 | return $fields; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Extracts session data from storage field set. |
||
121 | * @param array $fields storage fields. |
||
122 | * @return string session data. |
||
123 | */ |
||
124 | protected function extractData($fields) |
||
141 | } |
||
142 |