From 1d6b29dc84403bf0fd6163c0a9eef7f45a62198a Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Thu, 20 May 2021 15:26:30 +0200 Subject: [PATCH] add json support --- .../Versionable/Console/ConvertEncoding.php | 58 +++++++++++++++++++ .../Versionable/Providers/ServiceProvider.php | 6 ++ src/Mpociot/Versionable/Version.php | 2 +- src/Mpociot/Versionable/VersionableTrait.php | 13 +++-- src/config/config.php | 9 ++- 5 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 src/Mpociot/Versionable/Console/ConvertEncoding.php diff --git a/src/Mpociot/Versionable/Console/ConvertEncoding.php b/src/Mpociot/Versionable/Console/ConvertEncoding.php new file mode 100644 index 0000000..0f9f640 --- /dev/null +++ b/src/Mpociot/Versionable/Console/ConvertEncoding.php @@ -0,0 +1,58 @@ +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."); + } +} 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', +];