Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DispatchAudit Serialization Problems #893

Merged
merged 3 commits into from
May 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove SerializesModels trait
  • Loading branch information
erikn69 committed Dec 28, 2023
commit da23bab5c6f04f1412f46e858c938540590e5563
37 changes: 12 additions & 25 deletions src/Events/DispatchAudit.php
Original file line number Diff line number Diff line change
@@ -2,17 +2,11 @@

namespace OwenIt\Auditing\Events;

use Illuminate\Queue\SerializesModels;
use OwenIt\Auditing\Contracts\Auditable;
use ReflectionClass;

class DispatchAudit
{
use SerializesModels {
__serialize as __serialize_model;
__unserialize as __unserialize_model;
}

/**
* The Auditable model.
*
@@ -37,10 +31,14 @@ public function __construct(Auditable $model)
*/
public function __serialize()
{
$values = $this->__serialize_model();
$values = [
'class' => get_class($this->model),
'model_data' => [
'exists' => true,
'connection' => $this->model->getQueueableConnection()
]
];

$values['model_data'] = ['exists' => true];
$reflection = new ReflectionClass($this->model);
$customProperties = array_merge([
'attributes',
'original',
@@ -53,11 +51,11 @@ public function __serialize()
'preloadedResolverData',
], $this->model->auditEventSerializedProperties ?? []);

$reflection = new ReflectionClass($this->model);

foreach ($customProperties as $key) {
try {
$values['model_data'][$key] = $this->getSerializedPropertyValue(
$this->getModelPropertyValue($reflection, $key)
);
$values['model_data'][$key] = $this->getModelPropertyValue($reflection, $key);
} catch (\Throwable $e){
//
}
@@ -74,7 +72,7 @@ public function __serialize()
*/
public function __unserialize(array $values)
{
$this->__unserialize_model($values);
$this->model = new $values['class'];

$reflection = new ReflectionClass($this->model);
foreach ($values['model_data'] as $key => $value) {
@@ -84,17 +82,6 @@ public function __unserialize(array $values)
return $values;
}

/**
* Restore the model from the model identifier instance.
*
* @param \Illuminate\Contracts\Database\ModelIdentifier $value
* @return \Illuminate\Database\Eloquent\Model
*/
public function restoreModel($value)
{
return (new $value->class)->setConnection($value->connection);
}

/**
* Set the property value for the given property.
*/
@@ -104,7 +91,7 @@ protected function setModelPropertyValue(ReflectionClass $reflection, string $na

$property->setAccessible(true);

$property->setValue($this->model, $this->getRestoredPropertyValue($value));
$property->setValue($this->model, $value);
}

/**