Skip to content

Commit

Permalink
add json support
Browse files Browse the repository at this point in the history
  • Loading branch information
lsmith77 committed May 22, 2021
1 parent 043b311 commit 1d6b29d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
58 changes: 58 additions & 0 deletions src/Mpociot/Versionable/Console/ConvertEncoding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Mpociot\Versionable\Console;

use Illuminate\Console\Command;
use Mpociot\Versionable\Version;

class ConvertEncoding extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'versionable:convert-encoding {--encoding=}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Convert the encoding from JSON to serialize() or vice versa';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$encoding = $this->option('encoding') ?? config('versionable.encoding');

$versions = Version::query()->get();

foreach ($versions as $version) {
if ($encoding === 'serialize') {
if (strpos($version->model_data, '{') !== 0) {
$this->error('Data does not appear to be encoded via json_encode():'.$version->model_data);

return;
}

$version->model_data = serialize(json_decode($version->model_data, true));
} else {
if (strpos($version->model_data, 'a:') !== 0) {
$this->error('Data does not appear to be encoded via serialize():'.$version->model_data);

return;
}
$version->model_data = json_encode(unserialize($version->model_data));
}

$version->save();
}

$this->info("Converted {$versions->count()} models to '$encoding' encoding.");
}
}
6 changes: 6 additions & 0 deletions src/Mpociot/Versionable/Providers/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

class ServiceProvider extends LaravelServiceProvider
{
protected $commands = [
\Mpociot\Versionable\Console\ConvertEncoding::class,
];

/**
* Register bindings in the container.
*
Expand All @@ -14,6 +18,8 @@ class ServiceProvider extends LaravelServiceProvider
public function register ()
{
$this->mergeConfigFrom(__DIR__.'/../../../config/config.php', 'versionable');

$this->commands($this->commands);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Mpociot/Versionable/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getModel()

$model = new $this->versionable_type();
$model->unguard();
$model->fill(unserialize($modelData));
$model->fill(config('versionable.encoding') === 'json' ? json_decode($modelData, true) : unserialize($modelData));
$model->exists = true;
$model->reguard();
return $model;
Expand Down
13 changes: 7 additions & 6 deletions src/Mpociot/Versionable/VersionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait VersionableTrait
/**
* Retrieve, if exists, the property that define that Version model.
* If no property defined, use the default Version model.
*
*
* Trait cannot share properties whth their class !
* http://php.net/manual/en/language.oop5.traits.php
* @return unknown|string
Expand Down Expand Up @@ -173,10 +173,11 @@ protected function versionablePostSave()
$version->versionable_id = $this->getKey();
$version->versionable_type = method_exists($this, 'getMorphClass') ? $this->getMorphClass() : get_class($this);
$version->user_id = $this->getAuthUserId();

$versionedHiddenFields = $this->versionedHiddenFields ?? [];
$this->makeVisible($versionedHiddenFields);
$version->model_data = serialize($this->attributesToArray());
$version->model_data = config('versionable.encoding') === 'json'
? json_encode($this->attributesToArray()) : serialize($this->attributesToArray());
$this->makeHidden($versionedHiddenFields);

if (!empty( $this->reason )) {
Expand All @@ -191,16 +192,16 @@ protected function versionablePostSave()

/**
* Delete old versions of this model when the reach a specific count.
*
*
* @return void
*/
private function purgeOldVersions()
{
$keep = isset($this->keepOldVersions) ? $this->keepOldVersions : 0;

if ((int)$keep > 0) {
$count = $this->versions()->count();

if ($count > $keep) {
$this->getLatestVersions()
->take($count)
Expand Down
9 changes: 7 additions & 2 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
* Feel free to change this, if you need specific version
* model logic.
*/
'version_model' => \Mpociot\Versionable\Version::class
'version_model' => \Mpociot\Versionable\Version::class,

];
/*
* The encoding to use for the model data encoding.
* Default is 'serialize' and uses PHP serialize() but 'json' is also supported
*/
'encoding' => 'serialize',
];

0 comments on commit 1d6b29d

Please sign in to comment.