diff --git a/lib/Documents/src/Events/DocumentLifeCycleSubscriber.php b/lib/Documents/src/Events/DocumentLifeCycleSubscriber.php index eff6e623..db941616 100644 --- a/lib/Documents/src/Events/DocumentLifeCycleSubscriber.php +++ b/lib/Documents/src/Events/DocumentLifeCycleSubscriber.php @@ -4,10 +4,10 @@ namespace RZ\Roadiz\Documents\Events; -use Doctrine\Common\EventSubscriber; +use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; +use Doctrine\ORM\Event\PostRemoveEventArgs; use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Events; -use Doctrine\Persistence\Event\LifecycleEventArgs; use League\Flysystem\FilesystemException; use League\Flysystem\FilesystemOperator; use League\Flysystem\UnableToMoveFile; @@ -18,36 +18,27 @@ /** * Handle file management on document's lifecycle events. */ -class DocumentLifeCycleSubscriber implements EventSubscriber +#[AsDoctrineListener(event: Events::postRemove)] +#[AsDoctrineListener(event: Events::preUpdate)] +final class DocumentLifeCycleSubscriber { - private FilesystemOperator $documentsStorage; - - public function __construct(FilesystemOperator $documentsStorage) - { - $this->documentsStorage = $documentsStorage; - } - - /** - * {@inheritdoc} - */ - public function getSubscribedEvents(): array + public function __construct(private readonly FilesystemOperator $documentsStorage) { - return array( - Events::postRemove, - Events::preUpdate, - ); } /** - * @param PreUpdateEventArgs $args * @throws FilesystemException */ public function preUpdate(PreUpdateEventArgs $args): void { $document = $args->getObject(); + + if (!$document instanceof DocumentInterface) { + return; + } + if ( - $document instanceof DocumentInterface - && $args->hasChangedField('filename') + $args->hasChangedField('filename') && is_string($args->getOldValue('filename')) && is_string($args->getNewValue('filename')) && $args->getOldValue('filename') !== '' @@ -66,21 +57,16 @@ public function preUpdate(PreUpdateEventArgs $args): void } } } - if ($document instanceof DocumentInterface && $args->hasChangedField('private')) { + if ($args->hasChangedField('private')) { if ($document->isPrivate() === true) { - $this->makePrivate($document, $args); + $this->makePrivate($document); } else { - $this->makePublic($document, $args); + $this->makePublic($document); } } } - /** - * @param DocumentInterface $document - * @param PreUpdateEventArgs $args - * @throws FilesystemException - */ - protected function makePublic(DocumentInterface $document, PreUpdateEventArgs $args): void + private function makePublic(DocumentInterface $document): void { $this->validateDocument($document); $documentPublicPath = $this->getDocumentPublicPath($document); @@ -96,12 +82,7 @@ protected function makePublic(DocumentInterface $document, PreUpdateEventArgs $a } } - /** - * @param DocumentInterface $document - * @param PreUpdateEventArgs $args - * @throws FilesystemException - */ - protected function makePrivate(DocumentInterface $document, PreUpdateEventArgs $args): void + private function makePrivate(DocumentInterface $document): void { $this->validateDocument($document); $documentPublicPath = $this->getDocumentPublicPath($document); @@ -120,25 +101,28 @@ protected function makePrivate(DocumentInterface $document, PreUpdateEventArgs $ /** * Unlink file after document has been deleted. * - * @param LifecycleEventArgs $args + * @param PostRemoveEventArgs $args * @throws FilesystemException */ - public function postRemove(LifecycleEventArgs $args): void + public function postRemove(PostRemoveEventArgs $args): void { $document = $args->getObject(); - if ($document instanceof DocumentInterface) { - try { - $this->validateDocument($document); - $document->setRawDocument(null); - $documentPath = $this->getDocumentPath($document); - if ($this->documentsStorage->fileExists($documentPath)) { - $this->documentsStorage->delete($documentPath); - } - $this->cleanFileDirectory($this->getDocumentFolderPath($document)); - } catch (DocumentWithoutFileException $e) { - // Do nothing when document does not have any file on system. + if (!$document instanceof DocumentInterface) { + return; + } + + try { + $this->validateDocument($document); + $document->setRawDocument(null); + $documentPath = $this->getDocumentPath($document); + + if ($this->documentsStorage->fileExists($documentPath)) { + $this->documentsStorage->delete($documentPath); } + $this->cleanFileDirectory($this->getDocumentFolderPath($document)); + } catch (DocumentWithoutFileException $e) { + // Do nothing when document does not have any file on system. } } @@ -149,7 +133,7 @@ public function postRemove(LifecycleEventArgs $args): void * @return void * @throws FilesystemException */ - protected function cleanFileDirectory(string $documentFolderPath): void + private function cleanFileDirectory(string $documentFolderPath): void { if ($this->documentsStorage->directoryExists($documentFolderPath)) { $isDirEmpty = \count($this->documentsStorage->listContents($documentFolderPath)->toArray()) <= 0; @@ -165,7 +149,7 @@ protected function cleanFileDirectory(string $documentFolderPath): void * * @return string */ - protected function getDocumentRelativePathForFilename(DocumentInterface $document, string $filename): string + private function getDocumentRelativePathForFilename(DocumentInterface $document, string $filename): string { $this->validateDocument($document); @@ -178,7 +162,7 @@ protected function getDocumentRelativePathForFilename(DocumentInterface $documen * * @return string */ - protected function getDocumentMountPathForFilename(DocumentInterface $document, string $filename): string + private function getDocumentMountPathForFilename(DocumentInterface $document, string $filename): string { if ($document->isPrivate()) { return 'private://' . $this->getDocumentRelativePathForFilename($document, $filename); @@ -190,7 +174,7 @@ protected function getDocumentMountPathForFilename(DocumentInterface $document, * @param DocumentInterface $document * @return string */ - protected function getDocumentPath(DocumentInterface $document): string + private function getDocumentPath(DocumentInterface $document): string { $this->validateDocument($document); @@ -204,7 +188,7 @@ protected function getDocumentPath(DocumentInterface $document): string * @param DocumentInterface $document * @return string */ - protected function getDocumentPublicPath(DocumentInterface $document): string + private function getDocumentPublicPath(DocumentInterface $document): string { return 'public://' . $document->getRelativePath(); } @@ -213,7 +197,7 @@ protected function getDocumentPublicPath(DocumentInterface $document): string * @param DocumentInterface $document * @return string */ - protected function getDocumentPrivatePath(DocumentInterface $document): string + private function getDocumentPrivatePath(DocumentInterface $document): string { return 'private://' . $document->getRelativePath(); } @@ -222,7 +206,7 @@ protected function getDocumentPrivatePath(DocumentInterface $document): string * @param DocumentInterface $document * @return string */ - protected function getDocumentFolderPath(DocumentInterface $document): string + private function getDocumentFolderPath(DocumentInterface $document): string { if ($document->isPrivate()) { return $this->getDocumentPrivateFolderPath($document); @@ -234,7 +218,7 @@ protected function getDocumentFolderPath(DocumentInterface $document): string * @param DocumentInterface $document * @return string */ - protected function getDocumentPublicFolderPath(DocumentInterface $document): string + private function getDocumentPublicFolderPath(DocumentInterface $document): string { return 'public://' . $document->getFolder(); } @@ -243,7 +227,7 @@ protected function getDocumentPublicFolderPath(DocumentInterface $document): str * @param DocumentInterface $document * @return string */ - protected function getDocumentPrivateFolderPath(DocumentInterface $document): string + private function getDocumentPrivateFolderPath(DocumentInterface $document): string { return 'private://' . $document->getFolder(); } @@ -252,7 +236,7 @@ protected function getDocumentPrivateFolderPath(DocumentInterface $document): st * @param DocumentInterface $document * @throws DocumentWithoutFileException */ - protected function validateDocument(DocumentInterface $document): void + private function validateDocument(DocumentInterface $document): void { if (!$document->isLocal()) { throw new DocumentWithoutFileException($document); diff --git a/lib/Models/src/Core/Events/LeafEntityLifeCycleSubscriber.php b/lib/Models/src/Core/Events/LeafEntityLifeCycleSubscriber.php index 8d56061a..f20b40b4 100644 --- a/lib/Models/src/Core/Events/LeafEntityLifeCycleSubscriber.php +++ b/lib/Models/src/Core/Events/LeafEntityLifeCycleSubscriber.php @@ -4,72 +4,56 @@ namespace RZ\Roadiz\Core\Events; -use Doctrine\Common\EventSubscriber; -use Doctrine\ORM\Event\LifecycleEventArgs; +use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; +use Doctrine\ORM\Event\PrePersistEventArgs; use Doctrine\ORM\Events; use RZ\Roadiz\Core\AbstractEntities\AbstractEntity; use RZ\Roadiz\Core\AbstractEntities\LeafInterface; use RZ\Roadiz\Core\Handlers\HandlerFactoryInterface; -/** - * @package RZ\Roadiz\Core\Events - */ -class LeafEntityLifeCycleSubscriber implements EventSubscriber +#[AsDoctrineListener(event: Events::prePersist)] +final class LeafEntityLifeCycleSubscriber { - private HandlerFactoryInterface $handlerFactory; - - public function __construct(HandlerFactoryInterface $handlerFactory) + public function __construct(private readonly HandlerFactoryInterface $handlerFactory) { - $this->handlerFactory = $handlerFactory; } - /** - * {@inheritdoc} - */ - public function getSubscribedEvents(): array + public function prePersist(PrePersistEventArgs $event): void { - return [ - Events::prePersist, - ]; - } + $entity = $event->getObject(); - /** - * @param LifecycleEventArgs $event - * @return void - */ - public function prePersist(LifecycleEventArgs $event) - { - $entity = $event->getEntity(); - if ($entity instanceof AbstractEntity && $entity instanceof LeafInterface) { - /* - * Automatically set position only if not manually set before. - */ - try { - $handler = $this->handlerFactory->getHandler($entity); + if (!($entity instanceof AbstractEntity) || !($entity instanceof LeafInterface)) { + return; + } - if ($entity->getPosition() === 0.0) { - /* - * Get the last index after last tag in parent - */ - $lastPosition = $handler->cleanPositions(false); - if ($lastPosition > 1 && null !== $entity->getParent()) { - /* - * Need to decrement position because current tag is already - * in parent's children collection count. - */ - $entity->setPosition($lastPosition - 1); - } else { - $entity->setPosition($lastPosition); - } - } elseif ($entity->getPosition() === 0.5) { + /* + * Automatically set position only if not manually set before. + */ + try { + $handler = $this->handlerFactory->getHandler($entity); + + if ($entity->getPosition() === 0.0) { + /* + * Get the last index after last tag in parent + */ + $lastPosition = $handler->cleanPositions(false); + if ($lastPosition > 1 && null !== $entity->getParent()) { /* - * Position is set to 0.5, so we need to - * shift all tags to the bottom. + * Need to decrement position because current tag is already + * in parent's children collection count. */ - $handler->cleanPositions(true); + $entity->setPosition($lastPosition - 1); + } else { + $entity->setPosition($lastPosition); } - } catch (\InvalidArgumentException $e) { + } elseif ($entity->getPosition() === 0.5) { + /* + * Position is set to 0.5, so we need to + * shift all tags to the bottom. + */ + $handler->cleanPositions(true); } + } catch (\InvalidArgumentException $e) { } } } diff --git a/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/NodesSourcesInheritanceSubscriber.php b/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/NodesSourcesInheritanceSubscriber.php index acff2699..ddb6b199 100644 --- a/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/NodesSourcesInheritanceSubscriber.php +++ b/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/NodesSourcesInheritanceSubscriber.php @@ -4,52 +4,40 @@ namespace RZ\Roadiz\CoreBundle\Doctrine\EventSubscriber; -use Doctrine\Common\EventSubscriber; +use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; use Doctrine\ORM\Event\LoadClassMetadataEventArgs; use Doctrine\ORM\Event\PostLoadEventArgs; use Doctrine\ORM\Events; use Doctrine\ORM\Mapping\ClassMetadataInfo; -use Doctrine\ORM\Mapping\PostLoad; use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface; use RZ\Roadiz\CoreBundle\Bag\NodeTypes; use RZ\Roadiz\CoreBundle\DependencyInjection\Configuration; use RZ\Roadiz\CoreBundle\Entity\NodesSources; use RZ\Roadiz\CoreBundle\Entity\NodeType; -final class NodesSourcesInheritanceSubscriber implements EventSubscriber +#[AsDoctrineListener(event: Events::postLoad)] +#[AsDoctrineListener(event: Events::loadClassMetadata)] +final class NodesSourcesInheritanceSubscriber { - private NodeTypes $nodeTypes; - private string $inheritanceType; - /** * @param NodeTypes $nodeTypes * @param string $inheritanceType */ - public function __construct(NodeTypes $nodeTypes, string $inheritanceType) - { - $this->nodeTypes = $nodeTypes; - $this->inheritanceType = $inheritanceType; - } - - /** - * @inheritDoc - */ - public function getSubscribedEvents(): array - { - return [ - Events::loadClassMetadata - ]; + public function __construct( + private NodeTypes $nodeTypes, + private string $inheritanceType + ) { } - #[PostLoad] - public function postLoad(NodesSources $object, PostLoadEventArgs $event): void + public function postLoad(PostLoadEventArgs $event): void { + $object = $event->getObject(); + if (!$object instanceof NodesSources) { + return; + } $object->injectObjectManager($event->getObjectManager()); } - /** - * @param LoadClassMetadataEventArgs $eventArgs - */ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void { // the $metadata is all the mapping info for this class diff --git a/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/TablePrefixSubscriber.php b/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/TablePrefixSubscriber.php deleted file mode 100644 index e01c4c69..00000000 --- a/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/TablePrefixSubscriber.php +++ /dev/null @@ -1,61 +0,0 @@ -tablesPrefix = $tablesPrefix; - } - - - /** - * @inheritDoc - */ - public function getSubscribedEvents(): array - { - return [ - Events::loadClassMetadata, - ]; - } - - /** - * @param LoadClassMetadataEventArgs $eventArgs - */ - public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void - { - /* - * Prefix tables - */ - if (!empty($this->tablesPrefix) && $this->tablesPrefix !== '') { - // the $metadata is all the mapping info for this class - $metadata = $eventArgs->getClassMetadata(); - $metadata->table['name'] = $this->tablesPrefix . '_' . $metadata->table['name']; - - /* - * Prefix join tables - */ - foreach ($metadata->associationMappings as $key => $association) { - if (!empty($association['joinTable']['name'])) { - $metadata->associationMappings[$key]['joinTable']['name'] = - $this->tablesPrefix . '_' . $association['joinTable']['name']; - } - } - } - } -} diff --git a/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php b/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php index 0e3b98f8..1d8dd0c7 100644 --- a/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php +++ b/lib/RoadizCoreBundle/src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php @@ -4,10 +4,10 @@ namespace RZ\Roadiz\CoreBundle\Doctrine\EventSubscriber; -use Doctrine\Common\EventSubscriber; -use Doctrine\Persistence\Event\LifecycleEventArgs; +use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Events; +use Doctrine\Persistence\Event\LifecycleEventArgs; use Psr\Log\LoggerInterface; use RZ\Roadiz\CoreBundle\Entity\User; use RZ\Roadiz\CoreBundle\Event\User\UserCreatedEvent; @@ -22,7 +22,12 @@ use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; -final class UserLifeCycleSubscriber implements EventSubscriber +#[AsDoctrineListener(event: Events::preUpdate)] +#[AsDoctrineListener(event: Events::prePersist)] +#[AsDoctrineListener(event: Events::postPersist)] +#[AsDoctrineListener(event: Events::postUpdate)] +#[AsDoctrineListener(event: Events::postRemove)] +final class UserLifeCycleSubscriber { public function __construct( private readonly UserViewer $userViewer, @@ -33,20 +38,6 @@ public function __construct( ) { } - /** - * {@inheritdoc} - */ - public function getSubscribedEvents(): array - { - return [ - Events::preUpdate, - Events::prePersist, - Events::postPersist, - Events::postUpdate, - Events::postRemove, - ]; - } - /** * @param PreUpdateEventArgs $event * @return void @@ -54,7 +45,7 @@ public function getSubscribedEvents(): array */ public function preUpdate(PreUpdateEventArgs $event): void { - $user = $event->getEntity(); + $user = $event->getObject(); if ($user instanceof User) { if ( $event->hasChangedField('enabled') && diff --git a/lib/RoadizFontBundle/src/Doctrine/EventSubscriber/FontLifeCycleSubscriber.php b/lib/RoadizFontBundle/src/Doctrine/EventSubscriber/FontLifeCycleSubscriber.php index 19b9988a..c3ce3ac8 100644 --- a/lib/RoadizFontBundle/src/Doctrine/EventSubscriber/FontLifeCycleSubscriber.php +++ b/lib/RoadizFontBundle/src/Doctrine/EventSubscriber/FontLifeCycleSubscriber.php @@ -4,9 +4,13 @@ namespace RZ\Roadiz\FontBundle\Doctrine\EventSubscriber; -use Doctrine\Common\EventSubscriber; +use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; +use Doctrine\ORM\Event\PostPersistEventArgs; +use Doctrine\ORM\Event\PostUpdateEventArgs; +use Doctrine\ORM\Event\PrePersistEventArgs; +use Doctrine\ORM\Event\PreRemoveEventArgs; +use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Events; -use Doctrine\Persistence\Event\LifecycleEventArgs; use League\Flysystem\FilesystemException; use League\Flysystem\FilesystemOperator; use Psr\Log\LoggerInterface; @@ -16,36 +20,22 @@ /** * Handle file management on Fonts lifecycle events. */ -final class FontLifeCycleSubscriber implements EventSubscriber +#[AsDoctrineListener(event: Events::prePersist)] +#[AsDoctrineListener(event: Events::preUpdate)] +#[AsDoctrineListener(event: Events::preRemove)] +#[AsDoctrineListener(event: Events::postPersist)] +#[AsDoctrineListener(event: Events::postUpdate)] +final class FontLifeCycleSubscriber { private static array $formats = ['svg', 'otf', 'eot', 'woff', 'woff2']; - private LoggerInterface $logger; - private FilesystemOperator $fontStorage; - public function __construct(FilesystemOperator $fontStorage, LoggerInterface $logger) - { - $this->logger = $logger; - $this->fontStorage = $fontStorage; - } - - /** - * {@inheritdoc} - */ - public function getSubscribedEvents(): array - { - return [ - Events::prePersist, - Events::preUpdate, - Events::preRemove, - Events::postPersist, - Events::postUpdate, - ]; + public function __construct( + private readonly FilesystemOperator $fontStorage, + private readonly LoggerInterface $logger + ) { } - /** - * @param LifecycleEventArgs $args - */ - public function prePersist(LifecycleEventArgs $args): void + public function prePersist(PrePersistEventArgs $args): void { $entity = $args->getObject(); // perhaps you only want to act on some "Font" entity @@ -54,10 +44,7 @@ public function prePersist(LifecycleEventArgs $args): void } } - /** - * @param LifecycleEventArgs $args - */ - public function preUpdate(LifecycleEventArgs $args): void + public function preUpdate(PreUpdateEventArgs $args): void { $entity = $args->getObject(); // perhaps you only want to act on some "Font" entity @@ -66,11 +53,7 @@ public function preUpdate(LifecycleEventArgs $args): void } } - /** - * @param LifecycleEventArgs $args - * @throws FilesystemException - */ - public function postPersist(LifecycleEventArgs $args): void + public function postPersist(PostPersistEventArgs $args): void { $entity = $args->getObject(); // perhaps you only want to act on some "Font" entity @@ -79,11 +62,7 @@ public function postPersist(LifecycleEventArgs $args): void } } - /** - * @param LifecycleEventArgs $args - * @throws FilesystemException - */ - public function postUpdate(LifecycleEventArgs $args): void + public function postUpdate(PostUpdateEventArgs $args): void { $entity = $args->getObject(); // perhaps you only want to act on some "Font" entity @@ -92,7 +71,7 @@ public function postUpdate(LifecycleEventArgs $args): void } } - public function preRemove(LifecycleEventArgs $args): void + public function preRemove(PreRemoveEventArgs $args): void { $entity = $args->getObject(); // perhaps you only want to act on some "Product" entity diff --git a/lib/RoadizFontBundle/src/EventSubscriber/UpdateFontSubscriber.php b/lib/RoadizFontBundle/src/EventSubscriber/UpdateFontSubscriber.php index 64d6a284..1f6c6aa9 100644 --- a/lib/RoadizFontBundle/src/EventSubscriber/UpdateFontSubscriber.php +++ b/lib/RoadizFontBundle/src/EventSubscriber/UpdateFontSubscriber.php @@ -30,7 +30,6 @@ public static function getSubscribedEvents(): array { return [ PreUpdatedFontEvent::class => 'onPreUpdatedFont', - '\RZ\Roadiz\Core\Events\Font\PreUpdatedFontEvent' => 'onPreUpdatedFont', ]; }