DigiComp.FlowSessionLock/Classes/Aspects/ReadOnlyAspect.php

80 lines
2.1 KiB
PHP
Raw Permalink Normal View History

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\Aspects;
use DigiComp\FlowSessionLock\Http\SessionLockRequestMiddleware;
2021-08-26 15:05:37 +02:00
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\Unlock) || filter(DigiComp\FlowSessionLock\Aspects\ReadOnlyFilter)")
2021-08-26 15:05:37 +02:00
* @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->getHttpRequest()->getAttribute(
SessionLockRequestMiddleware::class . '.' . SessionLockRequestMiddleware::PARAMETER_NAME
2022-03-15 09:32:42 +01:00
);
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
}
}