master

laravel/framework

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

SymfonySessionDecorator.php

TLDR

This file contains the SymfonySessionDecorator class, which is a decorator for the Laravel session store. It implements the SessionInterface interface from Symfony's HttpFoundation component. The decorator simply proxies all method calls to the underlying Laravel session store.

Classes

SymfonySessionDecorator

The SymfonySessionDecorator class is a decorator for the Laravel session store. It implements the SessionInterface interface from Symfony's HttpFoundation component. The decorator proxies all method calls to the underlying Laravel session store. It has the following public methods:

  • start(): Starts the session.
  • getId(): string: Returns the session ID.
  • setId(string $id): void: Sets the session ID.
  • getName(): string: Returns the session name.
  • setName(string $name): void: Sets the session name.
  • invalidate(int $lifetime = null): bool: Invalidates the session.
  • migrate(bool $destroy = false, int $lifetime = null): bool: Migrates the session to a new ID.
  • save(): void: Saves the session data.
  • has(string $name): bool: Checks if a session variable exists.
  • get(string $name, mixed $default = null): mixed: Returns the value of a session variable.
  • set(string $name, mixed $value): void: Sets the value of a session variable.
  • all(): array: Returns all session variables.
  • replace(array $attributes): void: Replaces all session variables with the given array.
  • remove(string $name): mixed: Removes a session variable.
  • clear(): void: Clears all session variables.
  • isStarted(): bool: Checks if the session has been started.
  • registerBag(SessionBagInterface $bag): void: Registers a session bag (not implemented).
  • getBag(string $name): SessionBagInterface: Retrieves a session bag (not implemented).
  • getMetadataBag(): MetadataBag: Retrieves the metadata bag (not implemented).
<?php

namespace Illuminate\Session;

use BadMethodCallException;
use Illuminate\Contracts\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;

class SymfonySessionDecorator implements SessionInterface
{
    /**
     * The underlying Laravel session store.
     *
     * @var \Illuminate\Contracts\Session\Session
     */
    public readonly Session $store;

    /**
     * Create a new session decorator.
     *
     * @param  \Illuminate\Contracts\Session\Session  $store
     * @return void
     */
    public function __construct(Session $store)
    {
        $this->store = $store;
    }

    /**
     * {@inheritdoc}
     */
    public function start(): bool
    {
        return $this->store->start();
    }

    /**
     * {@inheritdoc}
     */
    public function getId(): string
    {
        return $this->store->getId();
    }

    /**
     * {@inheritdoc}
     */
    public function setId(string $id): void
    {
        $this->store->setId($id);
    }

    /**
     * {@inheritdoc}
     */
    public function getName(): string
    {
        return $this->store->getName();
    }

    /**
     * {@inheritdoc}
     */
    public function setName(string $name): void
    {
        $this->store->setName($name);
    }

    /**
     * {@inheritdoc}
     */
    public function invalidate(int $lifetime = null): bool
    {
        $this->store->invalidate();

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function migrate(bool $destroy = false, int $lifetime = null): bool
    {
        $this->store->migrate($destroy);

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function save(): void
    {
        $this->store->save();
    }

    /**
     * {@inheritdoc}
     */
    public function has(string $name): bool
    {
        return $this->store->has($name);
    }

    /**
     * {@inheritdoc}
     */
    public function get(string $name, mixed $default = null): mixed
    {
        return $this->store->get($name, $default);
    }

    /**
     * {@inheritdoc}
     */
    public function set(string $name, mixed $value): void
    {
        $this->store->put($name, $value);
    }

    /**
     * {@inheritdoc}
     */
    public function all(): array
    {
        return $this->store->all();
    }

    /**
     * {@inheritdoc}
     */
    public function replace(array $attributes): void
    {
        $this->store->replace($attributes);
    }

    /**
     * {@inheritdoc}
     */
    public function remove(string $name): mixed
    {
        return $this->store->remove($name);
    }

    /**
     * {@inheritdoc}
     */
    public function clear(): void
    {
        $this->store->flush();
    }

    /**
     * {@inheritdoc}
     */
    public function isStarted(): bool
    {
        return $this->store->isStarted();
    }

    /**
     * {@inheritdoc}
     */
    public function registerBag(SessionBagInterface $bag): void
    {
        throw new BadMethodCallException('Method not implemented by Laravel.');
    }

    /**
     * {@inheritdoc}
     */
    public function getBag(string $name): SessionBagInterface
    {
        throw new BadMethodCallException('Method not implemented by Laravel.');
    }

    /**
     * {@inheritdoc}
     */
    public function getMetadataBag(): MetadataBag
    {
        throw new BadMethodCallException('Method not implemented by Laravel.');
    }
}