DigiComp.FlowSessionLock/Classes/Http/SessionLockRequestComponent.php

70 lines
1.9 KiB
PHP
Raw Normal View History

2021-08-26 15:05:37 +02:00
<?php
namespace DigiComp\FlowSessionLock\Http;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\Component\ComponentContext;
use Neos\Flow\Http\Component\ComponentInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\LockFactory;
class SessionLockRequestComponent implements ComponentInterface
{
2022-03-15 09:32:42 +01:00
public const PARAMETER_NAME = 'sessionLock';
2021-08-26 15:05:37 +02:00
/**
2022-04-05 12:31:33 +02:00
* @Flow\Inject
2021-08-26 15:05:37 +02:00
* @var LoggerInterface
*/
2022-04-05 12:31:33 +02:00
protected $logger;
2021-08-26 15:05:37 +02:00
/**
* @Flow\Inject(name="DigiComp.FlowSessionLock:LockFactory")
* @var LockFactory
*/
protected $lockFactory;
/**
2022-03-15 09:32:42 +01:00
* @Flow\InjectConfiguration(package="Neos.Flow", path="session")
* @var array
*/
2022-03-15 09:32:42 +01:00
protected array $sessionSettings;
/**
* @Flow\InjectConfiguration(package="DigiComp.FlowSessionLock", path="timeToLive")
2022-03-15 09:32:42 +01:00
* @var float
*/
protected float $timeToLive;
/**
* @Flow\InjectConfiguration(package="DigiComp.FlowSessionLock", path="autoRelease")
* @var bool
*/
2022-03-15 09:32:42 +01:00
protected bool $autoRelease;
2021-08-26 15:05:37 +02:00
/**
* @inheritDoc
*/
public function handle(ComponentContext $componentContext)
{
$sessionCookieName = $this->sessionSettings['name'];
2022-03-15 09:32:42 +01:00
$cookies = $componentContext->getHttpRequest()->getCookieParams();
2021-08-26 15:05:37 +02:00
if (!isset($cookies[$sessionCookieName])) {
return;
}
2022-03-15 09:32:42 +01:00
// TODO: sessionIdentifier might be wrong, probably it should probably be storage identifier
$key = new Key('session-' . $cookies[$sessionCookieName]);
2021-08-26 15:05:37 +02:00
$lock = $this->lockFactory->createLockFromKey($key, $this->timeToLive, $this->autoRelease);
2021-08-26 15:05:37 +02:00
2022-03-15 09:32:42 +01:00
$componentContext->setParameter(SessionLockRequestComponent::class, static::PARAMETER_NAME, $lock);
2021-08-26 15:05:37 +02:00
$this->logger->debug('SessionLock: Get ' . $key);
$lock->acquire(true);
$this->logger->debug('SessionLock: Acquired ' . $key);
}
}