Comments (7)
Hi @ronisaha i'm still working with v1.4.7 but I can share with you my entity resolver in case it help you
<?php
namespace LoginBundle\Resolver;
use Symfony\Component\EventDispatcher\Event;
use Xiidea\EasyAuditBundle\Events\DoctrineEntityEvent;
use Xiidea\EasyAuditBundle\Resolver\EntityEventResolver as BaseResolver;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Psr\Log\LoggerInterface;
class EntityEventResolver extends BaseResolver
{
private $logger;
private $serializer;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* @param Event|DoctrineEntityEvent $event
* @param $eventName
*
* @return array
*/
public function getEventLogInfo(Event $event, $eventName)
{
if (!$event instanceof DoctrineEntityEvent) {
return null;
}
$this->initialize($event, $eventName);
$changesMetaData = $this->getChangeSets($this->entity);
if ($this->isUpdateEvent() && $changesMetaData == NULL) {
return NULL;
}
$reflectionClass = $this->getReflectionClassFromObject($this->entity);
$typeName = $reflectionClass->getShortName();
//$eventType = $this->getEventType($typeName);
$eventDescription = $this->getDescriptionString($reflectionClass, $typeName);
$data = [];
$ignorar = array('tu', 'ts', 'updated', 'created', 'transitions', '__initializer__', '__cloner__', '__isInitialized__', 'timezone', 'salt', 'password', 'newPassword');
if (empty($changesMetaData)) {
$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer();
$this->serializer = new Serializer(array($normalizer), array($encoder));
$reflClass = new \ReflectionClass($this->entity);
$arrayDatos = [];
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
if (
0 !== $reflMethod->getNumberOfRequiredParameters() ||
$reflMethod->isStatic() ||
$reflMethod->isConstructor() ||
$reflMethod->isDestructor()
) {
continue;
}
$name = $reflMethod->name;
$attributeName = null;
if (0 === strpos($name, 'get') || 0 === strpos($name, 'has')) {
// getters and hassers
$attributeName = substr($name, 3);
if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
} elseif (0 === strpos($name, 'is')) {
// issers
$attributeName = substr($name, 2);
if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
}
if (!is_null($attributeName) && !in_array($attributeName, $ignorar))
{
$val = $this->entity->$name();
$es_colecion = (is_array($val) || ($val instanceof \ArrayAccess && $val instanceof \Traversable));
if(!$es_colecion) {
$arrayDatos[$attributeName] = $this->extraerValor($val);
} else {
$arrayDatos[$attributeName] = [];
foreach ($val as $item) {
$arrayDatos[$attributeName][] = $this->extraerValor($item);
}
}
}
}
$data = $this->serializer->serialize($arrayDatos,'json');
} elseif (is_string($changesMetaData)) {
$data = $changesMetaData;
} else {
foreach ($changesMetaData as $key => $value) {
if($value[0]!=$value[1] && !in_array($key, $ignorar)) {
if(gettype($value[0])== 'object') {
$entity = null;
foreach (explode('\\',get_class($value[0])) as $v) {
$entity = $v;
}
if((method_exists($value[0], 'getId'))) {
$value1 = $value[0]->getId();
$value2 = is_null($value[1]) ? null : $value[1]->getId();
$data[$key] = array('entity' => $entity, 'id' => array($value1, $value2));
} else {
$value1 = $this->isDateOrNot($value[0]);
$value2 = $this->isDateOrNot($value[1]);
$data[$key] = array($value1, $value2);
}
} else {
$data[$key] = $value;
}
}
}
if (!empty($data)) {
$data = json_encode($data);
}
}
$infoFinal = array(
'table' => $typeName,
'type' => $this->translateToSpanishType(),
'description' => $eventDescription,
);
if ($typeName!='Agentes') {
$infoFinal['tableId'] = $this->getProperty('id');
}
if (!empty($data)) {
$infoFinal['data'] = $data;
}
return $infoFinal;
}
protected function extraerValor ($value) {
$es_entidad = method_exists($value, 'getId');
if($es_entidad) {
return $value->getId();
} elseif ($value instanceof \Date) {
return $value->format("Y-m-d");
} elseif ($value instanceof \DateTime) {
return $value->format("Y-m-d H:i:s");
} elseif (gettype($value)== 'object') {
$value = json_encode($value, JSON_PRETTY_PRINT|JSON_ERROR_UNSUPPORTED_TYPE|JSON_PARTIAL_OUTPUT_ON_ERROR);
}
return $value;
}
/**
* @param DoctrineEntityEvent $event
* @param string $eventName
*/
private function initialize(DoctrineEntityEvent $event, $eventName)
{
$this->eventShortName = null;
$this->propertiesFound = array();
$this->eventName = $eventName;
$this->event = $event;
$this->entity = $event->getLifecycleEventArgs()->getEntity();
}
protected function isDateOrNot($var) {
if(is_a($var, 'DateTime')){
return print_r(($var->format('H:i:s')!='00:00:00') ? $var->format('c') : $var->format('Y-m-d'), true);
} else {
return print_r($var, true);
}
}
protected function getDescriptionString(\ReflectionClass $reflectionClass, $typeName)
{
$property = $this->getBestCandidatePropertyForIdentify($reflectionClass);
$descriptionTemplate = '%s %s';//'%s ha sido %s ';
if ($property) {
$descriptionTemplate .= sprintf('.%s = %s', $property, $this->getProperty($property));
}
return sprintf($descriptionTemplate,
$this->translateToSpanishType(),
$typeName);
}
/**
* @param $changesMetaData
*
* @return null|string
*/
protected function getAsSerializedString($changesMetaData)
{
if (empty($changesMetaData)) {
return NULL;
} elseif (is_string($changesMetaData)) {
return $changesMetaData;
}
return serialize($changesMetaData);
}
protected function translateToSpanishType() {
$accion = null;
switch ($this->getEventShortName()) {
case 'created':
$accion = 'Crear';
break;
case 'updated':
$accion = 'Actualizar';
break;
case 'deleted':
$accion = 'Eliminar';
break;
default:
$accion = 'Acción';
break;
}
return $accion;
}
}
from easyauditbundle.
@ronisaha I can confirm the latest branch does work for both ODM and ORM
from easyauditbundle.
Yes you are right, I was waiting for some time to get a feedback from users, as I'm not using ODM in any of my projects.
Also I was waiting for SF4.3 release to test it. But could not make time for it. Hopefully I'll release a new tag with SF4.3 support checking soon.
from easyauditbundle.
Planning to rename following config values:
Old value | New value |
---|---|
entity_class | audit_log_class |
entity_event_resolver | doctrine_event_resolver |
doctrine_entities | doctrine_objects |
Any suggestion/thought from any one? @wh1pp3rz, @braianj
Thanks.
from easyauditbundle.
from easyauditbundle.
@wh1pp3rz Could you please confirm the latest odm branch is working for both ORM and Document ? After your confirmation i'll merge it to master branch.
from easyauditbundle.
Hi @ronisaha. Since this feature has lot of changes, is it still in master branch to get more matured, and to be better tested? It is kind of experimental feature?
I don't have rush in having this feature since I'm currently only using ORM, I'm just asking if is it planned to release a new tag regarding this.
Thanks.
from easyauditbundle.
Related Issues (20)
- How to log the many to many changes HOT 2
- Problem with DoctrineSubscriber delete event when using proxy classes HOT 2
- Saving collection of entity form types problem: PreUpdateEventArgs::__construct() must be of the type array, null given HOT 10
- Block Chain Audit Trail Logger
- How to save the circular reference HOT 1
- The service "xiidea.easy_audit.entity_delete_event.subscriber" must be public as event subscribers are lazy-loaded. HOT 3
- undefined index throwing notice
- Attempted to call an undefined method named getBestCandidatePropertyForIdentify of class Resolver\EntityEventResolver HOT 4
- Incompatibility for 1.4.6 ->1.4.9 HOT 2
- flush($entity) is deprecated, and duplicates updates and logs HOT 2
- Possible incompatibility with symfony/contracts
- Doctrine ODM namespace error HOT 5
- Add support for multiple entity managers HOT 1
- Symfony 5 Support
- Support for Symfony 4.4.* LTS? HOT 2
- Allow PHP 8 in composer.json version constraints. HOT 1
- Upgraded to Symfony 5 HOT 2
- Get previous value and changed value. HOT 1
- Symfony 6 support? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from easyauditbundle.