MultiFieldSession.php 4.2 KB

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