Skip to content

Commit

Permalink
add model commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Jun 7, 2024
1 parent 521035b commit c1cbd05
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/Commands/Model/ModelBackupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Kiwilan\Steward\Commands\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Kiwilan\Steward\Commands\Commandable;

class ModelBackupCommand extends Commandable
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'model:backup {model : The model to backup, can be `User` or `App\Models\User`}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Backup model from the database';

/**
* Execute the console command.
*/
public function handle()
{
$this->title();

$modelName = (string) $this->argument('model');

$model = ModelBackupCommand::getModel($modelName);
$path = ModelBackupCommand::getPath($model['filename']);

if (file_exists($path)) {
$contents = json_decode(file_get_contents($path), true);
if (count($contents) === 0) {
$this->info("{$model['name']} file is empty, deleting...");
unlink($path);
}
}

$items = DB::table($model['instance']->getTable())->get();
$json = $items->toJson(JSON_PRETTY_PRINT);

file_put_contents($path, $json);

$this->info("Saved {$model['name']} to {$path}");
}

/**
* @return array{name: string, filename: string, model: string, instance: \Illuminate\Database\Eloquent\Model}
*/
public static function getModel(string $modelName): array
{
if (str_contains($modelName, '\\')) {
$model = $modelName;
} else {
$model = 'App\\Models\\'.$modelName;
}

$instance = new $model;
$reflection = new \ReflectionClass($instance);
$className = $reflection->name;
$className = str_replace('\\', '_', $className);
$fileName = Str::slug($className).'.json';

return [
'name' => $reflection->name,
'model' => $model,
'filename' => $fileName,
'instance' => $instance,
];
}

public static function getPath(string $filename): string
{
$basePath = storage_path('app'.DIRECTORY_SEPARATOR.'model-backup');
if (! is_dir($basePath)) {
mkdir($basePath, 0775, true);
}

return $basePath.DIRECTORY_SEPARATOR.$filename;
}
}
50 changes: 50 additions & 0 deletions src/Commands/Model/ModelRestoreCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Kiwilan\Steward\Commands\Model;

use Illuminate\Console\Command;
use Kiwilan\Steward\Commands\Commandable;

class ModelRestoreCommand extends Commandable
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'model:restore {model : The model to restore, can be `User` or `App\Models\User`}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Restore model to the database';

/**
* Execute the console command.
*/
public function handle()
{
$this->title();

$modelName = (string) $this->argument('model');

$model = ModelBackupCommand::getModel($modelName);
$path = ModelBackupCommand::getPath($model['filename']);

if (! file_exists($path)) {
$this->error("{$model['name']} file does not exist");

return;
}

$json = file_get_contents($path);
$items = json_decode($json, true);

$model['instance']::query()->truncate();
$model['instance']::query()->insert($items);

$this->info("Restored {$model['name']} from {$path}");
}
}
2 changes: 2 additions & 0 deletions src/StewardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function configurePackage(Package $package): void
\Kiwilan\Steward\Commands\ClearFreshCommand::class,
\Kiwilan\Steward\Commands\Jobs\JobsListCommand::class,
\Kiwilan\Steward\Commands\Jobs\JobsClearCommand::class,
\Kiwilan\Steward\Commands\Model\ModelBackupCommand::class,
\Kiwilan\Steward\Commands\Model\ModelRestoreCommand::class,
]);
}

Expand Down

0 comments on commit c1cbd05

Please sign in to comment.