Skip to content

[TwigBridge] twig-bridge/AppVariable::getSession() has return type Session instead of SessionInterface #50789

@Dirkhuethorst

Description

@Dirkhuethorst

Symfony version(s) affected

6.3.0

Description

The getSession function in the AppVariable class in TwigBridge has a Session return type. This prevents creating a custom session factory / custom session. Currently the following type error is thrown:
Symfony\Bridge\Twig\AppVariable::getSession(): Return value must be of type ?Symfony\Component\HttpFoundation\Session\Session, App\Framework\CustomSession returned.

Changing the return type to Symfony\Component\HttpFoundation\Session\SessionInterface will resolve the error.

How to reproduce

Decorate the 'session.factory' service with a custom session factory in services.yaml:

App\Framework\CustomSessionFactory:
    decorates: session.factory
    arguments:
      $requestStack: '@request_stack'
      $storageFactory: '@session.storage.factory'

Create a custom session factory:

<?php

declare(strict_types=1);

namespace App\Framework;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionFactoryInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageFactoryInterface;

class CustomSessionFactory implements SessionFactoryInterface {

	public function __construct(private RequestStack $requestStack, private SessionStorageFactoryInterface $storageFactory) {
	}

	public function createSession(): SessionInterface {
		return new CustomSession(
			new Session(
				$this->storageFactory->createStorage($this->requestStack->getMainRequest())
			)
		);
	}
}

Create a custom session class:

<?php

declare(strict_types=1);

namespace App\Framework;

use Symfony\Component\HttpFoundation\Session\SessionInterface;

class CustomSession implements SessionInterface {
	public function __construct(private SessionInterface $session) {
	}
        // ... ///

}

Render a twig template and the error will be thrown

Possible Solution

Change return type of Symfony\Bridge\Twig\AppVariable::getSession() from Session to SessionInterface

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy