Skip to content

Commit c1cbd05

Browse files
add model commands
1 parent 521035b commit c1cbd05

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Kiwilan\Steward\Commands\Model;
4+
5+
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Str;
7+
use Kiwilan\Steward\Commands\Commandable;
8+
9+
class ModelBackupCommand extends Commandable
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'model:backup {model : The model to backup, can be `User` or `App\Models\User`}';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Backup model from the database';
24+
25+
/**
26+
* Execute the console command.
27+
*/
28+
public function handle()
29+
{
30+
$this->title();
31+
32+
$modelName = (string) $this->argument('model');
33+
34+
$model = ModelBackupCommand::getModel($modelName);
35+
$path = ModelBackupCommand::getPath($model['filename']);
36+
37+
if (file_exists($path)) {
38+
$contents = json_decode(file_get_contents($path), true);
39+
if (count($contents) === 0) {
40+
$this->info("{$model['name']} file is empty, deleting...");
41+
unlink($path);
42+
}
43+
}
44+
45+
$items = DB::table($model['instance']->getTable())->get();
46+
$json = $items->toJson(JSON_PRETTY_PRINT);
47+
48+
file_put_contents($path, $json);
49+
50+
$this->info("Saved {$model['name']} to {$path}");
51+
}
52+
53+
/**
54+
* @return array{name: string, filename: string, model: string, instance: \Illuminate\Database\Eloquent\Model}
55+
*/
56+
public static function getModel(string $modelName): array
57+
{
58+
if (str_contains($modelName, '\\')) {
59+
$model = $modelName;
60+
} else {
61+
$model = 'App\\Models\\'.$modelName;
62+
}
63+
64+
$instance = new $model;
65+
$reflection = new \ReflectionClass($instance);
66+
$className = $reflection->name;
67+
$className = str_replace('\\', '_', $className);
68+
$fileName = Str::slug($className).'.json';
69+
70+
return [
71+
'name' => $reflection->name,
72+
'model' => $model,
73+
'filename' => $fileName,
74+
'instance' => $instance,
75+
];
76+
}
77+
78+
public static function getPath(string $filename): string
79+
{
80+
$basePath = storage_path('app'.DIRECTORY_SEPARATOR.'model-backup');
81+
if (! is_dir($basePath)) {
82+
mkdir($basePath, 0775, true);
83+
}
84+
85+
return $basePath.DIRECTORY_SEPARATOR.$filename;
86+
}
87+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Kiwilan\Steward\Commands\Model;
4+
5+
use Illuminate\Console\Command;
6+
use Kiwilan\Steward\Commands\Commandable;
7+
8+
class ModelRestoreCommand extends Commandable
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'model:restore {model : The model to restore, can be `User` or `App\Models\User`}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Restore model to the database';
23+
24+
/**
25+
* Execute the console command.
26+
*/
27+
public function handle()
28+
{
29+
$this->title();
30+
31+
$modelName = (string) $this->argument('model');
32+
33+
$model = ModelBackupCommand::getModel($modelName);
34+
$path = ModelBackupCommand::getPath($model['filename']);
35+
36+
if (! file_exists($path)) {
37+
$this->error("{$model['name']} file does not exist");
38+
39+
return;
40+
}
41+
42+
$json = file_get_contents($path);
43+
$items = json_decode($json, true);
44+
45+
$model['instance']::query()->truncate();
46+
$model['instance']::query()->insert($items);
47+
48+
$this->info("Restored {$model['name']} from {$path}");
49+
}
50+
}

src/StewardServiceProvider.php

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public function configurePackage(Package $package): void
4242
\Kiwilan\Steward\Commands\ClearFreshCommand::class,
4343
\Kiwilan\Steward\Commands\Jobs\JobsListCommand::class,
4444
\Kiwilan\Steward\Commands\Jobs\JobsClearCommand::class,
45+
\Kiwilan\Steward\Commands\Model\ModelBackupCommand::class,
46+
\Kiwilan\Steward\Commands\Model\ModelRestoreCommand::class,
4547
]);
4648
}
4749

0 commit comments

Comments
 (0)