Skip to content

Commit c156ea9

Browse files
committed
feat: add model commands to schema
1 parent bba9185 commit c156ea9

4 files changed

Lines changed: 112 additions & 0 deletions

File tree

src/Commands/DatabaseCommands.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public static function commands()
1717
DatabaseSeedCommand::class,
1818
DeleteSchemaCommand::class,
1919
GenerateSchemaCommand::class,
20+
GenerateModelCommand::class,
21+
DeleteModelCommand::class,
2022
];
2123
}
2224
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Leaf\Commands;
4+
5+
use Leaf\Sprout\Command;
6+
use Illuminate\Support\Str;
7+
8+
class DeleteModelCommand extends Command
9+
{
10+
protected $signature = 'd:model {model : The name of the model}';
11+
protected $description = 'Delete a model';
12+
protected $help = 'Delete a model file';
13+
14+
protected function handle()
15+
{
16+
$model = Str::studly($this->argument('model'));
17+
18+
$file = function_exists('ModelsPath')
19+
? getcwd() . DIRECTORY_SEPARATOR . ModelsPath("$model.php")
20+
: getcwd() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . "$model.php";
21+
22+
if (!\Leaf\FS\File::exists($file)) {
23+
$this->error("$model doesn't exist!");
24+
return 1;
25+
}
26+
27+
if (!\Leaf\FS\File::delete($file)) {
28+
$this->error("Couldn't delete $file, you might need to remove it manually.");
29+
return 1;
30+
}
31+
32+
$this->comment("$model deleted successfully");
33+
34+
if (\Leaf\FS\Directory::isEmpty($dirname = dirname($file))) {
35+
if (sprout()->confirm("> $dirname is empty. Delete folder?")) {
36+
if (\Leaf\FS\Directory::delete($dirname)) {
37+
$this->comment("$dirname deleted successfully!");
38+
}
39+
}
40+
}
41+
42+
return 0;
43+
}
44+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Leaf\Commands;
4+
5+
use Leaf\Sprout\Command;
6+
use Illuminate\Support\Str;
7+
8+
class GenerateModelCommand extends Command
9+
{
10+
protected $signature = 'g:model
11+
{model : The name of the model}
12+
{--m|migration? : Create a migration for model}';
13+
protected $description = 'Create a new model class';
14+
protected $help = 'Create a new model class';
15+
16+
protected function handle()
17+
{
18+
$model = Str::singular(Str::studly($this->argument('model')));
19+
$className = $model;
20+
21+
if (strpos($model, '/') && strpos($model, '/') !== 0) {
22+
list($dirname, $className) = explode('/', $model);
23+
}
24+
25+
$file = function_exists('ModelsPath')
26+
? getcwd() . DIRECTORY_SEPARATOR . ModelsPath("$model.php")
27+
: getcwd() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . "$model.php";
28+
29+
if (file_exists($file)) {
30+
$this->error('Model already exists');
31+
return 1;
32+
}
33+
34+
\Leaf\FS\File::create($file, function () use ($className) {
35+
$fileContent = \file_get_contents(__DIR__ . '/stubs/model.stub');
36+
$fileContent = str_replace('ClassName', $className, $fileContent);
37+
38+
return $fileContent;
39+
}, ['recursive' => true]);
40+
41+
$this->info("<comment>$model</comment> model generated");
42+
43+
if ($this->option('migration')) {
44+
$migration = Str::snake(Str::plural($model));
45+
$process = sprout()->process("php leaf g:migration $migration")->run();
46+
47+
$this->info(
48+
$process === 0 ?
49+
"<comment>$migration</comment> migration generated" :
50+
"<error>Couldn't generate migration</error>"
51+
);
52+
53+
return $process;
54+
}
55+
56+
return 0;
57+
}
58+
}

src/Commands/stubs/model.stub

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
class ClassName extends Model
6+
{
7+
//
8+
}

0 commit comments

Comments
 (0)