master

laravel/framework

Last updated at: 29/12/2023 09:24

ArraySessionHandler.php

TLDR

The ArraySessionHandler.php file is a part of the Illuminate\Session namespace in the Demo Projects project. It contains a class named ArraySessionHandler, which implements the SessionHandlerInterface interface. This class is responsible for handling session data by storing and retrieving values in an array.

Methods

__construct($minutes)

This method is the constructor of the ArraySessionHandler class. It accepts the number of minutes the session should be valid and sets it as a property of the class.

open($savePath, $sessionName): bool

This method is implemented from the SessionHandlerInterface interface. It is responsible for opening the session. In this case, it always returns true, indicating that the session opening was successful.

close(): bool

This method is implemented from the SessionHandlerInterface interface. It is responsible for closing the session. In this case, it always returns true, indicating that the session closing was successful.

read($sessionId): string|false

This method is implemented from the SessionHandlerInterface interface. It is responsible for reading the session data based on the provided session ID. If the session data exists and is not expired, it returns the data as a string. Otherwise, it returns an empty string.

write($sessionId, $data): bool

This method is implemented from the SessionHandlerInterface interface. It is responsible for writing the session data to the storage array. It stores the data and the current time as an associative array in the $storage property of the class. It always returns true, indicating that the data was successfully written.

destroy($sessionId): bool

This method is implemented from the SessionHandlerInterface interface. It is responsible for destroying the specified session by removing it from the storage array. If the session exists, it is unset from the $storage property. It always returns true, indicating that the session was successfully destroyed.

gc($lifetime): int

This method is implemented from the SessionHandlerInterface interface. It is responsible for performing garbage collection on expired sessions. It calculates the expiration time based on the provided lifetime in seconds. It then iterates over the $storage array and removes any sessions that have expired. The method returns the number of deleted sessions.

calculateExpiration($seconds)

This protected method calculates the expiration time of the session based on the provided number of seconds. It subtracts the seconds from the current time and returns the result.

Classes

Class ArraySessionHandler

The ArraySessionHandler class implements the SessionHandlerInterface and is responsible for handling the storage of session data using an array. It provides methods for opening, closing, reading, writing, destroying sessions, and performing garbage collection on expired sessions. Sessions are stored in the $storage property array.

<?php

namespace Illuminate\Session;

use Illuminate\Support\InteractsWithTime;
use SessionHandlerInterface;

class ArraySessionHandler implements SessionHandlerInterface
{
    use InteractsWithTime;

    /**
     * The array of stored values.
     *
     * @var array
     */
    protected $storage = [];

    /**
     * The number of minutes the session should be valid.
     *
     * @var int
     */
    protected $minutes;

    /**
     * Create a new array driven handler instance.
     *
     * @param  int  $minutes
     * @return void
     */
    public function __construct($minutes)
    {
        $this->minutes = $minutes;
    }

    /**
     * {@inheritdoc}
     *
     * @return bool
     */
    public function open($savePath, $sessionName): bool
    {
        return true;
    }

    /**
     * {@inheritdoc}
     *
     * @return bool
     */
    public function close(): bool
    {
        return true;
    }

    /**
     * {@inheritdoc}
     *
     * @return string|false
     */
    public function read($sessionId): string|false
    {
        if (! isset($this->storage[$sessionId])) {
            return '';
        }

        $session = $this->storage[$sessionId];

        $expiration = $this->calculateExpiration($this->minutes * 60);

        if (isset($session['time']) && $session['time'] >= $expiration) {
            return $session['data'];
        }

        return '';
    }

    /**
     * {@inheritdoc}
     *
     * @return bool
     */
    public function write($sessionId, $data): bool
    {
        $this->storage[$sessionId] = [
            'data' => $data,
            'time' => $this->currentTime(),
        ];

        return true;
    }

    /**
     * {@inheritdoc}
     *
     * @return bool
     */
    public function destroy($sessionId): bool
    {
        if (isset($this->storage[$sessionId])) {
            unset($this->storage[$sessionId]);
        }

        return true;
    }

    /**
     * {@inheritdoc}
     *
     * @return int
     */
    public function gc($lifetime): int
    {
        $expiration = $this->calculateExpiration($lifetime);

        $deletedSessions = 0;

        foreach ($this->storage as $sessionId => $session) {
            if ($session['time'] < $expiration) {
                unset($this->storage[$sessionId]);
                $deletedSessions++;
            }
        }

        return $deletedSessions;
    }

    /**
     * Get the expiration time of the session.
     *
     * @param  int  $seconds
     * @return int
     */
    protected function calculateExpiration($seconds)
    {
        return $this->currentTime() - $seconds;
    }
}