173 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			173 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
/**
 | 
						|
 * @link https://www.yiiframework.com/
 | 
						|
 * @copyright Copyright (c) 2008 Yii Software LLC
 | 
						|
 * @license https://www.yiiframework.com/license/
 | 
						|
 */
 | 
						|
 | 
						|
namespace dominion\cache;
 | 
						|
 | 
						|
use Yii;
 | 
						|
use yii\base\InvalidConfigException;
 | 
						|
use yii\di\Instance;
 | 
						|
 | 
						|
/**
 | 
						|
 * Redis Session implements a session component using [redis](https://redis.io/) as the storage medium.
 | 
						|
 *
 | 
						|
 * Redis Session requires redis version 2.6.12 or higher to work properly.
 | 
						|
 *
 | 
						|
 * It needs to be configured with a redis [[Connection]] that is also configured as an application component.
 | 
						|
 * By default it will use the `redis` application component.
 | 
						|
 *
 | 
						|
 * To use redis Session as the session application component, configure the application as follows,
 | 
						|
 *
 | 
						|
 * ~~~
 | 
						|
 * [
 | 
						|
 *     'components' => [
 | 
						|
 *         'session' => [
 | 
						|
 *             'class' => 'yii\redis\Session',
 | 
						|
 *             'redis' => [
 | 
						|
 *                 'hostname' => 'localhost',
 | 
						|
 *                 'port' => 6379,
 | 
						|
 *                 'database' => 0,
 | 
						|
 *             ]
 | 
						|
 *         ],
 | 
						|
 *     ],
 | 
						|
 * ]
 | 
						|
 * ~~~
 | 
						|
 *
 | 
						|
 * Or if you have configured the redis [[Connection]] as an application component, the following is sufficient:
 | 
						|
 *
 | 
						|
 * ~~~
 | 
						|
 * [
 | 
						|
 *     'components' => [
 | 
						|
 *         'session' => [
 | 
						|
 *             'class' => 'yii\redis\Session',
 | 
						|
 *             // 'redis' => 'redis' // id of the connection application component
 | 
						|
 *         ],
 | 
						|
 *     ],
 | 
						|
 * ]
 | 
						|
 * ~~~
 | 
						|
 *
 | 
						|
 * @property-read bool $useCustomStorage Whether to use custom storage.
 | 
						|
 *
 | 
						|
 * @author Carsten Brandt <mail@cebe.cc>
 | 
						|
 * @since 2.0
 | 
						|
 */
 | 
						|
class Session extends \yii\web\Session
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * @var Connection|string|array the Redis [[Connection]] object or the application component ID of the Redis [[Connection]].
 | 
						|
     * This can also be an array that is used to create a redis [[Connection]] instance in case you do not want do configure
 | 
						|
     * redis connection as an application component.
 | 
						|
     * After the Session object is created, if you want to change this property, you should only assign it
 | 
						|
     * with a Redis [[Connection]] object.
 | 
						|
     */
 | 
						|
    public $redis = 'redis';
 | 
						|
    /**
 | 
						|
     * @var string a string prefixed to every cache key so that it is unique. If not set,
 | 
						|
     * it will use a prefix generated from [[Application::id]]. You may set this property to be an empty string
 | 
						|
     * if you don't want to use key prefix. It is recommended that you explicitly set this property to some
 | 
						|
     * static value if the cached data needs to be shared among multiple applications.
 | 
						|
     */
 | 
						|
    public $keyPrefix;
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * Initializes the redis Session component.
 | 
						|
     * This method will initialize the [[redis]] property to make sure it refers to a valid redis connection.
 | 
						|
     * @throws InvalidConfigException if [[redis]] is invalid.
 | 
						|
     */
 | 
						|
    public function init()
 | 
						|
    {
 | 
						|
        $this->redis = Instance::ensure($this->redis, Connection::className());
 | 
						|
        if ($this->keyPrefix === null) {
 | 
						|
            $this->keyPrefix = substr(md5(Yii::$app->id), 0, 5);
 | 
						|
        }
 | 
						|
        parent::init();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 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;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Session open handler.
 | 
						|
     * @internal Do not call this method directly.
 | 
						|
     * @param string $savePath session save path
 | 
						|
     * @param string $sessionName session name
 | 
						|
     * @return bool whether session is opened successfully
 | 
						|
     */
 | 
						|
    public function openSession($savePath, $sessionName)
 | 
						|
    {
 | 
						|
        if ($this->getUseStrictMode()) {
 | 
						|
            $id = $this->getId();
 | 
						|
            if (!$this->redis->exists($this->calculateKey($id))) {
 | 
						|
                //This session id does not exist, mark it for forced regeneration
 | 
						|
                $this->_forceRegenerateId = $id;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        return parent::openSession($savePath, $sessionName);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Session read handler.
 | 
						|
     * Do not call this method directly.
 | 
						|
     * @param string $id session ID
 | 
						|
     * @return string the session data
 | 
						|
     */
 | 
						|
    public function readSession($id)
 | 
						|
    {
 | 
						|
        $data = $this->redis->get($this->calculateKey($id));
 | 
						|
 | 
						|
        return $data === false || $data === null ? '' : $data;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Session write handler.
 | 
						|
     * Do not call this method directly.
 | 
						|
     * @param string $id session ID
 | 
						|
     * @param string $data session data
 | 
						|
     * @return bool whether session write is successful
 | 
						|
     */
 | 
						|
    public function writeSession($id, $data)
 | 
						|
    {
 | 
						|
        if ($this->getUseStrictMode() && $id === $this->_forceRegenerateId) {
 | 
						|
            //Ignore write when forceRegenerate is active for this id
 | 
						|
            return true;
 | 
						|
        }
 | 
						|
 | 
						|
        return (bool) $this->redis->set($this->calculateKey($id), $data, ['EX' => $this->getTimeout()]);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Session destroy handler.
 | 
						|
     * Do not call this method directly.
 | 
						|
     * @param string $id session ID
 | 
						|
     * @return bool whether session is destroyed successfully
 | 
						|
     */
 | 
						|
    public function destroySession($id)
 | 
						|
    {
 | 
						|
        $this->redis->del($this->calculateKey($id));
 | 
						|
        // @see https://github.com/yiisoft/yii2-redis/issues/82
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Generates a unique key used for storing session data in cache.
 | 
						|
     * @param string $id session variable name
 | 
						|
     * @return string a safe cache key associated with the session variable name
 | 
						|
     */
 | 
						|
    protected function calculateKey($id)
 | 
						|
    {
 | 
						|
        return $this->keyPrefix . md5(json_encode([__CLASS__, $id]));
 | 
						|
    }
 | 
						|
}
 |