-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?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'; | ||
|
||
/** | ||
* 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() | ||
{ | ||
$versions = Version::query()->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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters