diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c00f45d..d4a21d9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,7 @@ jobs: fail-fast: false matrix: operating-system: [ ubuntu-latest ] - php-versions: [ '8.0', '8.1' ] + php-versions: [ '8.0', '8.1', '8.2' ] steps: - name: Checkout diff --git a/DependencyInjection/Compiler/LoggerFactoryPass.php b/DependencyInjection/Compiler/LoggerFactoryPass.php index a734aff..8d8b1ba 100644 --- a/DependencyInjection/Compiler/LoggerFactoryPass.php +++ b/DependencyInjection/Compiler/LoggerFactoryPass.php @@ -17,7 +17,7 @@ class LoggerFactoryPass implements CompilerPassInterface { - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { if (false === $container->hasDefinition('xiidea.easy_audit.logger_factory')) { return; diff --git a/DependencyInjection/Compiler/MonologLoggerPass.php b/DependencyInjection/Compiler/MonologLoggerPass.php index 2bcccd2..aa764c4 100644 --- a/DependencyInjection/Compiler/MonologLoggerPass.php +++ b/DependencyInjection/Compiler/MonologLoggerPass.php @@ -16,7 +16,7 @@ class MonologLoggerPass implements CompilerPassInterface { - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { if (false === $container->hasAlias('logger')) { $container->removeDefinition('xiidea.easy_audit.mono_logger.service'); diff --git a/DependencyInjection/Compiler/ResolverFactoryPass.php b/DependencyInjection/Compiler/ResolverFactoryPass.php index db99565..28caada 100644 --- a/DependencyInjection/Compiler/ResolverFactoryPass.php +++ b/DependencyInjection/Compiler/ResolverFactoryPass.php @@ -17,7 +17,7 @@ class ResolverFactoryPass implements CompilerPassInterface { - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { if (false === $container->hasDefinition('xiidea.easy_audit.event_resolver_factory')) { return; diff --git a/DependencyInjection/Compiler/SubscriberPass.php b/DependencyInjection/Compiler/SubscriberPass.php index 0259659..4a676d2 100644 --- a/DependencyInjection/Compiler/SubscriberPass.php +++ b/DependencyInjection/Compiler/SubscriberPass.php @@ -17,7 +17,7 @@ class SubscriberPass implements CompilerPassInterface { - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { if (false === $container->hasDefinition('xiidea.easy_audit.event_listener')) { return; diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 037902f..45edc3d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -28,7 +28,7 @@ class Configuration implements ConfigurationInterface /** * {@inheritdoc} */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder(self::ROOT_NODE_NAME); diff --git a/DependencyInjection/XiideaEasyAuditExtension.php b/DependencyInjection/XiideaEasyAuditExtension.php index f0fa186..514a9a0 100644 --- a/DependencyInjection/XiideaEasyAuditExtension.php +++ b/DependencyInjection/XiideaEasyAuditExtension.php @@ -28,7 +28,7 @@ class XiideaEasyAuditExtension extends Extension implements PrependExtensionInte /** * {@inheritdoc} */ - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); @@ -72,7 +72,7 @@ protected function loadDefaultResolverServices($config, LoaderInterface $loader) * Allow an extension to prepend the extension configurations. * @param ContainerBuilder $container */ - public function prepend(ContainerBuilder $container) + public function prepend(ContainerBuilder $container): void { $prependConfig = $this->getExtendedConfig($container); @@ -86,7 +86,7 @@ public function prepend(ContainerBuilder $container) * * @return array */ - protected function getExtendedConfig(ContainerBuilder $container) + protected function getExtendedConfig(ContainerBuilder $container): array { $configs = array_merge(...$container->getExtensionConfig($this->getAlias())); diff --git a/Resources/config/doctrine_services.yml b/Resources/config/doctrine_services.yml index 099500b..8aea89e 100644 --- a/Resources/config/doctrine_services.yml +++ b/Resources/config/doctrine_services.yml @@ -7,5 +7,11 @@ services: - [ setDispatcher,[ '@event_dispatcher' ] ] - [ setAnnotationReader,[ '@annotation_reader' ] ] tags: - - { name: doctrine.event_subscriber } - - { name: doctrine_mongodb.odm.event_subscriber } + - { name: doctrine.event_listener, event: preRemove } + - { name: doctrine.event_listener, event: postUpdate } + - { name: doctrine.event_listener, event: postRemove } + - { name: doctrine.event_listener, event: postPersist } + - { name: doctrine_mongodb.odm.event_listener, event: preRemove } + - { name: doctrine_mongodb.odm.event_listener, event: postUpdate } + - { name: doctrine_mongodb.odm.event_listener, event: postRemove } + - { name: doctrine_mongodb.odm.event_listener, event: postPersist } diff --git a/Subscriber/DoctrineDeleteEventLogger.php b/Subscriber/DoctrineDeleteEventLogger.php index 63c5b22..b201a90 100644 --- a/Subscriber/DoctrineDeleteEventLogger.php +++ b/Subscriber/DoctrineDeleteEventLogger.php @@ -41,7 +41,7 @@ public function savePendingLogs() /** * @return array */ - public static function getSubscribedEvents() + public static function getSubscribedEvents(): array { return [ ConsoleEvents::TERMINATE => 'savePendingLogs', diff --git a/Subscriber/DoctrineSubscriber.php b/Subscriber/DoctrineSubscriber.php index 221bf94..0c6bc47 100644 --- a/Subscriber/DoctrineSubscriber.php +++ b/Subscriber/DoctrineSubscriber.php @@ -11,46 +11,26 @@ namespace Xiidea\EasyAuditBundle\Subscriber; -use Doctrine\Common\EventSubscriber; use Doctrine\Common\Util\ClassUtils; use Doctrine\Persistence\Event\LifecycleEventArgs; -use Symfony\Component\EventDispatcher\EventDispatcher; use Xiidea\EasyAuditBundle\Annotation\SubscribeDoctrineEvents; -use Xiidea\EasyAuditBundle\Events\DoctrineObjectEvent; use Xiidea\EasyAuditBundle\Events\DoctrineEvents; +use Xiidea\EasyAuditBundle\Events\DoctrineObjectEvent; -class DoctrineSubscriber implements EventSubscriber +#[AsDoctrineListener] +class DoctrineSubscriber { /** @var \Doctrine\Common\Annotations\Reader */ private $annotationReader; + private array $toBeDeleted = []; + private $dispatcher = null; + private array $entities; - private $toBeDeleted = []; - - /** - * @var EventDispatcher - */ - private $dispatcher; - - /** - * @var array - */ - private $entities; - - public function __construct($entities = array()) + public function __construct($entities = []) { $this->entities = $entities; } - public function getSubscribedEvents() - { - return array( - 'postPersist', - 'postUpdate', - 'preRemove', - 'postRemove', - ); - } - public function postPersist(LifecycleEventArgs $args) { $this->handleEvent(DoctrineEvents::ENTITY_CREATED, $args); @@ -95,13 +75,14 @@ private function getToBeDeletedId($entity) } /** - * @param string $eventName - * @param LifecycleEventArgs $args + * @param string $eventName + * @param LifecycleEventArgs $args */ private function handleEvent($eventName, LifecycleEventArgs $args) { if (true === $this->isConfiguredToTrack($args->getObject(), $eventName)) { - $this->dispatcher->dispatch(new DoctrineObjectEvent($args, $this->getIdentity($args, ClassUtils::getClass($args->getObject()))), + $this->dispatcher->dispatch( + new DoctrineObjectEvent($args, $this->getIdentity($args, ClassUtils::getClass($args->getObject()))), $eventName ); } @@ -109,7 +90,7 @@ private function handleEvent($eventName, LifecycleEventArgs $args) /** * @param $entity - * @param string $eventName + * @param string $eventName * * @return bool */ @@ -135,7 +116,7 @@ private function isConfiguredToTrack($entity, $eventName = '') /** * @param $entity - * @param string $eventType + * @param string $eventType * * @return bool|null */ @@ -185,8 +166,8 @@ protected function getReflectionClassFromObject($object) } /** - * @param string $eventType - * @param string $class + * @param string $eventType + * @param string $class * * @return bool */ @@ -196,7 +177,7 @@ private function shouldTrackEventType($eventType, $class) } /** - * @param string $class + * @param string $class * * @return bool */ @@ -206,7 +187,7 @@ private function shouldTrackAllEventType($class) } /** - * @param \Doctrine\Common\Annotations\Reader $annotationReader + * @param \Doctrine\Common\Annotations\Reader $annotationReader */ public function setAnnotationReader($annotationReader = null) { @@ -214,7 +195,7 @@ public function setAnnotationReader($annotationReader = null) } /** - * @param LifecycleEventArgs $args + * @param LifecycleEventArgs $args * @param $className * * @return array @@ -233,11 +214,15 @@ private function isScheduledForDelete($entity) { $originalClassName = ClassUtils::getClass($entity); - return isset($this->toBeDeleted[$originalClassName]) && isset($this->toBeDeleted[$originalClassName][spl_object_hash($entity)]); + return isset($this->toBeDeleted[$originalClassName]) && isset( + $this->toBeDeleted[$originalClassName][spl_object_hash( + $entity + )] + ); } /** - * @param EventDispatcher $dispatcher + * @param EventDispatcherInterface $dispatcher */ public function setDispatcher($dispatcher) { diff --git a/Subscriber/EasyAuditEventSubscriberInterface.php b/Subscriber/EasyAuditEventSubscriberInterface.php index 7b617ce..55f2440 100644 --- a/Subscriber/EasyAuditEventSubscriberInterface.php +++ b/Subscriber/EasyAuditEventSubscriberInterface.php @@ -18,5 +18,5 @@ interface EasyAuditEventSubscriberInterface * * @return array */ - public function getSubscribedEvents(); + public function getSubscribedEvents(): array; } diff --git a/Tests/Fixtures/Common/EasySubscriberOne.php b/Tests/Fixtures/Common/EasySubscriberOne.php index edc1b1a..65019ae 100644 --- a/Tests/Fixtures/Common/EasySubscriberOne.php +++ b/Tests/Fixtures/Common/EasySubscriberOne.php @@ -20,7 +20,7 @@ class EasySubscriberOne implements EasyAuditEventSubscriberInterface * * @return array */ - public function getSubscribedEvents() + public function getSubscribedEvents(): array { return array( 'custom_resolver1' => 'event1', diff --git a/Tests/Fixtures/Common/EasySubscriberTwo.php b/Tests/Fixtures/Common/EasySubscriberTwo.php index ffe52e8..d01a13f 100644 --- a/Tests/Fixtures/Common/EasySubscriberTwo.php +++ b/Tests/Fixtures/Common/EasySubscriberTwo.php @@ -20,7 +20,7 @@ class EasySubscriberTwo implements EasyAuditEventSubscriberInterface * * @return array */ - public function getSubscribedEvents() + public function getSubscribedEvents(): array { return array( 'common_event1', diff --git a/Tests/Subscriber/DoctrineSubscriberTest.php b/Tests/Subscriber/DoctrineSubscriberTest.php index 3b2a51e..91c7688 100644 --- a/Tests/Subscriber/DoctrineSubscriberTest.php +++ b/Tests/Subscriber/DoctrineSubscriberTest.php @@ -47,21 +47,6 @@ public function setUp(): void { ->getMock(); } - public function testInstanceOnSubscriber() - { - $this->assertInstanceOf('Doctrine\Common\EventSubscriber', new DoctrineSubscriber()); - } - - public function testSubscribedEvents() - { - $subscriber = new DoctrineSubscriber(); - $this->assertEquals(array( - 'postPersist', - 'postUpdate', - 'preRemove', - 'postRemove', - ), $subscriber->getSubscribedEvents()); - } public function testCreateEventForAnnotatedEntity() { diff --git a/XiideaEasyAuditBundle.php b/XiideaEasyAuditBundle.php index ad75112..4440192 100644 --- a/XiideaEasyAuditBundle.php +++ b/XiideaEasyAuditBundle.php @@ -20,7 +20,7 @@ class XiideaEasyAuditBundle extends Bundle { - public function build(ContainerBuilder $container) + public function build(ContainerBuilder $container): void { parent::build($container);