diff --git a/src/Mpociot/Versionable/Console/ConvertEncoding.php b/src/Mpociot/Versionable/Console/ConvertEncoding.php new file mode 100644 index 0000000..7663150 --- /dev/null +++ b/src/Mpociot/Versionable/Console/ConvertEncoding.php @@ -0,0 +1,54 @@ +get(); + + foreach ($versions as $version) { + if (config('versionable.encoding') === 'json') { + 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)); + } 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(); + } + } +} diff --git a/src/Mpociot/Versionable/Providers/ServiceProvider.php b/src/Mpociot/Versionable/Providers/ServiceProvider.php index d7c5c4e..19017c9 100644 --- a/src/Mpociot/Versionable/Providers/ServiceProvider.php +++ b/src/Mpociot/Versionable/Providers/ServiceProvider.php @@ -6,6 +6,10 @@ class ServiceProvider extends LaravelServiceProvider { + protected $commands = [ + \Mpociot\Versionable\Console\ConvertEncoding::class, + ]; + /** * Register bindings in the container. * @@ -14,6 +18,8 @@ class ServiceProvider extends LaravelServiceProvider public function register () { $this->mergeConfigFrom(__DIR__.'/../../../config/config.php', 'versionable'); + + $this->commands($this->commands); } /** diff --git a/src/Mpociot/Versionable/Version.php b/src/Mpociot/Versionable/Version.php index d145eb1..5519de7 100644 --- a/src/Mpociot/Versionable/Version.php +++ b/src/Mpociot/Versionable/Version.php @@ -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; diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php index 9be633a..13e4e56 100644 --- a/src/Mpociot/Versionable/VersionableTrait.php +++ b/src/Mpociot/Versionable/VersionableTrait.php @@ -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 @@ -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 )) { @@ -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) diff --git a/src/config/config.php b/src/config/config.php index 637864c..d50e620 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -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, -]; \ No newline at end of file + /* + * The encoding to use for the model data encoding. + * Default is 'serialize' and uses PHP serialize() but 'json' is also supported + */ + 'encoding' => 'serialize', +];