Skip to content

Commit 046814a

Browse files
committed
add json support
1 parent 043b311 commit 046814a

File tree

5 files changed

+87
-9
lines changed

5 files changed

+87
-9
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Mpociot\Versionable\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Mpociot\Versionable\Version;
7+
8+
class ConvertEncoding extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'versionable:convert-encoding {--encoding=}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Convert the encoding from JSON to serialize() or vice versa';
23+
24+
protected $encodingCheck = [
25+
'serialize' => '{',
26+
'json' => 'a:',
27+
];
28+
29+
/**
30+
* Execute the console command.
31+
*
32+
* @return mixed
33+
*/
34+
public function handle()
35+
{
36+
$encoding = $this->option('encoding') ?? config('versionable.encoding');
37+
$sourceEncoding = $encoding === 'json' ? 'serialize' : 'json';
38+
$versions = Version::query()->get();
39+
40+
foreach ($versions as $version) {
41+
if (!$this->validateData($version, $sourceEncoding)) {
42+
$this->error("Data does not appear to be as encoded $sourceEncoding: {$version->model_data}");
43+
44+
return;
45+
}
46+
47+
$version->model_data = $encoding === 'serialize'
48+
? serialize(json_decode($version->model_data, true))
49+
: json_encode(unserialize($version->model_data));
50+
51+
$version->save();
52+
}
53+
54+
$this->info("Converted {$versions->count()} models to '$encoding' encoding.");
55+
}
56+
57+
protected function validateData(Version $version, $sourceEncoding)
58+
{
59+
if (strpos($version->model_data, $this->encodingCheck[$sourceEncoding]) !== 0) {
60+
61+
return false;
62+
}
63+
64+
return true;
65+
}
66+
}

src/Mpociot/Versionable/Providers/ServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
class ServiceProvider extends LaravelServiceProvider
88
{
9+
protected $commands = [
10+
\Mpociot\Versionable\Console\ConvertEncoding::class,
11+
];
12+
913
/**
1014
* Register bindings in the container.
1115
*
@@ -14,6 +18,8 @@ class ServiceProvider extends LaravelServiceProvider
1418
public function register ()
1519
{
1620
$this->mergeConfigFrom(__DIR__.'/../../../config/config.php', 'versionable');
21+
22+
$this->commands($this->commands);
1723
}
1824

1925
/**

src/Mpociot/Versionable/Version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getModel()
5353

5454
$model = new $this->versionable_type();
5555
$model->unguard();
56-
$model->fill(unserialize($modelData));
56+
$model->fill(config('versionable.encoding') === 'json' ? json_decode($modelData, true) : unserialize($modelData));
5757
$model->exists = true;
5858
$model->reguard();
5959
return $model;

src/Mpociot/Versionable/VersionableTrait.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ trait VersionableTrait
1414
/**
1515
* Retrieve, if exists, the property that define that Version model.
1616
* If no property defined, use the default Version model.
17-
*
17+
*
1818
* Trait cannot share properties whth their class !
1919
* http://php.net/manual/en/language.oop5.traits.php
2020
* @return unknown|string
@@ -173,10 +173,11 @@ protected function versionablePostSave()
173173
$version->versionable_id = $this->getKey();
174174
$version->versionable_type = method_exists($this, 'getMorphClass') ? $this->getMorphClass() : get_class($this);
175175
$version->user_id = $this->getAuthUserId();
176-
176+
177177
$versionedHiddenFields = $this->versionedHiddenFields ?? [];
178178
$this->makeVisible($versionedHiddenFields);
179-
$version->model_data = serialize($this->attributesToArray());
179+
$version->model_data = config('versionable.encoding') === 'json'
180+
? json_encode($this->attributesToArray()) : serialize($this->attributesToArray());
180181
$this->makeHidden($versionedHiddenFields);
181182

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

192193
/**
193194
* Delete old versions of this model when the reach a specific count.
194-
*
195+
*
195196
* @return void
196197
*/
197198
private function purgeOldVersions()
198199
{
199200
$keep = isset($this->keepOldVersions) ? $this->keepOldVersions : 0;
200-
201+
201202
if ((int)$keep > 0) {
202203
$count = $this->versions()->count();
203-
204+
204205
if ($count > $keep) {
205206
$this->getLatestVersions()
206207
->take($count)

src/config/config.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
* Feel free to change this, if you need specific version
88
* model logic.
99
*/
10-
'version_model' => \Mpociot\Versionable\Version::class
10+
'version_model' => \Mpociot\Versionable\Version::class,
1111

12-
];
12+
/*
13+
* The encoding to use for the model data encoding.
14+
* Default is 'serialize' and uses PHP serialize() but 'json' is also supported
15+
*/
16+
'encoding' => 'serialize',
17+
];

0 commit comments

Comments
 (0)