master

laravel/framework

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

CookieSessionHandler.php

TLDR

The CookieSessionHandler.php file contains the implementation of the CookieSessionHandler class, which is responsible for handling session data using cookies. It implements the SessionHandlerInterface interface and provides methods for opening, closing, reading, writing, and destroying sessions.

Methods

open

This method is called when a session is being opened. It takes the save path and session name as parameters and returns a boolean value indicating whether the operation was successful or not.

close

This method is called when a session is being closed. It does not take any parameters and returns a boolean value indicating whether the operation was successful or not.

read

This method is called when session data needs to be read. It takes the session ID as a parameter and returns the corresponding session data as a string. If the session data is not found, it returns an empty string.

write

This method is called when session data needs to be written. It takes the session ID and session data as parameters. It encodes the session data as JSON and stores it in a cookie. It returns a boolean value indicating whether the operation was successful or not.

destroy

This method is called when a session needs to be destroyed. It takes the session ID as a parameter and removes the corresponding session cookie. It returns a boolean value indicating whether the operation was successful or not.

gc

This method is called by PHP's garbage collector. It takes the maximum lifetime of a session as a parameter and does not perform any action. It returns the number of deleted sessions, which in this case is always zero.

Classes

CookieSessionHandler

This class implements the SessionHandlerInterface interface and provides methods for handling session data using cookies. It has the following properties:

  • $cookie: The cookie jar instance.
  • $request: The request instance.
  • $minutes: The number of minutes the session should be valid.
  • $expireOnClose: Indicates whether the session should be expired when the browser closes.

The class also uses the InteractsWithTime trait, which provides functionality for working with timestamps.

The class constructor accepts the cookie jar, session duration in minutes, and an optional parameter to specify if the session should expire when the browser closes.

The class includes the methods described above for handling session operations such as opening, closing, reading, writing, and destroying sessions. It also includes a method setRequest to set the request instance.

<?php

namespace Illuminate\Session;

use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;
use Illuminate\Support\InteractsWithTime;
use SessionHandlerInterface;
use Symfony\Component\HttpFoundation\Request;

class CookieSessionHandler implements SessionHandlerInterface
{
    use InteractsWithTime;

    /**
     * The cookie jar instance.
     *
     * @var \Illuminate\Contracts\Cookie\Factory
     */
    protected $cookie;

    /**
     * The request instance.
     *
     * @var \Symfony\Component\HttpFoundation\Request
     */
    protected $request;

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

    /**
     * Indicates whether the session should be expired when the browser closes.
     *
     * @var bool
     */
    protected $expireOnClose;

    /**
     * Create a new cookie driven handler instance.
     *
     * @param  \Illuminate\Contracts\Cookie\QueueingFactory  $cookie
     * @param  int  $minutes
     * @param  bool  $expireOnClose
     * @return void
     */
    public function __construct(CookieJar $cookie, $minutes, $expireOnClose = false)
    {
        $this->cookie = $cookie;
        $this->minutes = $minutes;
        $this->expireOnClose = $expireOnClose;
    }

    /**
     * {@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
    {
        $value = $this->request->cookies->get($sessionId) ?: '';

        if (! is_null($decoded = json_decode($value, true)) && is_array($decoded) &&
            isset($decoded['expires']) && $this->currentTime() <= $decoded['expires']) {
            return $decoded['data'];
        }

        return '';
    }

    /**
     * {@inheritdoc}
     *
     * @return bool
     */
    public function write($sessionId, $data): bool
    {
        $this->cookie->queue($sessionId, json_encode([
            'data' => $data,
            'expires' => $this->availableAt($this->minutes * 60),
        ]), $this->expireOnClose ? 0 : $this->minutes);

        return true;
    }

    /**
     * {@inheritdoc}
     *
     * @return bool
     */
    public function destroy($sessionId): bool
    {
        $this->cookie->queue($this->cookie->forget($sessionId));

        return true;
    }

    /**
     * {@inheritdoc}
     *
     * @return int
     */
    public function gc($lifetime): int
    {
        return 0;
    }

    /**
     * Set the request instance.
     *
     * @param  \Symfony\Component\HttpFoundation\Request  $request
     * @return void
     */
    public function setRequest(Request $request)
    {
        $this->request = $request;
    }
}