yii2-sessions-arangodb/mirzaev/yii2/arangodb/sessions/MultiFieldSession.php

135 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace mirzaev\yii2\arangodb\sessions;
use yii;
use yii\web\Session;
/**
* Базовый класс для реализации хранилища сессий
*
* MultiFieldSession is the base class for session storage implementations with multi-field data storage support
*
* @property-read bool $useCustomStorage Разрешение использовать своё хранилища (Whether to use custom storage)
*
* @author Arsen Mirzaev Tatyano-Muradovich <red@hood.su>
* @author Paul Klimov <klimov.paul@gmail.com>
*/
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.
*
* 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(): bool
{
return true;
}
/**
* Composes storage field set for session writing.
*
* @param string $id Optional session id
* @param string $data Optional session data
*
* @return array storage fields
*/
protected function composeFields($id = null, $data = null): array
{
$buffer = $this->writeCallback ? call_user_func($this->writeCallback, $this) : [];
if ($id !== null) {
$buffer['hash'] = $id;
}
if ($data !== null) {
$buffer['data'] = $data;
}
return $buffer;
}
/**
* Extracts session data from storage field set.
*
* @param array $buffer storage buffer.
*
* @return string session data.
*/
protected function extractData(array $buffer): string
{
if ($this->readCallback !== null) {
if (!isset($buffer['data'])) {
$buffer['data'] = '';
}
$extraData = call_user_func($this->readCallback, $buffer);
if (!empty($extraData)) {
session_decode($buffer['data']);
$_SESSION = array_merge((array) $_SESSION, (array) $extraData);
return session_encode();
}
return $buffer['data'];
}
return isset($buffer['data']) ? $buffer['data'] : '';
}
}