DigiComp.FlowSessionLock/Classes/Aspects/ReadOnlyAspect.php

79 lines
2.1 KiB
PHP
Raw Normal View History

2021-08-26 15:05:37 +02:00
<?php
namespace DigiComp\FlowSessionLock\Aspects;
use DigiComp\FlowSessionLock\Http\SessionLockRequestComponent;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Http\HttpRequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Lock\Lock;
/**
* @Flow\Aspect
* @Flow\Scope("singleton")
*/
class ReadOnlyAspect
{
/**
* @Flow\Inject
* @var Bootstrap
*/
protected $bootstrap;
/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;
/**
* @var bool
*/
protected bool $readOnly = false;
/**
* @Flow\Around("methodAnnotatedWith(DigiComp\FlowSessionLock\Annotations\ReadOnly) || filter(DigiComp\FlowSessionLock\Aspects\ReadOnlyFilter)")
* @param JoinPointInterface $joinPoint
2022-03-15 09:32:42 +01:00
* @return mixed
2021-08-26 15:05:37 +02:00
*/
public function demoteLockToReadOnly(JoinPointInterface $joinPoint)
{
2022-03-15 09:32:42 +01:00
$activeRequestHandler = $this->bootstrap->getActiveRequestHandler();
if (!$activeRequestHandler instanceof HttpRequestHandlerInterface) {
$this->logger->debug('SessionLock: ' . \get_class($activeRequestHandler));
2021-08-26 15:05:37 +02:00
return $joinPoint->getAdviceChain()->proceed($joinPoint);
}
2022-03-15 09:32:42 +01:00
2021-08-26 15:05:37 +02:00
$this->readOnly = true;
2022-03-15 09:32:42 +01:00
/** @var Lock|null $lock */
$lock = $activeRequestHandler->getComponentContext()->getParameter(
SessionLockRequestComponent::class,
SessionLockRequestComponent::PARAMETER_NAME
);
if ($lock !== null) {
$this->logger->debug('SessionLock: Release, as this is marked read only.');
2021-08-26 15:05:37 +02:00
$lock->release();
}
2022-03-15 09:32:42 +01:00
2021-08-26 15:05:37 +02:00
return $joinPoint->getAdviceChain()->proceed($joinPoint);
}
/**
* @Flow\Around("method(Neos\Flow\Session\Session->shutdownObject())")
* @param JoinPointInterface $joinPoint
2022-03-15 09:32:42 +01:00
* @return mixed|void
2021-08-26 15:05:37 +02:00
*/
public function doNotSaveSession(JoinPointInterface $joinPoint)
{
if ($this->readOnly) {
return;
}
2022-03-15 09:32:42 +01:00
return $joinPoint->getAdviceChain()->proceed($joinPoint);
2021-08-26 15:05:37 +02:00
}
}