2021-08-26 15:05:37 +02:00
|
|
|
<?php
|
|
|
|
|
2022-05-02 09:56:10 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-08-26 15:05:37 +02:00
|
|
|
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;
|
2022-05-16 11:28:39 +02:00
|
|
|
use Symfony\Component\Lock\Exception\LockAcquiringException;
|
2021-08-26 15:05:37 +02:00
|
|
|
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-14 08:53:06 +01:00
|
|
|
/**
|
2022-03-15 09:32:42 +01:00
|
|
|
* @Flow\InjectConfiguration(package="Neos.Flow", path="session")
|
|
|
|
* @var array
|
2022-03-14 08:53:06 +01:00
|
|
|
*/
|
2022-03-15 09:32:42 +01:00
|
|
|
protected array $sessionSettings;
|
2022-03-14 08:53:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-14 08:53:06 +01:00
|
|
|
*/
|
2022-03-15 09:32:42 +01:00
|
|
|
protected bool $autoRelease;
|
2022-03-14 08:53:06 +01:00
|
|
|
|
2022-05-16 11:28:39 +02:00
|
|
|
/**
|
|
|
|
* @Flow\InjectConfiguration(package="DigiComp.FlowSessionLock", path="secondsToWait")
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected int $secondsToWait;
|
|
|
|
|
2021-08-26 15:05:37 +02:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2022-04-05 15:19:26 +02:00
|
|
|
public function handle(ComponentContext $componentContext): void
|
2021-08-26 15:05:37 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
2022-03-14 08:53:06 +01: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
|
|
|
|
2022-05-16 11:28:39 +02:00
|
|
|
$this->logger->debug('SessionLock: Try to get "' . $key . '"');
|
|
|
|
$timedOut = \time() + $this->secondsToWait;
|
2022-05-16 12:04:25 +02:00
|
|
|
while (!$lock->acquire()) {
|
|
|
|
if (\time() >= $timedOut) {
|
|
|
|
throw new LockAcquiringException(
|
|
|
|
'Could not acquire the lock for "' . $key . '" in ' . $this->secondsToWait . ' seconds.',
|
|
|
|
1652687960
|
|
|
|
);
|
|
|
|
}
|
2022-05-16 12:08:39 +02:00
|
|
|
\usleep(100000);
|
2022-05-16 11:28:39 +02:00
|
|
|
}
|
|
|
|
$this->logger->debug('SessionLock: Acquired "' . $key . '"');
|
2021-08-26 15:05:37 +02:00
|
|
|
}
|
|
|
|
}
|