Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Symfony 6] Fix session storage #422

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ parameters:
- %currentWorkingDirectory%/src/Component/vendor/*

ignoreErrors:
- '/Call to an undefined method Symfony\\Component\\HttpFoundation\\RequestStack::getSession\(\)/'
- '/Call to method getArguments\(\) on an unknown class ReflectionAttribute./'
- '/Call to an undefined method ReflectionClass::getAttributes\(\)./'
- '/Class Doctrine\\Bundle\\MongoDBBundle/'
Expand Down
3 changes: 2 additions & 1 deletion src/Bundle/Resources/config/services/storage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
<defaults public="true" />

<service id="sylius.storage.session" class="Sylius\Bundle\ResourceBundle\Storage\SessionStorage">
<argument type="service" id="session" />
<argument type="service" id="session" on-invalid="null" />
<argument type="service" id="request_stack" />
</service>

<service id="sylius.storage.cookie" class="Sylius\Bundle\ResourceBundle\Storage\CookieStorage">
Expand Down
23 changes: 16 additions & 7 deletions src/Bundle/Storage/SessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,48 @@
namespace Sylius\Bundle\ResourceBundle\Storage;

use Sylius\Component\Resource\Storage\StorageInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

final class SessionStorage implements StorageInterface
{
private SessionInterface $session;
private ?SessionInterface $session;

public function __construct(SessionInterface $session)
private RequestStack $requestStack;

public function __construct(?SessionInterface $session, RequestStack $requestStack)
{
$this->session = $session;
$this->requestStack = $requestStack;
}

public function has(string $name): bool
{
return $this->session->has($name);
return $this->getSession()->has($name);
}

public function get(string $name, $default = null)
{
return $this->session->get($name, $default);
return $this->getSession()->get($name, $default);
}

public function set(string $name, $value): void
{
$this->session->set($name, $value);
$this->getSession()->set($name, $value);
}

public function remove(string $name): void
{
$this->session->remove($name);
$this->getSession()->remove($name);
}

public function all(): array
{
return $this->session->all();
return $this->getSession()->all();
}

private function getSession(): SessionInterface
{
return $this->session ?: $this->requestStack->getSession();
}
}
30 changes: 28 additions & 2 deletions src/Bundle/spec/Storage/SessionStorageSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,47 @@

use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Storage\StorageInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\Kernel;

final class SessionStorageSpec extends ObjectBehavior
{
function let(): void
function let(RequestStack $requestStack): void
{
$this->beConstructedWith(new Session(new MockArraySessionStorage()));
$this->beConstructedWith(new Session(new MockArraySessionStorage()), $requestStack);
}

function it_is_a_storage(): void
{
$this->shouldImplement(StorageInterface::class);
}

function its_session_can_be_retrieved_from_container(RequestStack $requestStack): void
{
if (Kernel::MAJOR_VERSION > 4) {
$requestStack->getSession()->shouldNotBeCalled();
}

$this->beConstructedWith(new Session(new MockArraySessionStorage()), $requestStack);

$this->get('name');
}

function its_session_can_be_retrieved_from_request_stack(RequestStack $requestStack): void
{
if (Kernel::MAJOR_VERSION === 4) {
return;
}

$requestStack->getSession()->shouldBeCalled();

$this->beConstructedWith(null, $requestStack);

$this->get('name');
}

function it_does_not_have_a_named_value_if_it_was_not_set_previously(): void
{
$this->get('name')->shouldReturn(null);
Expand Down