Skip to content

Commit

Permalink
Refactoring Listener and Service and adding listener service overload. (
Browse files Browse the repository at this point in the history
  • Loading branch information
dcatz authored Aug 31, 2021
1 parent 24a628f commit 5a6d14b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 84 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions src/DependencyInjection/MbDoctrineLogExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

47 changes: 38 additions & 9 deletions src/EventListener/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ class Logger
*/
private $em;

/**
* @var LoggerService
*/
private $loggerService;

/**
* @var Serializer
*/
Expand All @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}

6 changes: 1 addition & 5 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@
<tag name="doctrine.event_listener" event="postFlush" priority="1"/>
<tag name="doctrine.event_listener" event="preRemove" priority="1"/>
<argument type="service" id="doctrine.orm.default_entity_manager"/>
<argument type="service" id="mb_doctrine_log.service.logger"/>
<argument type="service" id="jms_serializer.serializer"/>
<argument type="service" id="mb_doctrine_log.service.annotation_reader"/>
<argument type="service" id="monolog.logger"/>
</service>

<service class="Mb\DoctrineLogBundle\Service\Logger" id="mb_doctrine_log.service.logger">
<argument type="service" id="doctrine.orm.default_entity_manager" />
</service>

<service class="Mb\DoctrineLogBundle\Service\AnnotationReader" id="mb_doctrine_log.service.annotation_reader">
<argument type="service" id="annotations.reader"/>
</service>
</services>
</container>

64 changes: 0 additions & 64 deletions src/Service/Logger.php

This file was deleted.

0 comments on commit 5a6d14b

Please sign in to comment.