From 5a6d14b57d1816c1554933431e377c6b49192862 Mon Sep 17 00:00:00 2001 From: dcatz Date: Tue, 31 Aug 2021 11:13:03 +0200 Subject: [PATCH] Refactoring Listener and Service and adding listener service overload. (#3) --- README.md | 2 +- src/DependencyInjection/Configuration.php | 3 + .../MbDoctrineLogExtension.php | 8 +-- src/EventListener/Logger.php | 47 +++++++++++--- src/Resources/config/services.xml | 6 +- src/Service/Logger.php | 64 ------------------- 6 files changed, 46 insertions(+), 84 deletions(-) delete mode 100644 src/Service/Logger.php diff --git a/README.md b/README.md index 01e14af..59a334e 100644 --- a/README.md +++ b/README.md @@ -189,7 +189,7 @@ Configuration ------------- If you are using blameable trait or timestampable trait then you must want to log changes of those properties. Or you may have some common properties that is added in many entries and you don't want to log the changes of those properties then add this configuration. -A different entity manager can be configured to support multiple entity managers. +A different entity manager can be configured to support multiple entity managers. The listener service could be extended to make any required changes. ```yaml # config/packages/mb_doctrine_log.yaml diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 243d549..f73d59b 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -26,6 +26,9 @@ public function getConfigTreeBuilder() ->scalarNode('entity_manager') ->defaultValue('default') ->end() + ->scalarNode('listener') + ->defaultValue('mb_doctrine_log.event_listener.logger') + ->end() ; return $treeBuilder; diff --git a/src/DependencyInjection/MbDoctrineLogExtension.php b/src/DependencyInjection/MbDoctrineLogExtension.php index 2c1f75b..79d1ef5 100644 --- a/src/DependencyInjection/MbDoctrineLogExtension.php +++ b/src/DependencyInjection/MbDoctrineLogExtension.php @@ -28,11 +28,9 @@ public function load(array $configs, ContainerBuilder $container) $emName = sprintf('doctrine.orm.%s_entity_manager', $config['entity_manager']); $emReference = new Reference($emName); - $definition = $container->getDefinition('mb_doctrine_log.event_listener.logger'); - $definition->setArgument(5, $config['ignore_properties']); - $definition->setArgument(0, $emReference); - - $definition = $container->getDefinition('mb_doctrine_log.service.logger'); + $definition = $container->getDefinition($config['listener']); $definition->setArgument(0, $emReference); + $definition->setArgument(4, $config['ignore_properties']); } } + diff --git a/src/EventListener/Logger.php b/src/EventListener/Logger.php index d171668..98f9826 100644 --- a/src/EventListener/Logger.php +++ b/src/EventListener/Logger.php @@ -32,11 +32,6 @@ class Logger */ private $em; - /** - * @var LoggerService - */ - private $loggerService; - /** * @var Serializer */ @@ -59,22 +54,20 @@ class Logger /** * Logger constructor. + * * @param EntityManagerInterface $em - * @param LoggerService $loggerService * @param Serializer $serializer * @param AnnotationReader $reader * @param array $ignoreProperties */ public function __construct( EntityManagerInterface $em, - LoggerService $loggerService, Serializer $serializer, AnnotationReader $reader, LoggerInterface $monolog, array $ignoreProperties ) { $this->em = $em; - $this->loggerService = $loggerService; $this->serializer = $serializer; $this->reader = $reader; $this->ignoreProperties = $ignoreProperties; @@ -133,6 +126,20 @@ public function postFlush(PostFlushEventArgs $args) } } + /** + * Saves a log + * + * @param LogEntity $log + * @return bool + */ + public function save(LogEntity $log) : bool + { + $this->em->persist($log); + $this->em->flush(); + + return true; + } + /** * Log the action * @@ -181,7 +188,7 @@ private function log($entity, $action) if ($action === LogEntity::ACTION_UPDATE && !$changes) { // Log nothing } else { - $this->logs[] = $this->loggerService->log( + $this->logs[] = $this->createLogEntity( $entity, $action, $changes @@ -192,4 +199,26 @@ private function log($entity, $action) $this->monolog->error($e->getMessage()); } } + + /** + * Creates the log entity + * + * @param object $object + * @param string $action + * @param string $changes + * @return LogEntity + */ + private function createLogEntity($object, $action, $changes = null) : LogEntity + { + $log = new LogEntity(); + $log + ->setObjectClass(str_replace('Proxies\__CG__\\', '', get_class($object))) + ->setForeignKey($object->getId()) + ->setAction($action) + ->setChanges($changes) + ; + + return $log; + } } + diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 4c208f5..2aedf0a 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -11,18 +11,14 @@ - - - - - + diff --git a/src/Service/Logger.php b/src/Service/Logger.php deleted file mode 100644 index 9209a7f..0000000 --- a/src/Service/Logger.php +++ /dev/null @@ -1,64 +0,0 @@ -em = $em; - } - - /** - * Logs object change - * - * @param object $object - * @param string $action - * @param string $changes - * @return LogEntity - */ - public function log($object, $action, $changes = null) : LogEntity - { - $log = new LogEntity(); - $log - ->setObjectClass(str_replace('Proxies\__CG__\\', '', get_class($object))) - ->setForeignKey($object->getId()) - ->setAction($action) - ->setChanges($changes) - ; - - return $log; - } - - /** - * Saves a log - * - * @param LogEntity $log - * @return bool - */ - public function save(LogEntity $log) : bool - { - $this->em->persist($log); - $this->em->flush(); - - return true; - } -}